diff --git a/src/cursor.h b/src/cursor.h index 7322248..36ea7fa 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -19,6 +19,9 @@ #ifndef LMDBAL_CURSOR_H #define LMDBAL_CURSOR_H +#include + +#include "lmdb.h" #include "base.h" #include "storage.h" @@ -28,18 +31,45 @@ template class Cursor { friend class Storage; private: + enum State { /*** parent); ~Cursor(); public: - void open(); - void close(); + void open() const; + void open(TransactionID txn) const; + void renew() const; + void renew(TransactionID txn) const; + void close() const; + + std::pair first() const; + std::pair last() const; + std::pair next() const; + std::pair prev() const; + std::pair current() const; + + void first(std::pair& out) const; + void last(std::pair& out) const; + void next(std::pair& out) const; + void prev(std::pair& out) const; + void current(std::pair& out) const; private: - void terminated(); + void terminated() const; private: Storage* 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*/ }; }; diff --git a/src/cursor.hpp b/src/cursor.hpp index 68f8497..6a575ad 100644 --- a/src/cursor.hpp +++ b/src/cursor.hpp @@ -23,19 +23,122 @@ template LMDBAL::Cursor::Cursor(Storage* parent): - storage(parent) + storage(parent), + cursor(nullptr), + state(closed) { } template LMDBAL::Cursor::~Cursor () { - + close(); } template -void LMDBAL::Cursor::terminated () { +void LMDBAL::Cursor::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 +void LMDBAL::Cursor::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 +void LMDBAL::Cursor::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 +void LMDBAL::Cursor::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 +void LMDBAL::Cursor::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 +void LMDBAL::Cursor::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 diff --git a/src/storage.h b/src/storage.h index e52667b..bfac49c 100644 --- a/src/storage.h +++ b/src/storage.h @@ -91,9 +91,6 @@ protected: static std::string toString(const T& value); }; -// template -// class Cursor; - template class Storage : public iStorage { friend class Base; @@ -133,7 +130,7 @@ public: protected: mutable Serializer keySerializer; /**<\brief internal object that would serialize and deserialize keys*/ mutable Serializer valueSerializer; /**<\brief internal object that would serialize and deserialize values*/ - std::set*> cursors; /**<\brief a set of cursors that has been created under this storage*/ + std::set*> cursors; /**<\brief a set of cursors that has been created under this storage*/ int open(MDB_txn* transaction) override; void close() override; diff --git a/src/storage.hpp b/src/storage.hpp index f14fa5e..58e3dee 100644 --- a/src/storage.hpp +++ b/src/storage.hpp @@ -54,7 +54,10 @@ LMDBAL::Storage::Storage(const std::string& _name, Base* parent): * \brief Destroys a storage */ template -LMDBAL::Storage::~Storage() {} +LMDBAL::Storage::~Storage() { + for (Cursor* cursor : cursors) + delete cursor; +} /** * \brief Adds a key-value record to the storage