1
0
Fork 0
forked from blue/lmdbal

replaced some pointers to mutables, some first thoughts about cursors

This commit is contained in:
Blue 2023-08-05 17:13:43 -03:00
parent 4975721a5c
commit 69bf1fcc3d
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 229 additions and 104 deletions

View file

@ -45,15 +45,11 @@
template<class K, class V>
LMDBAL::Cache<K, V>::Cache(const std::string& _name, Base* parent):
Storage<K, V>(_name, parent),
mode(new Mode),
mode(Mode::nothing),
cache(new std::map<K, V>()),
abscent(new std::set<K>()),
sizeDifference(new uint32_t),
transactionCache(new TransactionCache)
{
*mode = Mode::nothing;
*sizeDifference = 0;
}
sizeDifference(0),
transactionCache(new TransactionCache) {}
/**
* \brief Destroys a cache
@ -61,8 +57,6 @@ LMDBAL::Cache<K, V>::Cache(const std::string& _name, Base* parent):
template<class K, class V>
LMDBAL::Cache<K, V>::~Cache() {
delete transactionCache;
delete sizeDifference;
delete mode;
delete cache;
delete abscent;
}
@ -98,7 +92,7 @@ template<class K, class V>
void LMDBAL::Cache<K, V>::handleAddRecord(const K& key, const V& value) {
cache->insert(std::make_pair(key, value));
if (*mode != Mode::full)
if (mode != Mode::full)
abscent->erase(key);
}
@ -129,7 +123,7 @@ bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value, TransactionI
template<class K, class V>
void LMDBAL::Cache<K, V>::handleForceRecord(const K& key, const V& value, bool added) {
if (*mode == Mode::full) {
if (mode == Mode::full) {
(*cache)[key] = value;
} else {
if (added)
@ -148,7 +142,7 @@ template<class K, class V>
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::changeRecordMethodName);
if (*mode == Mode::full) {
if (mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key));
@ -178,7 +172,7 @@ template<class K, class V>
void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
iStorage::ensureOpened(iStorage::changeRecordMethodName);
if (*mode == Mode::full) {
if (mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key));
@ -205,7 +199,7 @@ void LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value, Transaction
template<class K, class V>
void LMDBAL::Cache<K, V>::handleChangeRecord(const K& key, const V& value) {
if (*mode == Mode::full) {
if (mode == Mode::full) {
cache->at(key) = value;
} else {
typename std::pair<typename std::map<K, V>::iterator, bool> res =
@ -236,7 +230,7 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
return;
}
if (*mode == Mode::full || abscent->count(key) != 0)
if (mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
@ -245,7 +239,7 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out) const {
handleMode();
return;
} catch (const NotFound& error) {
if (*mode != Mode::full)
if (mode != Mode::full)
abscent->insert(key);
throw error;
@ -338,7 +332,7 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) con
return;
}
if (*mode == Mode::full || abscent->count(key) != 0)
if (mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
@ -349,7 +343,7 @@ void LMDBAL::Cache<K, V>::getRecord(const K& key, V& out, TransactionID txn) con
}
return;
} catch (const NotFound& error) {
if (! currentTransaction && *mode != Mode::full)
if (!currentTransaction && mode != Mode::full)
abscent->insert(key);
throw error;
@ -364,7 +358,7 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
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 {
@ -373,7 +367,7 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
handleMode();
return true;
} catch (const NotFound& error) {
if (*mode != Mode::full)
if (mode != Mode::full)
abscent->insert(key);
return false;
@ -439,7 +433,7 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
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 {
@ -450,7 +444,7 @@ bool LMDBAL::Cache<K, V>::checkRecord(const K& key, TransactionID txn) const {
}
return true;
} catch (const NotFound& error) {
if (!currentTransaction && *mode != Mode::full)
if (!currentTransaction && mode != Mode::full)
abscent->insert(key);
return false;
@ -461,11 +455,11 @@ template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll() const {
iStorage::ensureOpened(iStorage::readAllMethodName);
if (*mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values
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;
sizeDifference = 0;
}
return *cache;
@ -475,12 +469,12 @@ template<class K, class V>
void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out) const {
iStorage::ensureOpened(iStorage::readAllMethodName);
if (*mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
Storage<K, V>::readAll(out); //that are missing in the cache
*cache = out;
abscent->clear();
*sizeDifference = 0;
sizeDifference = 0;
}
}
@ -501,7 +495,7 @@ void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const
typename TransactionCache::iterator tc = transactionCache->find(txn);
if (tc != transactionCache->end()) {
Queue& queue = tc->second;
if (*mode != Mode::full) {
if (mode != Mode::full) {
out = *cache;
for (typename Queue::const_iterator i = queue.begin(), end = queue.end(); i != end; ++i) {
@ -552,12 +546,12 @@ void LMDBAL::Cache<K, V>::readAll(std::map<K, V>& out, TransactionID txn) const
queue.emplace_back(Operation::replace, new std::map<K, V>(out)); //I can as well erase all previous cache entries
}
} else {
if (*mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values
if (mode != Mode::full) { //there is a room for optimization
mode = Mode::full; //I can read and deserialize only those values
Storage<K, V>::readAll(out); //that are missing in the cache
*cache = out;
abscent->clear();
*sizeDifference = 0;
sizeDifference = 0;
}
}
}
@ -567,10 +561,10 @@ void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
Storage<K, V>::replaceAll(data);
*cache = data;
if (*mode != Mode::full) {
*mode = Mode::full;
if (mode != Mode::full) {
mode = Mode::full;
abscent->clear();
*sizeDifference = 0;
sizeDifference = 0;
}
}
@ -591,10 +585,10 @@ void LMDBAL::Cache<K, V>::handleReplaceAll(std::map<K, V>* data) {
delete cache;
cache = data;
if (*mode != Mode::full) {
*mode = Mode::full;
if (mode != Mode::full) {
mode = Mode::full;
abscent->clear();
*sizeDifference = 0;
sizeDifference = 0;
}
}
@ -622,9 +616,8 @@ LMDBAL::SizeType LMDBAL::Cache<K, V>::addRecords(const std::map<K, V>& data, Tra
template<class K, class V>
void LMDBAL::Cache<K, V>::handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize) {
Mode& m = *mode;
if (m == Mode::nothing)
m = Mode::size;
if (mode == Mode::nothing)
mode = Mode::size;
std::map<K, V>& c = *cache;
std::set<K>& a = *abscent;
@ -633,15 +626,15 @@ void LMDBAL::Cache<K, V>::handleAddRecords(const std::map<K, V>& data, bool over
if (!res.second) {
if (overwrite)
res.first->second = pair.second;
} else if (m != Mode::full) {
} else if (mode != Mode::full) {
a.erase(pair.first);
}
}
if (m != Mode::full) {
*sizeDifference = newSize - c.size();
if (*sizeDifference == 0) {
*mode = Mode::full;
if (mode != Mode::full) {
sizeDifference = newSize - c.size();
if (sizeDifference == 0) {
mode = Mode::full;
abscent->clear();
}
}
@ -652,7 +645,7 @@ void LMDBAL::Cache<K, V>::removeRecord(const K& key) {
iStorage::ensureOpened(iStorage::removeRecordMethodName);
bool noKey = false;
if (*mode != Mode::full)
if (mode != Mode::full)
noKey = cache->count(key) == 0;
else
noKey = abscent->count(key) > 0;
@ -669,7 +662,7 @@ void LMDBAL::Cache<K, V>::removeRecord(const K& key, TransactionID txn) {
iStorage::ensureOpened(iStorage::removeRecordMethodName);
bool noKey = false;
if (*mode != Mode::full)
if (mode != Mode::full)
noKey = cache->count(key) == 0;
else
noKey = abscent->count(key) > 0;
@ -689,27 +682,27 @@ void LMDBAL::Cache<K, V>::handleRemoveRecord(const K& 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 LMDBAL::Cache<K, V>::count() const {
switch (*mode) {
switch (mode) {
case Mode::nothing:
{
uint32_t sz = Storage<K, V>::count();
*sizeDifference = sz - cache->size();
sizeDifference = sz - cache->size();
if (sz == 0) {
*mode = Mode::full;
mode = Mode::full;
abscent->clear();
} else {
*mode = Mode::size;
mode = Mode::size;
}
return sz;
}
case Mode::size:
return cache->size() + *sizeDifference;
return cache->size() + sizeDifference;
case Mode::full:
return cache->size();
default:
@ -755,22 +748,22 @@ uint32_t LMDBAL::Cache<K, V>::count(TransactionID txn) const {
}
}
switch (*mode) {
switch (mode) {
case Mode::nothing: {
uint32_t sz = Storage<K, V>::count(txn);
if (!currentTransaction) {
*sizeDifference = sz - cache->size();
sizeDifference = sz - cache->size();
if (sz == 0) {
*mode = Mode::full;
mode = Mode::full;
abscent->clear();
} else {
*mode = Mode::size;
mode = Mode::size;
}
}
return sz;
}
case Mode::size:
return cache->size() + *sizeDifference + diff;
return cache->size() + sizeDifference + diff;
case Mode::full:
return cache->size() + diff;
default:
@ -780,10 +773,10 @@ uint32_t LMDBAL::Cache<K, V>::count(TransactionID txn) const {
template<class K, class V>
void LMDBAL::Cache<K, V>::handleMode() const {
if (*mode == Mode::size) {
--(*sizeDifference);
if (*sizeDifference == 0) {
*mode = Mode::full;
if (mode == Mode::size) {
--sizeDifference;
if (sizeDifference == 0) {
mode = Mode::full;
abscent->clear();
}
}
@ -804,8 +797,8 @@ template<class K, class V>
void LMDBAL::Cache<K, V>::handleDrop() {
cache->clear();
abscent->clear();
*mode = Mode::full;
*sizeDifference = 0;
mode = Mode::full;
sizeDifference = 0;
}
template<class K, class V>