tests, set method, tests for set method
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 46s
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 46s
This commit is contained in:
parent
96d7d9ef64
commit
3307860ca6
7 changed files with 316 additions and 15 deletions
|
@ -402,10 +402,9 @@ void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string
|
|||
LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::string& storageName) const {
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
|
||||
return txn;
|
||||
}
|
||||
|
||||
|
|
|
@ -73,6 +73,7 @@ public:
|
|||
|
||||
private:
|
||||
void dropped();
|
||||
void freed();
|
||||
void terminated();
|
||||
void operateCursorRead(K& key, V& value, MDB_cursor_op operation, const std::string& methodName, const std::string& operationName) const;
|
||||
|
||||
|
@ -91,6 +92,7 @@ private:
|
|||
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 setMethodName = "set"; /**<\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*/
|
||||
|
|
|
@ -81,13 +81,11 @@ LMDBAL::Cursor<K, V>::Cursor(Cursor&& other):
|
|||
state(other.state),
|
||||
id(other.id)
|
||||
{
|
||||
other.terminated();
|
||||
if (id != 0)
|
||||
storage->cursors[id] = this;
|
||||
|
||||
other.cursor = nullptr;
|
||||
other.storage = nullptr;
|
||||
other.id = 0;
|
||||
other.state = closed;
|
||||
other.freed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,9 +107,7 @@ LMDBAL::Cursor<K, V>& LMDBAL::Cursor<K, V>::operator = (Cursor&& other) {
|
|||
id = other.id;
|
||||
|
||||
if (id != 0) {
|
||||
other.cursor = nullptr;
|
||||
other.storage = nullptr;
|
||||
other.id = 0;
|
||||
other.freed();
|
||||
other.state = closed;
|
||||
|
||||
storage->cursors[id] = this;
|
||||
|
@ -146,20 +142,29 @@ void LMDBAL::Cursor<K, V>::drop () {
|
|||
if (id != 0)
|
||||
storage->cursors.erase(id);
|
||||
|
||||
cursor = nullptr;
|
||||
storage = nullptr;
|
||||
id = 0;
|
||||
freed();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief A private method that turns cursor into an empty one
|
||||
*
|
||||
* This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid.
|
||||
* This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid cursors.
|
||||
* Those cursors will become empty, and can't be used anymore
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::dropped () {
|
||||
terminated();
|
||||
freed();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief A private method that turns cursor into an empty one (submethod)
|
||||
*
|
||||
* This function is called from LMDBAL::Storage, when the cursor is getting destoryed.
|
||||
* Those cursors will become empty, and can't be used anymore
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::freed () {
|
||||
cursor = nullptr;
|
||||
storage = nullptr;
|
||||
id = 0;
|
||||
|
@ -585,6 +590,34 @@ std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Sets cursors to the defined position
|
||||
*
|
||||
* If exactly the same element wasn't found it sets the cursor to the first
|
||||
* element greater then the key and returns false
|
||||
*
|
||||
* \param[in] key a key of the element you would like the cursor to be
|
||||
* \returns true if the exact value was found, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
|
||||
* \exception LMDBAL::Unknown thrown if there was some unexpected problem
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Cursor<K, V>::set (const K& key) {
|
||||
if (state == closed)
|
||||
storage->throwCursorNotReady(setMethodName);
|
||||
|
||||
MDB_val mdbKey = storage->keySerializer.setData(key);
|
||||
int result = mdb_cursor_get(cursor, &mdbKey, nullptr, MDB_SET);
|
||||
if (result == MDB_SUCCESS)
|
||||
return true;
|
||||
else if (result == MDB_NOTFOUND)
|
||||
return false;
|
||||
|
||||
storage->throwUnknown(result);
|
||||
return false; //unreachable, just to suppress the warning
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief a private mothod that actually doing all the reading
|
||||
*
|
||||
|
|
|
@ -1022,6 +1022,9 @@ void LMDBAL::Storage<K, V>::close() {
|
|||
/**
|
||||
* \brief Creates cursor
|
||||
*
|
||||
* This is a legitimate way to aquire cursor to a storage.
|
||||
* Aquired cursor is RAII safe, automatic destructor will free everything it occupied.
|
||||
*
|
||||
* \returns LMDBAL::Cursor for this storage and returs you a pointer to a created cursor
|
||||
*/
|
||||
template<class K, class V>
|
||||
|
@ -1029,6 +1032,29 @@ LMDBAL::Cursor<K, V> LMDBAL::Storage<K, V>::createCursor() {
|
|||
return Cursor<K, V>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Frees cursor
|
||||
*
|
||||
* This is a legitimate way to free cursor.
|
||||
* You don't actually need to do it manually,
|
||||
* you can just reassign cursor, let it be destroyed by leaving the scope
|
||||
* or call LMDBAL::Cursor<K, V>::drop, but you may if you wish.
|
||||
*
|
||||
* \param[in] cursor cursor you wish to destroy
|
||||
*
|
||||
* \exception LMDBAL::Unknown thrown if you try to destroy a cursor this storage didn't create
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::destroyCursor(LMDBAL::Cursor<K, V>& cursor) {
|
||||
typename std::map<uint32_t, Cursor<K, V>*>::iterator itr = cursors.find(cursor.id);
|
||||
if (itr == cursors.end())
|
||||
throwUnknown("An attempt to destroy a cursor the storage doesn't own");
|
||||
|
||||
cursor.close();
|
||||
cursors.erase(itr);
|
||||
cursor.freed();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reads current storage flags it was opened with
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue