2022-09-20 17:16:48 +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/>.
|
|
|
|
|
|
|
|
#ifndef DATABASE_CACHE_HPP
|
|
|
|
#define DATABASE_CACHE_HPP
|
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
DataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
|
|
|
|
DataBase::Table<K, V>(p_name, parent),
|
2022-10-08 16:45:07 +00:00
|
|
|
mode(new Mode),
|
2022-09-20 17:16:48 +00:00
|
|
|
cache(new std::map<K, V>()),
|
2022-10-08 16:45:07 +00:00
|
|
|
abscent(new std::set<K>()),
|
|
|
|
sizeDifference(new uint32_t)
|
2022-09-20 17:16:48 +00:00
|
|
|
{
|
2022-10-08 16:45:07 +00:00
|
|
|
*mode = Mode::nothing;
|
|
|
|
*sizeDifference = 0;
|
2022-09-20 17:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
DataBase::Cache<K, V>::~Cache() {
|
2022-10-08 16:45:07 +00:00
|
|
|
delete sizeDifference;
|
|
|
|
delete mode;
|
2022-09-20 17:16:48 +00:00
|
|
|
delete cache;
|
|
|
|
delete abscent;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void DataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("addRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cache->count(key) > 0) {
|
|
|
|
throw Exist(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Table<K, V>::addRecord(key, value);
|
|
|
|
cache->insert(std::make_pair(key, value));
|
2022-12-13 22:57:49 +00:00
|
|
|
|
|
|
|
if (*mode != Mode::full) {
|
|
|
|
abscent->erase(key);
|
|
|
|
}
|
2022-09-20 17:16:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 22:57:49 +00:00
|
|
|
template<class K, class V>
|
|
|
|
bool DataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("forceRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool added = Table<K, V>::forceRecord(key, value);
|
|
|
|
if (*mode == Mode::full) {
|
|
|
|
(*cache)[key] = value;
|
|
|
|
} else {
|
|
|
|
if (added) {
|
|
|
|
abscent->erase(key);
|
|
|
|
}
|
|
|
|
std::pair<typename std::map<K, V>::iterator, bool> result = cache->insert(std::make_pair(key, value));
|
|
|
|
if (!result.second) {
|
|
|
|
result.first->second = value;
|
|
|
|
} else if (!added) { //this way database had value but cache didn't, so, need to decrease sizeDifference
|
|
|
|
handleMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
2022-09-20 17:16:48 +00:00
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void DataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("changeRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
2022-10-08 16:45:07 +00:00
|
|
|
if (*mode == Mode::full) {
|
|
|
|
typename std::map<K, V>::iterator itr = cache->find(key);
|
|
|
|
if (itr != cache->end()) {
|
|
|
|
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
2022-09-20 17:16:48 +00:00
|
|
|
Table<K, V>::changeRecord(key, value);
|
|
|
|
itr->second = value;
|
2022-10-08 16:45:07 +00:00
|
|
|
} else {
|
|
|
|
if (abscent->count(key) > 0) {
|
|
|
|
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Table<K, V>::changeRecord(key, value);
|
|
|
|
typename std::map<K, V>::iterator itr = cache->find(key);
|
|
|
|
if (itr != cache->end()) {
|
|
|
|
itr->second = value;
|
|
|
|
} else {
|
|
|
|
cache->insert(std::make_pair(key, value));
|
|
|
|
handleMode();
|
|
|
|
}
|
|
|
|
} catch (const NotFound& error) {
|
|
|
|
abscent->insert(key);
|
|
|
|
throw error;
|
|
|
|
}
|
2022-09-20 17:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
V DataBase::Cache<K, V>::getRecord(const K& key) const {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("getRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
typename std::map<K, V>::const_iterator itr = cache->find(key);
|
|
|
|
if (itr != cache->end()) {
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
2022-10-09 14:03:30 +00:00
|
|
|
if (*mode == Mode::full || abscent->count(key) != 0) {
|
2022-09-20 17:16:48 +00:00
|
|
|
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
V value = Table<K, V>::getRecord(key);
|
|
|
|
cache->insert(std::make_pair(key, value));
|
2022-10-08 16:45:07 +00:00
|
|
|
handleMode();
|
2022-09-20 17:16:48 +00:00
|
|
|
return value;
|
|
|
|
} catch (const NotFound& error) {
|
2022-12-15 17:29:49 +00:00
|
|
|
if (*mode != Mode::full) {
|
|
|
|
abscent->insert(key);
|
|
|
|
}
|
2022-09-20 17:16:48 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 17:29:49 +00:00
|
|
|
template<class K, class V>
|
|
|
|
bool DataBase::Cache<K, V>::checkRecord(const K& key) const {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("checkRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
typename std::map<K, V>::const_iterator itr = cache->find(key);
|
|
|
|
if (itr != cache->end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*mode == Mode::full || abscent->count(key) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
V value = Table<K, V>::getRecord(key);
|
|
|
|
cache->insert(std::make_pair(key, value));
|
|
|
|
handleMode();
|
|
|
|
return true;
|
|
|
|
} catch (const NotFound& error) {
|
|
|
|
if (*mode != Mode::full) {
|
|
|
|
abscent->insert(key);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 10:54:34 +00:00
|
|
|
template<class K, class V>
|
|
|
|
std::map<K, V> DataBase::Cache<K, V>::readAll() const {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("readAll", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*mode != Mode::full) { //there is a room for optimization
|
|
|
|
*mode = Mode::full; //I can read and deserialize only those values
|
|
|
|
*cache = DataBase::Table<K, V>::readAll(); //that are missing in the cache
|
|
|
|
abscent->clear();
|
|
|
|
*sizeDifference = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *cache;
|
|
|
|
}
|
|
|
|
|
2022-09-20 17:16:48 +00:00
|
|
|
template<class K, class V>
|
|
|
|
void DataBase::Cache<K, V>::removeRecord(const K& key) {
|
|
|
|
if (!DataBase::Table<K, V>::db->opened) {
|
|
|
|
throw Closed("removeRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
typename std::pair<typename std::set<K>::const_iterator, bool> pair = abscent->insert(key);
|
|
|
|
if (!pair.second) {
|
|
|
|
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Table<K, V>::removeRecord(key);
|
2022-10-08 16:45:07 +00:00
|
|
|
if (cache->erase(key) == 0) { //if it was not cached and we are now in size mode then the sizeDifference would decrease
|
|
|
|
handleMode();
|
|
|
|
}
|
|
|
|
if (*mode != Mode::full) {
|
|
|
|
abscent->insert(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
uint32_t DataBase::Cache<K, V>::count() const {
|
|
|
|
switch (*mode) {
|
|
|
|
case Mode::nothing:
|
|
|
|
{
|
|
|
|
uint32_t sz = DataBase::Table<K, V>::count();
|
|
|
|
*sizeDifference = sz - cache->size();
|
|
|
|
if (sz == 0) {
|
|
|
|
*mode = Mode::full;
|
|
|
|
abscent->clear();
|
|
|
|
} else {
|
|
|
|
*mode = Mode::size;
|
|
|
|
}
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
case Mode::size:
|
|
|
|
return cache->size() + *sizeDifference;
|
|
|
|
case Mode::full:
|
|
|
|
return cache->size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void DataBase::Cache<K, V>::handleMode() const {
|
|
|
|
if (*mode == Mode::size) {
|
|
|
|
--(*sizeDifference);
|
|
|
|
if (*sizeDifference == 0) {
|
|
|
|
*mode = Mode::full;
|
|
|
|
abscent->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
2022-10-09 14:03:30 +00:00
|
|
|
int DataBase::Cache<K, V>::drop(MDB_txn * transaction) {
|
|
|
|
int res = DataBase::Table<K, V>::drop(transaction);
|
2022-10-08 16:45:07 +00:00
|
|
|
cache->clear();
|
|
|
|
abscent->clear();
|
|
|
|
*mode = Mode::full;
|
|
|
|
*sizeDifference = 0;
|
2022-10-09 14:03:30 +00:00
|
|
|
return res;
|
2022-09-20 17:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //DATABASE_CACHE_HPP
|