1
0
Fork 0
forked from blue/lmdbal

finally methods that actually do something, some testing of them

This commit is contained in:
Blue 2023-08-09 14:41:15 -03:00
parent 5fba60f7f0
commit 8cb1e97e30
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 348 additions and 12 deletions

View file

@ -53,23 +53,36 @@ public:
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;
void first(K& key, V& value) const;
void last(K& key, V& value) const;
void next(K& key, V& value) const;
void prev(K& key, V& value) const;
void current(K& key, V& value) const;
private:
void terminated() const;
void operateCursorRead(K& key, V& value, MDB_cursor_op operation, const std::string& methodName, const std::string& operationName) const;
private:
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*/
inline static const std::string openCursorMethodName = "Cursor::open"; /**<\brief member function name, just for exceptions*/
inline static const std::string closeCursorMethodName = "Cursor::close"; /**<\brief member function name, just for exceptions*/
inline static const std::string renewCursorMethodName = "Cursor::renew"; /**<\brief member function name, just for exceptions*/
inline static const std::string firstMethodName = "first"; /**<\brief member function name, just for exceptions*/
inline static const std::string lastMethodName = "last"; /**<\brief member function name, just for exceptions*/
inline static const std::string nextMethodName = "next"; /**<\brief member function name, just for exceptions*/
inline static const std::string prevMethodName = "prev"; /**<\brief member function name, just for exceptions*/
inline static const std::string currentMethodName = "current"; /**<\brief member function name, just for exceptions*/
inline static const std::string firstOperationName = "Cursor::first"; /**<\brief member function name, just for exceptions*/
inline static const std::string lastOperationName = "Cursor::last"; /**<\brief member function name, just for exceptions*/
inline static const std::string nextOperationName = "Cursor::next"; /**<\brief member function name, just for exceptions*/
inline static const std::string prevOperationName = "Cursor::prev"; /**<\brief member function name, just for exceptions*/
inline static const std::string currentOperationName = "Cursor::current"; /**<\brief member function name, just for exceptions*/
};
};

View file

@ -42,7 +42,7 @@ void LMDBAL::Cursor<K, V>::terminated () const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::open () const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(openCursorMethodName);
switch (state) {
case closed: {
TransactionID txn = storage->beginReadOnlyTransaction();
@ -60,7 +60,7 @@ void LMDBAL::Cursor<K, V>::open () const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(openCursorMethodName);
switch (state) {
case closed: {
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
@ -76,7 +76,7 @@ void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew () const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(renewCursorMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
@ -100,7 +100,7 @@ void LMDBAL::Cursor<K, V>::renew () const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(renewCursorMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
@ -141,4 +141,90 @@ void LMDBAL::Cursor<K, V>::close () const {
}
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::first (K& key, V& value) const {
operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::last (K& key, V& value) const {
operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::next (K& key, V& value) const {
operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::prev (K& key, V& value) const {
operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
operateCursorRead(key, value, MDB_GET_CURRENT, currentMethodName, currentOperationName);
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::first () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::last () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::next () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::prev () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_GET_CURRENT, currentMethodName, currentOperationName);
return result;
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::operateCursorRead(
K& key,
V& value,
MDB_cursor_op operation,
const std::string& methodName,
const std::string& operationName
) const {
if (state == closed)
storage->throwCursorNotReady(methodName);
MDB_val mdbKey, mdbValue;
int result = mdb_cursor_get(cursor, &mdbKey, &mdbValue, operation);
if (result != MDB_SUCCESS)
storage->throwNotFoundOrUnknown(result, operationName);
storage->keySerializer.deserialize(mdbKey, key);
storage->valueSerializer.deserialize(mdbValue, value);
if (state == openedPrivate)
storage->discoveredRecord(key, value);
else
storage->discoveredRecord(key, value, mdb_cursor_txn(cursor));
}
#endif //LMDBAL_CURSOR_HPP

View file

@ -55,6 +55,23 @@ std::string LMDBAL::Closed::getMessage() const {
return msg;
}
LMDBAL::CursorNotReady::CursorNotReady(
const std::string& p_operation,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
operation(p_operation),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::CursorNotReady::getMessage() const {
std::string msg = "An attempt to perform operation " + operation
+ " on closed cursor that belongs to the table " + tableName
+ " within database " + dbName;
return msg;
}
LMDBAL::Opened::Opened(const std::string& p_dbName, const std::string& p_action):
Exception(),
dbName(p_dbName),

View file

@ -76,6 +76,27 @@ private:
std::optional<std::string> tableName;
};
/**
* \brief Thrown if the cursor was operated in closed state
*/
class CursorNotReady : public Exception {
public:
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on closed cursor
* \param dbName - name of the database
* \param tableName - name of the storage owning the cursor
*/
CursorNotReady(const std::string& operation, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string operation;
std::string dbName;
std::string tableName;
};
/**
* \brief Thrown if something in the database was called on opened state and it is not supported
*/

View file

@ -277,6 +277,18 @@ void LMDBAL::iStorage::throwDuplicate(const std::string& key) const {
void LMDBAL::iStorage::throwNotFound(const std::string& key) const {
throw NotFound(key, db->name, name);}
/**
* \brief Throws LMDBAL::CursorNotReady
*
* Helper function ment to be used in heirs and reduce the code a bit
*
* \param[in] method - called cursor method name, just to show in std::exception::what() message
*
* \exception LMDBAL::CursorNotReady thrown everytime
*/
void LMDBAL::iStorage::throwCursorNotReady(const std::string& method) const {
throw CursorNotReady(method, db->name, name);}
/**
* \brief Begins read-only transaction
*

View file

@ -50,6 +50,7 @@ protected:
void throwUnknown(int rc) const;
void throwDuplicate(const std::string& key) const;
void throwNotFound(const std::string& key) const;
void throwCursorNotReady(const std::string& method) const;
TransactionID beginReadOnlyTransaction() const;
TransactionID beginTransaction() const;
@ -99,6 +100,9 @@ protected:
Storage(const std::string& name, Base* parent);
~Storage() override;
virtual void discoveredRecord(const K& key, const V& value) const;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const;
public:
using iStorage::drop;
virtual void addRecord(const K& key, const V& value);

View file

@ -22,6 +22,8 @@
#include "storage.h"
#include "exceptions.h"
#define UNUSED(x) (void)(x)
/**
* \class LMDBAL::Storage
* \brief This is a basic key value storage.
@ -718,6 +720,19 @@ void LMDBAL::Storage<K, V>::destroyCursor(Cursor<K, V>* cursor) {
delete cursor;
}
template<class K, class V>
void LMDBAL::Storage<K, V>::discoveredRecord(const K& key, const V& value) const {
UNUSED(key);
UNUSED(value);
}
template<class K, class V>
void LMDBAL::Storage<K, V>::discoveredRecord(const K& key, const V& value, TransactionID txn) const {
UNUSED(key);
UNUSED(value);
UNUSED(txn);
}
/**
* \brief A functiion to actually open <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gadbe68a06c448dfb62da16443d251a78b">MDB_dbi</a> storage
*