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: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 229 additions and 104 deletions

View file

@ -45,18 +45,16 @@
template<class K, class V>
LMDBAL::Storage<K, V>::Storage(const std::string& _name, Base* parent):
iStorage(_name, parent),
keySerializer(new Serializer<K>()),
valueSerializer(new Serializer<V>())
keySerializer(),
valueSerializer(),
cursors()
{}
/**
* \brief Destroys a storage
*/
template<class K, class V>
LMDBAL::Storage<K, V>::~Storage() {
delete valueSerializer;
delete keySerializer;
}
LMDBAL::Storage<K, V>::~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) {
ensureOpened(addRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData = valueSerializer->setData(value);
MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData = valueSerializer.setData(value);
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc != MDB_SUCCESS)
@ -158,7 +156,7 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
ensureOpened(forceRecordMethodName);
bool added;
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbKey = keySerializer.setData(key);
MDB_val 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);
}
lmdbData = valueSerializer->setData(value);
lmdbData = valueSerializer.setData(value);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
@ -234,12 +232,12 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, Transacti
if (rc != MDB_SUCCESS)
throwUnknown(rc);
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbKey = keySerializer.setData(key);
rc = mdb_cursor_get(cursor, &lmdbKey, nullptr, MDB_SET);
if (rc != MDB_SUCCESS)
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);
mdb_cursor_close(cursor);
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 {
ensureOpened(getRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
if (rc != MDB_SUCCESS)
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 {
ensureOpened(checkRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbKey = keySerializer.setData(key);
MDB_val 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);
while (rc == MDB_SUCCESS) {
K key;
keySerializer->deserialize(lmdbKey, key);
keySerializer.deserialize(lmdbKey, key);
V& value = result[key];
valueSerializer->deserialize(lmdbData, value);
valueSerializer.deserialize(lmdbData, value);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT);
}
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;
for (const std::pair<const K, V>& pair : data) {
lmdbKey = keySerializer->setData(pair.first);
lmdbData = valueSerializer->setData(pair.second);
lmdbKey = keySerializer.setData(pair.first);
lmdbData = valueSerializer.setData(pair.second);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here?
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;
int rc;
for (const std::pair<const K, V>& pair : data) {
lmdbKey = keySerializer->setData(pair.first);
lmdbData = valueSerializer->setData(pair.second);
lmdbKey = keySerializer.setData(pair.first);
lmdbData = valueSerializer.setData(pair.second);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE);
if (rc == MDB_KEYEXIST)
@ -675,7 +673,7 @@ template<class K, class V>
void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) {
ensureOpened(removeRecordMethodName);
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbKey = keySerializer.setData(key);
int rc = mdb_del(txn, dbi, &lmdbKey, NULL);
if (rc != MDB_SUCCESS)
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
*/
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);
}
/**
* \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
*