lmdbal/src/cursor.hpp

496 lines
20 KiB
C++

/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LMDBAL_CURSOR_HPP
#define LMDBAL_CURSOR_HPP
#include "cursor.h"
/**
* \class LMDBAL::Cursor
* \brief An object to iterate storages.
*
* \tparam K type of the keys in the storage that this cursor would iterate
* \tparam V type of the values in the storage that this cursor would iterate
*
* Cursor allowes you to perform sequential querries, navigate from one element to another at reduced operation price.
* For now Cursors are read only but in the future you might also be able to write to the storage with them.
*
* Cursors are owned by the storage, they die with the storage.
* They also get closed if the storage is closed (if you close by the database for example)
*
* You can obtain an instance of this class calling LMDBAL::Storage::createCursor()
* and destory it calling LMDBAL::Storage::destoryCursor() at any time, LMDBAL::Base doesn't necessarily need to be opened.
*
* You are not supposed to instantiate or destory instances of this class yourself!
*/
/**
* \brief Creates a cursor
*
* \param[in] parent a storage that created this cursor
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
storage(parent),
cursor(nullptr),
state(closed)
{}
/**
* \brief Destroys a cursor
*
* If the cursor wasn't properly closed - it's going to be upon destruction
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::~Cursor () {
close();
}
/**
* \brief A private function the storage owning this cursor will call to inform this cursor that the thansaction needs to be aborted
*/
template<class K, class V>
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
}
/**
* \brief Opens the cursor for operations.
*
* This is a normal way to start the sequence of operations with the cursor.
* This variant of the function creates a read only transaction just for this cursor
*
* This function should be called when the LMDBAL::Storage is already opened and before any query with this cursor!
* It will do nothing to a cursor that was already opened (no matter what way).
*
* \throws LMDBAL::Closed thrown if you try to open the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb, or to begin a transaction
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::open () const {
storage->ensureOpened(openCursorMethodName);
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;
}
}
/**
* \brief Opens the cursor for operations.
*
* This is a normal way to start the sequence of operations with the cursor.
* This variant of the function uses for queries a transaction you have obtained somewhere else.
*
* This function should be called when the LMDBAL::Storage is already opened and before any query with this cursor!
* It will do nothing to a cursor that was already opened (no matter what way).
*
* \throws LMDBAL::Closed thrown if you try to open the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
storage->ensureOpened(openCursorMethodName);
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;
}
}
/**
* \brief Renews a cursor
*
* This function aborts current transaction if the cursor was opened with it's own transaction
* (does not mess up if the transaction was public),
* creates new private transaction and rebinds this cursor to it.
*
* Theoretically you could call this method if your public transaction was aborted (or commited)
* but you wish to continue to keep working with your cursor.
* Or if you just want to rebind your cursor to a new private transaction.
*
* This function does nothing if the cursor is closed
*
* \param[in] txn a transaction you wish this cursor to be bound to
*
* \throws LMDBAL::Closed thrown if you try to renew the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem beginning new transaction or if there was a problem renewing the cursor by lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew () const {
storage->ensureOpened(renewCursorMethodName);
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;
}
}
/**
* \brief Renews a cursor
*
* This function aborts current transaction if the cursor was opened with it's own transaction
* (does not mess up if the transaction was public),
* and rebinds this cursor to a passed new transaction.
*
* Theoretically you could call this method if your previous public transaction was aborted (or commited)
* but you wish to continue to keep working with your cursor.
* Or if you just want to rebind your cursor to another public transaction.
*
* This function does nothing if the cursor is closed
*
* \param[in] txn a transaction you wish this cursor to be bound to
*
* \throws LMDBAL::Closed thrown if you try to renew the cursor on a closed database
* \throws LMDBAL::Unknown thrown if there was a problem renewing the cursor by lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
storage->ensureOpened(renewCursorMethodName);
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;
}
}
/**
* \brief Termiates a sequence of operations with the cursor
*
* This is a normal way to tell that you're done with the cursor and don't want to continue the sequence of queries.
* The state of the cursor is lost after calling this method, some inner resorce is freed.
*
* If the cursor was opened with the private transaction - the owner storage will be notified of the aborted transaction.
*
* This function does nothing on a closed cursor.
*/
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;
}
}
/**
* \brief Tells if the cursor is open
*/
template<class K, class V>
bool LMDBAL::Cursor<K, V>::opened () const {
return state != closed;
}
/**
* \brief Queries the first element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::first (K& key, V& value) const {
operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
}
/**
* \brief Queries the last element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::last (K& key, V& value) const {
operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
}
/**
* \brief Queries the next element from the storage
*
* If there was no operation before this method positions the cursor on the first element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::first(K key, V value).
*
* It will also throw LMDBAL::NotFound if you call this method being on the last element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::next (K& key, V& value) const {
operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
}
/**
* \brief Queries the previous element from the storage
*
* If there was no operation before this method positions the cursor on the last element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::last(K key, V value).
*
* It will also throw LMDBAL::NotFound if you call this method being on the first element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::prev (K& key, V& value) const {
operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
}
/**
* \brief Returns current cursor element from the storage
*
* If there was no operation before this method throws LMDBAL::Unknown for some reason
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
template<class K, class V>
void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
operateCursorRead(key, value, MDB_GET_CURRENT, currentMethodName, currentOperationName);
}
/**
* \brief Queries the first element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
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;
}
/**
* \brief Queries the last element in the storage
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
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;
}
/**
* \brief Queries the next element from the storage
*
* If there was no operation before this method positions the cursor on the first element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::first().
*
* It will also throw LMDBAL::NotFound if you call this method being on the last element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
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;
}
/**
* \brief Queries the previous element from the storage
*
* If there was no operation before this method positions the cursor on the last element and returns it
* so, it's basically doing the same as LMDBAL::Cursor::last().
*
* It will also throw LMDBAL::NotFound if you call this method being on the first element
* or if there are no elements in the database.
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage
* \throws LMDBAL::Unknown thrown if there was some unexpected problem with lmdb
*/
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;
}
/**
* \brief Returns current cursor element from the storage
*
* If there was no operation before this method throws LMDBAL::Unknown for some reason
*
* Notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \returns std::pair where first is element key and second is element value
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about
* \throws LMDBAL::Unknown thrown if there was no positioning operation before of if there was some unexpected problem with lmdb
*/
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;
}
/**
* \brief a private mothod that actually doing all the reading
*
* Queries the storage, deserializes the output, notifies the storage of the queried element calling LMDBAL::Storage::discoveredRecord()
*
* \param[out] key a reference to an object the key of queried element is going to be assigned
* \param[out] value a reference to an object the value of queried element is going to be assigned
* \param[in] operation LMDB cursor <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#ga1206b2af8b95e7f6b0ef6b28708c9127">operation code</a>
* \param[in] methodName a name of the method you called it from, just for the exception message if something goes not as expected
* \param[in] operationName a name of the opeartion, just for the exception message if something goes not as expected
*
* \throws LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor
* \throws LMDBAL::NotFound mostly thrown if the query wasn't found
* \throws LMDBAL::Unknown mostly thrown if there was some unexpected problem with lmdb
*/
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