1
0
forked from blue/lmdbal

some thoughts about cursors, nothing special yet

This commit is contained in:
Blue 2023-08-07 18:27:44 -03:00
parent 69bf1fcc3d
commit 5fba60f7f0
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
4 changed files with 144 additions and 11 deletions

View File

@ -19,6 +19,9 @@
#ifndef LMDBAL_CURSOR_H #ifndef LMDBAL_CURSOR_H
#define LMDBAL_CURSOR_H #define LMDBAL_CURSOR_H
#include <string>
#include "lmdb.h"
#include "base.h" #include "base.h"
#include "storage.h" #include "storage.h"
@ -28,18 +31,45 @@ template <class K, class V>
class Cursor { class Cursor {
friend class Storage<K, V>; friend class Storage<K, V>;
private: private:
enum State { /**<Cursor state:*/
closed, /**< - closed*/
openedPublic, /**< - opened with public transaction, all storages will be notified about it after it's done*/
openedPrivate /**< - opened with private transaction, only current storage will be notified when cursor is closed*/
};
Cursor(Storage<K, V>* parent); Cursor(Storage<K, V>* parent);
~Cursor(); ~Cursor();
public: public:
void open(); void open() const;
void close(); void open(TransactionID txn) const;
void renew() const;
void renew(TransactionID txn) const;
void close() const;
std::pair<K, V> first() const;
std::pair<K, V> last() const;
std::pair<K, V> next() const;
std::pair<K, V> prev() const;
std::pair<K, V> current() const;
void first(std::pair<K, V>& out) const;
void last(std::pair<K, V>& out) const;
void next(std::pair<K, V>& out) const;
void prev(std::pair<K, V>& out) const;
void current(std::pair<K, V>& out) const;
private: private:
void terminated(); void terminated() const;
private: private:
Storage<K, V>* storage; Storage<K, V>* storage;
mutable MDB_cursor* cursor;
mutable State state;
inline static const std::string openRecordMethodName = "Cursor::open"; /**<\brief member function name, just for exceptions*/
inline static const std::string closeRecordMethodName = "Cursor::close"; /**<\brief member function name, just for exceptions*/
inline static const std::string renewRecordMethodName = "Cursor::renew"; /**<\brief member function name, just for exceptions*/
}; };
}; };

View File

@ -23,19 +23,122 @@
template<class K, class V> template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent): LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
storage(parent) storage(parent),
cursor(nullptr),
state(closed)
{ {
} }
template<class K, class V> template<class K, class V>
LMDBAL::Cursor<K, V>::~Cursor () { LMDBAL::Cursor<K, V>::~Cursor () {
close();
} }
template<class K, class V> template<class K, class V>
void LMDBAL::Cursor<K, V>::terminated () { void LMDBAL::Cursor<K, V>::terminated () const {
close(); //for now it's the same, but if I ever going to make writable cursor - here is where it's gonna be different
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::open () const {
storage->ensureOpened(openRecordMethodName);
switch (state) {
case closed: {
TransactionID txn = storage->beginReadOnlyTransaction();
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn);
storage->transactionStarted(txn, true);
state = openedPrivate;
} break;
default:
break;
}
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
storage->ensureOpened(openRecordMethodName);
switch (state) {
case closed: {
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result);
state = openedPublic;
} break;
default:
break;
}
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew () const {
storage->ensureOpened(openRecordMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
storage->abortTransaction(txn);
storage->transactionAborted(txn);
[[fallthrough]];
}
case openedPublic: {
TransactionID txn = storage->beginReadOnlyTransaction();
int result = mdb_cursor_renew(txn, cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn);
storage->transactionStarted(txn, true);
state = openedPrivate;
} break;
default:
break;
}
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
storage->ensureOpened(openRecordMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
storage->abortTransaction(txn);
storage->transactionAborted(txn);
[[fallthrough]];
}
case openedPublic: {
int result = mdb_cursor_renew(txn, cursor);
if (result != MDB_SUCCESS)
storage->throwUnknown(result);
state = openedPublic;
} break;
default:
break;
}
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::close () const {
switch (state) {
case openedPublic: {
mdb_cursor_close(cursor);
state = closed;
} break;
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
mdb_cursor_close(cursor);
storage->abortTransaction(txn);
storage->transactionAborted(txn);
state = closed;
} break;
default:
break;
}
} }
#endif //LMDBAL_CURSOR_HPP #endif //LMDBAL_CURSOR_HPP

View File

@ -91,9 +91,6 @@ 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;
@ -133,7 +130,7 @@ public:
protected: protected:
mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/ 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*/ 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*/ std::set<Cursor<K, V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
int open(MDB_txn* transaction) override; int open(MDB_txn* transaction) override;
void close() override; void close() override;

View File

@ -54,7 +54,10 @@ LMDBAL::Storage<K, V>::Storage(const std::string& _name, Base* parent):
* \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() {
for (Cursor<K, V>* cursor : cursors)
delete cursor;
}
/** /**
* \brief Adds a key-value record to the storage * \brief Adds a key-value record to the storage