some new shared classes, little reorganization, preparation to cache client info

This commit is contained in:
Blue 2022-08-22 23:29:43 +03:00
parent 2ae75a4b91
commit 037dabbe06
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
20 changed files with 297 additions and 32 deletions

View file

@ -1,10 +1,10 @@
target_sources(squawk PRIVATE
archive.cpp
archive.h
storage.cpp
storage.hpp
storage.h
urlstorage.cpp
urlstorage.h
cache.cpp
cache.hpp
cache.h
)

View file

@ -18,6 +18,7 @@
#define CORE_CACHE_H
#include <map>
#include <set>
#include <QString>
@ -39,12 +40,16 @@ public:
void changeRecord(const QString& key, const T& value);
void removeRecord(const QString& key);
T getRecord(const QString& key) const;
bool checkRecord(const QString& key) const;
private:
Core::Storage<T> storage;
std::map<QString, T>* cache;
std::set<QString>* abscent;
};
}
#include "cache.hpp"
#endif // CORE_CACHE_H

View file

@ -14,21 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CORE_CACHE_HPP
#define CORE_CACHE_HPP
#include "cache.h"
#include <shared/clientinfo.h>
template class Core::Cache<Shared::ClientInfo>;
template <class T>
Core::Cache<T>::Cache(const QString& name):
storage(name),
cache(new std::map<QString, T> ()) {}
cache(new std::map<QString, T> ()),
abscent(new std::set<QString> ()) {}
template <class T>
Core::Cache<T>::~Cache() {
close();
delete cache;
delete abscent;
}
template <class T>
@ -43,27 +43,60 @@ template <class T>
void Core::Cache<T>::addRecord(const QString& key, const T& value) {
storage.addRecord(key, value);
cache->insert(std::make_pair(key, value));
abscent->erase(key);
}
template <class T>
T Core::Cache<T>::getRecord(const QString& key) const {
typename std::map<QString, T>::const_iterator itr = cache->find(key);
if (itr == cache->end()) {
T value = storage.getRecord(key);
itr = cache->insert(std::make_pair(key, value)).first;
if (abscent->count(key) > 0) {
throw Archive::NotFound(key, storage.getName().toStdString());
}
try {
T value = storage.getRecord(key);
itr = cache->insert(std::make_pair(key, value)).first;
} catch (const Archive::NotFound& error) {
abscent->insert(key);
throw error;
}
}
return itr->second;
}
template<class T>
bool Core::Cache<T>::checkRecord(const QString& key) const {
typename std::map<QString, T>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return true;
if (abscent->count(key) > 0)
return false;
try {
T value = storage.getRecord(key);
itr = cache->insert(std::make_pair(key, value)).first;
} catch (const Archive::NotFound& error) {
return false;
}
return true;
}
template<typename T>
void Core::Cache<T>::changeRecord(const QString& key, const T& value) {
storage.changeRecord(key, value);
storage.changeRecord(key, value); //there is a non straightforward behaviour: if there was no element at the sorage it will be added
cache->at(key) = value;
abscent->erase(key); //so... this line here is to make it coherent with the storage
}
template<typename T>
void Core::Cache<T>::removeRecord(const QString& key) {
storage.removeRecord(key);
cache->erase(key);
abscent->insert(key);
}
#endif //CORE_CACHE_HPP

View file

@ -43,6 +43,7 @@ public:
void changeRecord(const QString& key, const T& value);
void removeRecord(const QString& key);
T getRecord(const QString& key) const;
QString getName() const;
private:
@ -54,4 +55,6 @@ private:
}
#include "storage.hpp"
#endif // CORE_STORAGE_H

View file

@ -15,16 +15,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CORE_STORAGE_HPP
#define CORE_STORAGE_HPP
#include <QStandardPaths>
#include <QDir>
#include "storage.h"
#include <shared/clientinfo.h>
template class Core::Storage<Shared::ClientInfo>;
template <class T>
Core::Storage<T>::Storage(const QString& p_name):
name(p_name),
@ -202,3 +200,9 @@ void Core::Storage<T>::removeRecord(const QString& key)
mdb_txn_commit(txn);
}
}
template <class T>
QString Core::Storage<T>::getName() const {
return name;}
#endif //CORE_STORAGE_HPP