2022-08-19 21:28:59 +00:00
|
|
|
// Squawk messenger.
|
|
|
|
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-08-22 20:29:43 +00:00
|
|
|
#ifndef CORE_CACHE_HPP
|
|
|
|
#define CORE_CACHE_HPP
|
2022-08-19 21:28:59 +00:00
|
|
|
#include "cache.h"
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template <class K, class V>
|
|
|
|
Core::Cache<K, V>::Cache(const QString& name):
|
2022-08-19 21:28:59 +00:00
|
|
|
storage(name),
|
2022-09-03 11:39:42 +00:00
|
|
|
cache(new std::map<K, V> ()),
|
|
|
|
abscent(new std::set<K> ()) {}
|
2022-08-19 21:28:59 +00:00
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template <class K, class V>
|
|
|
|
Core::Cache<K, V>::~Cache() {
|
2022-08-19 21:28:59 +00:00
|
|
|
close();
|
|
|
|
delete cache;
|
2022-08-22 20:29:43 +00:00
|
|
|
delete abscent;
|
2022-08-19 21:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template <class K, class V>
|
|
|
|
void Core::Cache<K, V>::open() {
|
2022-08-19 21:28:59 +00:00
|
|
|
storage.open();}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template <class K, class V>
|
|
|
|
void Core::Cache<K, V>::close() {
|
2022-08-19 21:28:59 +00:00
|
|
|
storage.close();}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template <class K, class V>
|
|
|
|
void Core::Cache<K, V>::addRecord(const K& key, const V& value) {
|
2022-08-19 21:28:59 +00:00
|
|
|
storage.addRecord(key, value);
|
|
|
|
cache->insert(std::make_pair(key, value));
|
2022-08-22 20:29:43 +00:00
|
|
|
abscent->erase(key);
|
2022-08-19 21:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template <class K, class V>
|
|
|
|
V Core::Cache<K, V>::getRecord(const K& key) const {
|
|
|
|
typename std::map<K, V>::const_iterator itr = cache->find(key);
|
2022-08-19 21:28:59 +00:00
|
|
|
if (itr == cache->end()) {
|
2022-08-22 20:29:43 +00:00
|
|
|
if (abscent->count(key) > 0) {
|
2022-09-03 11:39:42 +00:00
|
|
|
throw Archive::NotFound(std::to_string(key), storage.getName().toStdString());
|
2022-08-22 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-09-03 11:39:42 +00:00
|
|
|
V value = storage.getRecord(key);
|
2022-08-22 20:29:43 +00:00
|
|
|
itr = cache->insert(std::make_pair(key, value)).first;
|
|
|
|
} catch (const Archive::NotFound& error) {
|
|
|
|
abscent->insert(key);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template<class K, class V>
|
|
|
|
bool Core::Cache<K, V>::checkRecord(const K& key) const {
|
|
|
|
typename std::map<K, V>::const_iterator itr = cache->find(key);
|
2022-08-22 20:29:43 +00:00
|
|
|
if (itr != cache->end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (abscent->count(key) > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
2022-09-03 11:39:42 +00:00
|
|
|
V value = storage.getRecord(key);
|
2022-08-19 21:28:59 +00:00
|
|
|
itr = cache->insert(std::make_pair(key, value)).first;
|
2022-08-22 20:29:43 +00:00
|
|
|
} catch (const Archive::NotFound& error) {
|
|
|
|
return false;
|
2022-08-19 21:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 20:29:43 +00:00
|
|
|
return true;
|
2022-08-19 21:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template<class K, class V>
|
|
|
|
void Core::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
2022-08-22 20:29:43 +00:00
|
|
|
storage.changeRecord(key, value); //there is a non straightforward behaviour: if there was no element at the sorage it will be added
|
2022-08-19 21:28:59 +00:00
|
|
|
cache->at(key) = value;
|
2022-08-22 20:29:43 +00:00
|
|
|
abscent->erase(key); //so... this line here is to make it coherent with the storage
|
2022-08-19 21:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 11:39:42 +00:00
|
|
|
template<class K, class V>
|
|
|
|
void Core::Cache<K, V>::removeRecord(const K& key) {
|
2022-08-19 21:28:59 +00:00
|
|
|
storage.removeRecord(key);
|
|
|
|
cache->erase(key);
|
2022-08-22 20:29:43 +00:00
|
|
|
abscent->insert(key);
|
2022-08-19 21:28:59 +00:00
|
|
|
}
|
2022-08-22 20:29:43 +00:00
|
|
|
|
|
|
|
#endif //CORE_CACHE_HPP
|