forked from blue/lmdbal
115 lines
3.6 KiB
C++
115 lines
3.6 KiB
C++
|
// 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),
|
||
|
mode(Mode::nothing),
|
||
|
cache(new std::map<K, V>()),
|
||
|
abscent(new std::set<K>())
|
||
|
{
|
||
|
}
|
||
|
|
||
|
template<class K, class V>
|
||
|
DataBase::Cache<K, V>::~Cache() {
|
||
|
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));
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
typename std::map<K, V>::iterator itr = cache->find(key);
|
||
|
if (itr == cache->end() || 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);
|
||
|
itr->second = value;
|
||
|
} catch (const NotFound& error) {
|
||
|
abscent->insert(key);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
if (abscent->count(key) == 0) {
|
||
|
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));
|
||
|
return value;
|
||
|
} catch (const NotFound& error) {
|
||
|
abscent->insert(key);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
cache->erase(key);
|
||
|
Table<K, V>::removeRecord(key);
|
||
|
}
|
||
|
|
||
|
#endif //DATABASE_CACHE_HPP
|