1
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

@ -9,6 +9,8 @@ set(HEADERS
exceptions.h exceptions.h
storage.h storage.h
storage.hpp storage.hpp
cursor.h
cursor.hpp
cache.h cache.h
cache.hpp cache.hpp
serializer.h serializer.h

View File

@ -67,13 +67,12 @@ LMDBAL::Base::~Base() {
*/ */
void LMDBAL::Base::close() { void LMDBAL::Base::close() {
if (opened) { if (opened) {
for (LMDBAL::TransactionID id : *transactions) for (const LMDBAL::TransactionID id : *transactions)
abortTransaction(id, emptyName); abortTransaction(id, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages) { for (const std::pair<const std::string, iStorage*>& pair : storages)
iStorage* storage = pair.second; pair.second->close();
mdb_dbi_close(environment, storage->dbi);
}
mdb_env_close(environment); mdb_env_close(environment);
transactions->clear(); transactions->clear();
opened = false; opened = false;
@ -101,7 +100,7 @@ void LMDBAL::Base::open() {
TransactionID txn = beginPrivateTransaction(emptyName); TransactionID txn = beginPrivateTransaction(emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages) { for (const std::pair<const std::string, iStorage*>& pair : storages) {
iStorage* storage = pair.second; iStorage* storage = pair.second;
int rc = storage->createStorage(txn); int rc = storage->open(txn);
if (rc) if (rc)
throw Unknown(name, mdb_strerror(rc)); throw Unknown(name, mdb_strerror(rc));
} }

View File

@ -105,15 +105,13 @@ protected:
* \brief Cache mode * \brief Cache mode
* *
* Sometimes we have a complete information about the content of the database, * Sometimes we have a complete information about the content of the database,
* and we don't even need to refer to lmdb to fetch or check for data. * and we don't even need to call lmdb to fetch or check for data.
* This member actually shows what is the current state, more about them you can read in Enum descriptions * This member actually shows what is the current state, more about them you can read in Enum descriptions
*
* It's done with pointer to comply some of the const method limitations which would modify this state eventually
*/ */
Mode* mode; mutable Mode mode;
std::map<K, V>* cache; /**<\brief Cached data*/ std::map<K, V>* cache; /**<\brief Cached data*/
std::set<K>* abscent; /**<\brief Set of keys that are definitely not in the cache*/ std::set<K>* abscent; /**<\brief Set of keys that are definitely not in the cache*/
SizeType* sizeDifference; /**<\brief Difference of size between cached data and amount of records in the lmdb storage*/ mutable SizeType sizeDifference; /**<\brief Difference of size between cached data and amount of records in the lmdb storage*/
TransactionCache* transactionCache; /**<\brief All changes made under under uncommited transactions*/ TransactionCache* transactionCache; /**<\brief All changes made under under uncommited transactions*/
}; };

View File

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

50
src/cursor.h Normal file
View File

@ -0,0 +1,50 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 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 LMDBAL_CURSOR_H
#define LMDBAL_CURSOR_H
#include "base.h"
#include "storage.h"
namespace LMDBAL {
template <class K, class V>
class Cursor {
friend class Storage<K, V>;
private:
Cursor(Storage<K, V>* parent);
~Cursor();
public:
void open();
void close();
private:
void terminated();
private:
Storage<K, V>* storage;
};
};
#include "cursor.hpp"
#endif //LMDBAL_CURSOR_H

41
src/cursor.hpp Normal file
View File

@ -0,0 +1,41 @@
/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 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 LMDBAL_CURSOR_HPP
#define LMDBAL_CURSOR_HPP
#include "cursor.h"
template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
storage(parent)
{
}
template<class K, class V>
LMDBAL::Cursor<K, V>::~Cursor () {
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::terminated () {
}
#endif //LMDBAL_CURSOR_HPP

View File

@ -42,6 +42,14 @@ LMDBAL::iStorage::iStorage(const std::string& p_name, Base* parent):
*/ */
LMDBAL::iStorage::~iStorage() {} LMDBAL::iStorage::~iStorage() {}
/**
* \brief A private virtual function I need to close each storage in the database
*/
void LMDBAL::iStorage::close() {
mdb_dbi_close(db->environment, dbi);
}
/** /**
* \brief Drops content of a storage interface * \brief Drops content of a storage interface
* *

View File

@ -21,6 +21,7 @@
#include "base.h" #include "base.h"
#include "serializer.h" #include "serializer.h"
#include "cursor.h"
namespace LMDBAL { namespace LMDBAL {
@ -34,7 +35,8 @@ protected:
iStorage(const std::string& name, Base* parent); iStorage(const std::string& name, Base* parent);
virtual ~iStorage(); virtual ~iStorage();
virtual int createStorage(MDB_txn * transaction) = 0; virtual int open(MDB_txn * transaction) = 0;
virtual void close();
bool isDBOpened() const; bool isDBOpened() const;
const std::string& dbName() const; const std::string& dbName() const;
@ -89,9 +91,13 @@ protected:
static std::string toString(const T& value); static std::string toString(const T& value);
}; };
// template <class K, class V>
// class Cursor;
template <class K, class V> template <class K, class V>
class Storage : public iStorage { class Storage : public iStorage {
friend class Base; friend class Base;
friend class Cursor<K, V>;
protected: protected:
Storage(const std::string& name, Base* parent); Storage(const std::string& name, Base* parent);
~Storage() override; ~Storage() override;
@ -121,11 +127,16 @@ public:
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false); virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false); virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
protected: Cursor<K, V>* createCursor();
Serializer<K>* keySerializer; /**<\brief internal object that would serialize and deserialize keys*/ void destroyCursor(Cursor<K, V>* cursor);
Serializer<V>* valueSerializer; /**<\brief internal object that would serialize and deserialize values*/
int createStorage(MDB_txn* transaction) override; protected:
mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/
mutable Serializer<V> valueSerializer; /**<\brief internal object that would serialize and deserialize values*/
std::set<Cursor<K,V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
int open(MDB_txn* transaction) override;
void close() override;
}; };
} }

View File

@ -45,18 +45,16 @@
template<class K, class V> template<class K, class V>
LMDBAL::Storage<K, V>::Storage(const std::string& _name, Base* parent): LMDBAL::Storage<K, V>::Storage(const std::string& _name, Base* parent):
iStorage(_name, parent), iStorage(_name, parent),
keySerializer(new Serializer<K>()), keySerializer(),
valueSerializer(new Serializer<V>()) valueSerializer(),
cursors()
{} {}
/** /**
* \brief Destroys a storage * \brief Destroys a storage
*/ */
template<class K, class V> template<class K, class V>
LMDBAL::Storage<K, V>::~Storage() { LMDBAL::Storage<K, V>::~Storage() {}
delete valueSerializer;
delete keySerializer;
}
/** /**
* \brief Adds a key-value record to the storage * \brief Adds a key-value record to the storage
@ -104,8 +102,8 @@ template<class K, class V>
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionID txn) { void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
ensureOpened(addRecordMethodName); ensureOpened(addRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData = valueSerializer->setData(value); MDB_val lmdbData = valueSerializer.setData(value);
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
@ -158,7 +156,7 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
ensureOpened(forceRecordMethodName); ensureOpened(forceRecordMethodName);
bool added; bool added;
MDB_val lmdbKey = keySerializer->setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
@ -174,7 +172,7 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
throwUnknown(rc); throwUnknown(rc);
} }
lmdbData = valueSerializer->setData(value); lmdbData = valueSerializer.setData(value);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0); rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
@ -234,12 +232,12 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, Transacti
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
MDB_val lmdbKey = keySerializer->setData(key); MDB_val lmdbKey = keySerializer.setData(key);
rc = mdb_cursor_get(cursor, &lmdbKey, nullptr, MDB_SET); rc = mdb_cursor_get(cursor, &lmdbKey, nullptr, MDB_SET);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key)); throwNotFoundOrUnknown(rc, toString(key));
MDB_val lmdbData = valueSerializer->setData(value); MDB_val lmdbData = valueSerializer.setData(value);
rc = mdb_cursor_put(cursor, &lmdbKey, &lmdbData, MDB_CURRENT); rc = mdb_cursor_put(cursor, &lmdbKey, &lmdbData, MDB_CURRENT);
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
@ -339,14 +337,14 @@ template<class K, class V>
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn) const { void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn) const {
ensureOpened(getRecordMethodName); ensureOpened(getRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key)); throwNotFoundOrUnknown(rc, toString(key));
valueSerializer->deserialize(lmdbData, value); valueSerializer.deserialize(lmdbData, value);
} }
/** /**
@ -392,7 +390,7 @@ template<class K, class V>
bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const { bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const {
ensureOpened(checkRecordMethodName); ensureOpened(checkRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
@ -495,9 +493,9 @@ void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) c
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST); rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
while (rc == MDB_SUCCESS) { while (rc == MDB_SUCCESS) {
K key; K key;
keySerializer->deserialize(lmdbKey, key); keySerializer.deserialize(lmdbKey, key);
V& value = result[key]; V& value = result[key];
valueSerializer->deserialize(lmdbData, value); valueSerializer.deserialize(lmdbData, value);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT); rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT);
} }
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
@ -553,8 +551,8 @@ void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data, TransactionID
MDB_val lmdbKey, lmdbData; MDB_val lmdbKey, lmdbData;
for (const std::pair<const K, V>& pair : data) { for (const std::pair<const K, V>& pair : data) {
lmdbKey = keySerializer->setData(pair.first); lmdbKey = keySerializer.setData(pair.first);
lmdbData = valueSerializer->setData(pair.second); lmdbData = valueSerializer.setData(pair.second);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here? rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here?
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
@ -612,8 +610,8 @@ uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, Transacti
MDB_val lmdbKey, lmdbData; MDB_val lmdbKey, lmdbData;
int rc; int rc;
for (const std::pair<const K, V>& pair : data) { for (const std::pair<const K, V>& pair : data) {
lmdbKey = keySerializer->setData(pair.first); lmdbKey = keySerializer.setData(pair.first);
lmdbData = valueSerializer->setData(pair.second); lmdbData = valueSerializer.setData(pair.second);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE); rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE);
if (rc == MDB_KEYEXIST) if (rc == MDB_KEYEXIST)
@ -675,7 +673,7 @@ template<class K, class V>
void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) { void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) {
ensureOpened(removeRecordMethodName); ensureOpened(removeRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key); MDB_val lmdbKey = keySerializer.setData(key);
int rc = mdb_del(txn, dbi, &lmdbKey, NULL); int rc = mdb_del(txn, dbi, &lmdbKey, NULL);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key)); throwNotFoundOrUnknown(rc, toString(key));
@ -688,10 +686,35 @@ void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) {
* \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code * \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code
*/ */
template<class K, class V> template<class K, class V>
int LMDBAL::Storage<K, V>::createStorage(MDB_txn* transaction) { int LMDBAL::Storage<K, V>::open(MDB_txn* transaction) {
return makeStorage<K>(transaction); return makeStorage<K>(transaction);
} }
/**
* \brief A private virtual function I need to close each storage in the database
*/
template<class K, class V>
void LMDBAL::Storage<K, V>::close() {
for (Cursor<K, V>* cursor : cursors)
cursor->terminated();
iStorage::close();
}
template<class K, class V>
LMDBAL::Cursor<K, V>* LMDBAL::Storage<K, V>::createCursor() {
Cursor<K, V>* cursor = new Cursor<K, V>(this);
cursors.insert(cursor);
return cursor;
}
template<class K, class V>
void LMDBAL::Storage<K, V>::destroyCursor(Cursor<K, V>* cursor) {
cursors.erase(cursor);
delete cursor;
}
/** /**
* \brief A functiion to actually open <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gadbe68a06c448dfb62da16443d251a78b">MDB_dbi</a> storage * \brief A functiion to actually open <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gadbe68a06c448dfb62da16443d251a78b">MDB_dbi</a> storage
* *