1
0
Fork 0
forked from blue/lmdbal

big refactoring part 1

This commit is contained in:
Blue 2023-03-20 18:37:13 +03:00
parent 6a8f67ac34
commit 19229f6c26
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
28 changed files with 867 additions and 795 deletions

174
cache.hpp
View file

@ -14,15 +14,15 @@
// 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
#ifndef LMDBDATABASE_CACHE_HPP
#define LMDBDATABASE_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),
LMDBDataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
Storage<K, V>(p_name, parent),
mode(new Mode),
cache(new std::map<K, V>()),
abscent(new std::set<K>()),
@ -33,7 +33,7 @@ DataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
}
template<class K, class V>
DataBase::Cache<K, V>::~Cache() {
LMDBDataBase::Cache<K, V>::~Cache() {
delete sizeDifference;
delete mode;
delete cache;
@ -41,73 +41,62 @@ DataBase::Cache<K, V>::~Cache() {
}
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);
}
void LMDBDataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::addRecordMethodName);
if (cache->count(key) > 0) {
throw Exist(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
}
if (cache->count(key) > 0)
StorageBase::throwDuplicate(StorageBase::toString(key));
Table<K, V>::addRecord(key, value);
Storage<K, V>::addRecord(key, value);
cache->insert(std::make_pair(key, value));
if (*mode != Mode::full) {
if (*mode != Mode::full)
abscent->erase(key);
}
}
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 LMDBDataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::forceRecordMethodName);
bool added = Table<K, V>::forceRecord(key, value);
bool added = Storage<K, V>::forceRecord(key, value);
if (*mode == Mode::full) {
(*cache)[key] = value;
} else {
if (added) {
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) {
if (!result.second)
result.first->second = value;
} else if (!added) { //this way database had value but cache didn't, so, need to decrease sizeDifference
else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference
handleMode();
}
}
return added;
}
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);
}
void LMDBDataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::changeRecordMethodName);
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);
}
Table<K, V>::changeRecord(key, value);
if (itr == cache->end())
StorageBase::throwNotFound(StorageBase::toString(key));
Storage<K, V>::changeRecord(key, value);
itr->second = value;
} else {
if (abscent->count(key) > 0) {
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
}
if (abscent->count(key) > 0)
StorageBase::throwNotFound(StorageBase::toString(key));
try {
Table<K, V>::changeRecord(key, value);
Storage<K, V>::changeRecord(key, value);
typename std::pair<typename std::map<K, V>::iterator, bool> res = cache->insert(std::make_pair(key, value));
if (!res.second) {
if (!res.second)
res.first->second = value;
} else {
else
handleMode();
}
} catch (const NotFound& error) {
abscent->insert(key);
throw error;
@ -116,70 +105,60 @@ void DataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
}
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);
}
V LMDBDataBase::Cache<K, V>::getRecord(const K& key) const {
StorageBase::ensureOpened(StorageBase::getRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end()) {
if (itr != cache->end())
return itr->second;
}
if (*mode == Mode::full || abscent->count(key) != 0) {
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
}
if (*mode == Mode::full || abscent->count(key) != 0)
StorageBase::throwNotFound(StorageBase::toString(key));
try {
V value = Table<K, V>::getRecord(key);
V value = Storage<K, V>::getRecord(key);
cache->insert(std::make_pair(key, value));
handleMode();
return value;
} catch (const NotFound& error) {
if (*mode != Mode::full) {
if (*mode != Mode::full)
abscent->insert(key);
}
throw error;
}
}
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);
}
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);
if (itr != cache->end()) {
if (itr != cache->end())
return true;
}
if (*mode == Mode::full || abscent->count(key) != 0) {
if (*mode == Mode::full || abscent->count(key) != 0)
return false;
}
try {
V value = Table<K, V>::getRecord(key);
V value = Storage<K, V>::getRecord(key);
cache->insert(std::make_pair(key, value));
handleMode();
return true;
} catch (const NotFound& error) {
if (*mode != Mode::full) {
if (*mode != Mode::full)
abscent->insert(key);
}
return false;
}
}
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);
}
std::map<K, V> LMDBDataBase::Cache<K, V>::readAll() const {
StorageBase::ensureOpened(StorageBase::readAllMethodName);
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
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;
}
@ -188,27 +167,32 @@ std::map<K, V> DataBase::Cache<K, V>::readAll() const {
}
template<class K, class V>
void DataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
DataBase::Table<K, V>::replaceAll(data);
void LMDBDataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
Storage<K, V>::replaceAll(data);
*cache = data;
if (*mode != Mode::full) {
*mode = Mode::full;
abscent->clear();
*sizeDifference = 0;
}
}
template<class K, class V>
uint32_t DataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
uint32_t newSize = DataBase::Table<K, V>::addRecords(data, overwrite);
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;
if (m == Mode::nothing) {
if (m == Mode::nothing)
m = Mode::size;
}
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) {
if (overwrite) {
if (overwrite)
res.first->second = pair.second;
}
} else if (m != Mode::full) {
a.erase(pair.first);
}
@ -226,31 +210,27 @@ uint32_t DataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool over
}
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);
}
void LMDBDataBase::Cache<K, V>::removeRecord(const K& key) {
StorageBase::ensureOpened(StorageBase::removeRecordMethodName);
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);
}
if (!pair.second)
StorageBase::throwNotFound(StorageBase::toString(key));
Table<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
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();
}
if (*mode != Mode::full) {
if (*mode != Mode::full)
abscent->insert(key);
}
}
template<class K, class V>
uint32_t DataBase::Cache<K, V>::count() const {
uint32_t LMDBDataBase::Cache<K, V>::count() const {
switch (*mode) {
case Mode::nothing:
{
uint32_t sz = DataBase::Table<K, V>::count();
uint32_t sz = Storage<K, V>::count();
*sizeDifference = sz - cache->size();
if (sz == 0) {
*mode = Mode::full;
@ -264,11 +244,13 @@ uint32_t DataBase::Cache<K, V>::count() const {
return cache->size() + *sizeDifference;
case Mode::full:
return cache->size();
default:
return 0; //unreachable, no such state, just to suppress the waring
}
}
template<class K, class V>
void DataBase::Cache<K, V>::handleMode() const {
void LMDBDataBase::Cache<K, V>::handleMode() const {
if (*mode == Mode::size) {
--(*sizeDifference);
if (*sizeDifference == 0) {
@ -279,8 +261,8 @@ void DataBase::Cache<K, V>::handleMode() const {
}
template<class K, class V>
int DataBase::Cache<K, V>::drop(MDB_txn * transaction) {
int res = DataBase::Table<K, V>::drop(transaction);
int LMDBDataBase::Cache<K, V>::drop(MDB_txn * transaction) {
int res = Storage<K, V>::drop(transaction);
cache->clear();
abscent->clear();
*mode = Mode::full;
@ -288,4 +270,4 @@ int DataBase::Cache<K, V>::drop(MDB_txn * transaction) {
return res;
}
#endif //DATABASE_CACHE_HPP
#endif //LMDBDATABASE_CACHE_HPP