lmdbal/cache.hpp

274 lines
8.0 KiB
C++
Raw Normal View History

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/>.
2023-03-20 15:37:13 +00:00
#ifndef LMDBDATABASE_CACHE_HPP
#define LMDBDATABASE_CACHE_HPP
2022-09-20 17:16:48 +00:00
#include "cache.h"
#include "exceptions.h"
template<class K, class V>
2023-03-20 15:37:13 +00:00
LMDBDataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
Storage<K, V>(p_name, parent),
mode(new Mode),
2022-09-20 17:16:48 +00:00
cache(new std::map<K, V>()),
abscent(new std::set<K>()),
sizeDifference(new uint32_t)
2022-09-20 17:16:48 +00:00
{
*mode = Mode::nothing;
*sizeDifference = 0;
2022-09-20 17:16:48 +00:00
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
LMDBDataBase::Cache<K, V>::~Cache() {
delete sizeDifference;
delete mode;
2022-09-20 17:16:48 +00:00
delete cache;
delete abscent;
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
void LMDBDataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::addRecordMethodName);
2022-09-20 17:16:48 +00:00
2023-03-20 15:37:13 +00:00
if (cache->count(key) > 0)
StorageBase::throwDuplicate(StorageBase::toString(key));
2022-09-20 17:16:48 +00:00
2023-03-20 15:37:13 +00:00
Storage<K, V>::addRecord(key, value);
2022-09-20 17:16:48 +00:00
cache->insert(std::make_pair(key, value));
2022-12-13 22:57:49 +00:00
2023-03-20 15:37:13 +00:00
if (*mode != Mode::full)
2022-12-13 22:57:49 +00:00
abscent->erase(key);
2022-09-20 17:16:48 +00:00
}
2022-12-13 22:57:49 +00:00
template<class K, class V>
2023-03-20 15:37:13 +00:00
bool LMDBDataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::forceRecordMethodName);
2022-12-13 22:57:49 +00:00
2023-03-20 15:37:13 +00:00
bool added = Storage<K, V>::forceRecord(key, value);
2022-12-13 22:57:49 +00:00
if (*mode == Mode::full) {
(*cache)[key] = value;
} else {
2023-03-20 15:37:13 +00:00
if (added)
2022-12-13 22:57:49 +00:00
abscent->erase(key);
2023-03-20 15:37:13 +00:00
2022-12-13 22:57:49 +00:00
std::pair<typename std::map<K, V>::iterator, bool> result = cache->insert(std::make_pair(key, value));
2023-03-20 15:37:13 +00:00
if (!result.second)
2022-12-13 22:57:49 +00:00
result.first->second = value;
2023-03-20 15:37:13 +00:00
else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference
2022-12-13 22:57:49 +00:00
handleMode();
}
return added;
}
2022-09-20 17:16:48 +00:00
template<class K, class V>
2023-03-20 15:37:13 +00:00
void LMDBDataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::changeRecordMethodName);
2022-09-20 17:16:48 +00:00
if (*mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
2023-03-20 15:37:13 +00:00
if (itr == cache->end())
StorageBase::throwNotFound(StorageBase::toString(key));
Storage<K, V>::changeRecord(key, value);
2022-09-20 17:16:48 +00:00
itr->second = value;
} else {
2023-03-20 15:37:13 +00:00
if (abscent->count(key) > 0)
StorageBase::throwNotFound(StorageBase::toString(key));
try {
2023-03-20 15:37:13 +00:00
Storage<K, V>::changeRecord(key, value);
typename std::pair<typename std::map<K, V>::iterator, bool> res = cache->insert(std::make_pair(key, value));
2023-03-20 15:37:13 +00:00
if (!res.second)
res.first->second = value;
2023-03-20 15:37:13 +00:00
else
handleMode();
} catch (const NotFound& error) {
abscent->insert(key);
throw error;
}
2022-09-20 17:16:48 +00:00
}
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
V LMDBDataBase::Cache<K, V>::getRecord(const K& key) const {
StorageBase::ensureOpened(StorageBase::getRecordMethodName);
2022-09-20 17:16:48 +00:00
typename std::map<K, V>::const_iterator itr = cache->find(key);
2023-03-20 15:37:13 +00:00
if (itr != cache->end())
2022-09-20 17:16:48 +00:00
return itr->second;
2023-03-20 15:37:13 +00:00
if (*mode == Mode::full || abscent->count(key) != 0)
StorageBase::throwNotFound(StorageBase::toString(key));
2022-09-20 17:16:48 +00:00
try {
2023-03-20 15:37:13 +00:00
V value = Storage<K, V>::getRecord(key);
2022-09-20 17:16:48 +00:00
cache->insert(std::make_pair(key, value));
handleMode();
2022-09-20 17:16:48 +00:00
return value;
} catch (const NotFound& error) {
2023-03-20 15:37:13 +00:00
if (*mode != Mode::full)
abscent->insert(key);
2023-03-20 15:37:13 +00:00
2022-09-20 17:16:48 +00:00
throw error;
}
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
bool LMDBDataBase::Cache<K, V>::checkRecord(const K& key) const {
StorageBase::ensureOpened(StorageBase::checkRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
2023-03-20 15:37:13 +00:00
if (itr != cache->end())
return true;
2023-03-20 15:37:13 +00:00
if (*mode == Mode::full || abscent->count(key) != 0)
return false;
try {
2023-03-20 15:37:13 +00:00
V value = Storage<K, V>::getRecord(key);
cache->insert(std::make_pair(key, value));
handleMode();
return true;
} catch (const NotFound& error) {
2023-03-20 15:37:13 +00:00
if (*mode != Mode::full)
abscent->insert(key);
2023-03-20 15:37:13 +00:00
return false;
}
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
std::map<K, V> LMDBDataBase::Cache<K, V>::readAll() const {
StorageBase::ensureOpened(StorageBase::readAllMethodName);
2023-03-20 15:37:13 +00:00
if (*mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values
*cache = Storage<K, V>::readAll(); //that are missing in the cache
abscent->clear();
*sizeDifference = 0;
}
return *cache;
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
void LMDBDataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
Storage<K, V>::replaceAll(data);
*cache = data;
2023-03-20 15:37:13 +00:00
if (*mode != Mode::full) {
*mode = Mode::full;
abscent->clear();
*sizeDifference = 0;
}
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
uint32_t LMDBDataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
uint32_t newSize = Storage<K, V>::addRecords(data, overwrite);
Mode& m = *mode;
2023-03-20 15:37:13 +00:00
if (m == Mode::nothing)
m = Mode::size;
2023-03-20 15:37:13 +00:00
std::map<K, V>& c = *cache;
std::set<K>& a = *abscent;
for (const std::pair<const K, V>& pair : data) {
std::pair<typename std::map<K, V>::iterator, bool> res = c.insert(pair);
if (!res.second) {
2023-03-20 15:37:13 +00:00
if (overwrite)
res.first->second = pair.second;
} else if (m != Mode::full) {
a.erase(pair.first);
}
}
if (m != Mode::full) {
*sizeDifference = newSize - c.size();
if (*sizeDifference == 0) {
*mode = Mode::full;
abscent->clear();
}
}
return newSize;
}
2022-09-20 17:16:48 +00:00
template<class K, class V>
2023-03-20 15:37:13 +00:00
void LMDBDataBase::Cache<K, V>::removeRecord(const K& key) {
StorageBase::ensureOpened(StorageBase::removeRecordMethodName);
2022-09-20 17:16:48 +00:00
typename std::pair<typename std::set<K>::const_iterator, bool> pair = abscent->insert(key);
2023-03-20 15:37:13 +00:00
if (!pair.second)
StorageBase::throwNotFound(StorageBase::toString(key));
2022-09-20 17:16:48 +00:00
2023-03-20 15:37:13 +00:00
Storage<K, V>::removeRecord(key);
if (cache->erase(key) == 0) //if it was not cached and we are now in size mode then the sizeDifference would decrease
handleMode();
2023-03-20 15:37:13 +00:00
if (*mode != Mode::full)
abscent->insert(key);
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
uint32_t LMDBDataBase::Cache<K, V>::count() const {
switch (*mode) {
case Mode::nothing:
{
2023-03-20 15:37:13 +00:00
uint32_t sz = Storage<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();
2023-03-20 15:37:13 +00:00
default:
return 0; //unreachable, no such state, just to suppress the waring
}
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
void LMDBDataBase::Cache<K, V>::handleMode() const {
if (*mode == Mode::size) {
--(*sizeDifference);
if (*sizeDifference == 0) {
*mode = Mode::full;
abscent->clear();
}
}
}
template<class K, class V>
2023-03-20 15:37:13 +00:00
int LMDBDataBase::Cache<K, V>::drop(MDB_txn * transaction) {
int res = Storage<K, V>::drop(transaction);
cache->clear();
abscent->clear();
*mode = Mode::full;
*sizeDifference = 0;
return res;
2022-09-20 17:16:48 +00:00
}
2023-03-20 15:37:13 +00:00
#endif //LMDBDATABASE_CACHE_HPP