/* * LMDB Abstraction Layer. * Copyright (C) 2023 Yury Gubich * * 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 . */ #pragma once #include "cursor.h" #include /** * \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! */ static uint32_t idCounter = 0; /** * \brief Creates a cursor * * \param[in] parent a storage that created this cursor */ template LMDBAL::Cursor::Cursor(Storage* parent): storage(parent), cursor(nullptr), state(closed), id(++idCounter) { storage->cursors[id] = this; } /** * \brief Creates an empty cursor. * * It's not usable, but can exist just to be a target of moves */ template LMDBAL::Cursor::Cursor(): storage(nullptr), cursor(nullptr), state(closed), id(0) {} /** * \brief Moves from another cursor */ template LMDBAL::Cursor::Cursor(Cursor&& other): storage(other.storage), cursor(other.cursor), state(other.state), id(other.id) { other.terminated(); if (id != 0) storage->cursors[id] = this; if (state == openedPublic) storage->attachCursorToTransaction(id, cursor, this); other.freed(); } /** * \brief A private function that turns cursor into an empty one * * This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid. * Those cursors will become empty, and can't be used anymore */ template LMDBAL::Cursor& LMDBAL::Cursor::operator = (Cursor&& other) { terminated(); if (id != 0) storage->cursors.erase(id); storage = other.storage; cursor = other.cursor; state = other.state; id = other.id; if (id != 0) { other.freed(); other.state = closed; storage->cursors[id] = this; if (state == openedPublic) storage->attachCursorToTransaction(id, cursor, this); } return *this; } /** * \brief Destroys a cursor * * If the cursor wasn't properly closed - it's going to be upon destruction */ template LMDBAL::Cursor::~Cursor () { close(); if (id != 0) storage->cursors.erase(id); } /** * \brief Turns cursor into an empty one, releasing resources * * This function is called from LMDBAL::Storage, when it gets destroyed, but still has some valid. * Those cursors will become empty, and can't be used anymore */ template void LMDBAL::Cursor::drop () { close(); if (id != 0) storage->cursors.erase(id); 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 cursors. * Those cursors will become empty, and can't be used anymore */ template void LMDBAL::Cursor::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 void LMDBAL::Cursor::freed () { cursor = nullptr; storage = nullptr; id = 0; } /** * \brief Returns true if the cursor is empty * * Empty cursors can't be used, they can be only targets of move operations */ template bool LMDBAL::Cursor::empty () const { return id == 0; } /** * \brief A private function the storage owning this cursor will call to inform this cursor that the thansaction needs to be aborted */ template void LMDBAL::Cursor::terminated () { switch (state) { case openedPublic: storage->_mdbCursorClose(cursor); state = closed; break; case openedPrivate: storage->closeCursorTransaction(cursor, true); state = closed; 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 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). * * \exception LMDBAL::Closed thrown if you try to open the cursor on a closed database * \exception LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb, or to begin a transaction * \exception LMDBAL::CursorEmpty thrown if the cursor was empty */ template void LMDBAL::Cursor::open () { if (empty()) throw CursorEmpty(openCursorMethodName); switch (state) { case closed: storage->openCursorTransaction(&cursor, false); 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). * * \param[in] transaction - a transaction, can be read only * * \exception LMDBAL::Closed thrown if you try to open the cursor on a closed database * \exception LMDBAL::Unknown thrown if there was a problem opening the cursor by the lmdb * \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error * \exception LMDBAL::CursorEmpty thrown if the cursor was empty */ template void LMDBAL::Cursor::open (const Transaction& transaction) { if (empty()) throw CursorEmpty(openCursorMethodName); storage->ensureOpened(openCursorMethodName); TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName); switch (state) { case closed: { int result = storage->_mdbCursorOpen(txn, &cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result); storage->attachCursorToTransaction(id, cursor, this); 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 * * \exception LMDBAL::Closed thrown if you try to renew the cursor on a closed database * \exception LMDBAL::Unknown thrown if there was a problem beginning new transaction or if there was a problem renewing the cursor by lmdb * \exception LMDBAL::CursorEmpty thrown if the cursor was empty */ template void LMDBAL::Cursor::renew () { if (empty()) throw CursorEmpty(openCursorMethodName); storage->ensureOpened(renewCursorMethodName); switch (state) { case openedPrivate: storage->closeCursorTransaction(cursor, false); storage->openCursorTransaction(&cursor, true); break; case openedPublic: storage->disconnectCursorFromTransaction(id, cursor); storage->openCursorTransaction(&cursor, 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] transaction - a transaction you wish this cursor to be bound to * * \exception LMDBAL::Closed thrown if you try to renew the cursor on a closed database * \exception LMDBAL::Unknown thrown if there was a problem renewing the cursor by lmdb * \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error * \exception LMDBAL::CursorEmpty thrown if the cursor was empty */ template void LMDBAL::Cursor::renew (const Transaction& transaction) { if (empty()) throw CursorEmpty(openCursorMethodName); storage->ensureOpened(renewCursorMethodName); TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName); switch (state) { case openedPrivate: { storage->closeCursorTransaction(cursor, false); int result = storage->_mdbCursorRenew(txn, cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result); storage->attachCursorToTransaction(id, cursor, this); state = openedPublic; } break; case openedPublic: { storage->disconnectCursorFromTransaction(id, cursor); int result = storage->_mdbCursorRenew(txn, cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result); storage->attachCursorToTransaction(id, cursor, this); } 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 void LMDBAL::Cursor::close () { switch (state) { case openedPublic: storage->disconnectCursorFromTransaction(id, cursor); storage->_mdbCursorClose(cursor); state = closed; break; case openedPrivate: storage->closeCursorTransaction(cursor, true); state = closed; break; default: break; } } /** * \brief Tells if the cursor is open */ template bool LMDBAL::Cursor::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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template void LMDBAL::Cursor::first (K& key, V& value) { 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template void LMDBAL::Cursor::last (K& key, V& value) { 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template void LMDBAL::Cursor::next (K& key, V& value) { 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template void LMDBAL::Cursor::prev (K& key, V& value) { 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template void LMDBAL::Cursor::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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template std::pair LMDBAL::Cursor::first () { std::pair 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template std::pair LMDBAL::Cursor::last () { std::pair 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if the cursor already was on the last element or if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template std::pair LMDBAL::Cursor::next () { std::pair 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound thrown if the cursor already was on the first element or if there are no elements in the storage * \exception LMDBAL::Unknown thrown if there was some unexpected problem with lmdb */ template std::pair LMDBAL::Cursor::prev () { std::pair 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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound probably never thrown but there might be still some corner case I don't know about * \exception LMDBAL::Unknown thrown if there was no positioning operation before of if there was some unexpected problem with lmdb */ template std::pair LMDBAL::Cursor::current () const { std::pair result; operateCursorRead(result.first, result.second, MDB_GET_CURRENT, currentMethodName, currentOperationName); 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 bool LMDBAL::Cursor::set (const K& key) { if (state == closed) storage->throwCursorNotReady(setMethodName); MDB_val mdbKey = storage->keySerializer.setData(key); int result = storage->_mdbCursorSet(cursor, mdbKey); 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 * * 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 operation code * \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 * * \exception LMDBAL::CursorNotReady thrown if you try to call this method on a closed cursor * \exception LMDBAL::NotFound mostly thrown if the query wasn't found * \exception LMDBAL::Unknown mostly thrown if there was some unexpected problem with lmdb */ template void LMDBAL::Cursor::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 = storage->_mdbCursorGet(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, storage->_mdbCursorTxn(cursor)); }