/* * 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! */ /** * \brief Creates a cursor * * \param[in] parent a storage that created this cursor */ template LMDBAL::Cursor::Cursor(Storage* parent): CursorCommon(parent) { parent->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(): CursorCommon() {} /** * \brief Moves from another cursor */ template LMDBAL::Cursor::Cursor(Cursor&& other): CursorCommon(std::move(other)) { if (!empty()) static_cast*>(storage)->cursors[id] = this; } /** * \brief Move assignment operator * * Transfers other cursor into this one */ template LMDBAL::Cursor& LMDBAL::Cursor::operator = (Cursor&& other) { if (!empty() && other.empty()) static_cast*>(storage)->cursors.erase(id); CursorCommon::operator=(std::move(other)); if (!empty()) static_cast*>(storage)->cursors[id] = this; return *this; } /** * \brief Destroys this cursor * * If the cursor wasn't properly closed - it's going to be upon destruction */ template LMDBAL::Cursor::~Cursor () { if (id != 0) static_cast*>(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) static_cast*>(storage)->cursors.erase(id); reset(); } /** * \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) static_cast*>(storage)->throwCursorNotReady(setMethodName); MDB_val mdbKey = static_cast*>(storage)->keySerializer.setData(key); int result = static_cast*>(storage)->_mdbCursorSet(handle, mdbKey); if (result == MDB_SUCCESS) return true; else if (result == MDB_NOTFOUND) return false; static_cast*>(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) static_cast*>(storage)->throwCursorNotReady(methodName); MDB_val mdbKey, mdbValue; int result = static_cast*>(storage)->_mdbCursorGet(handle, mdbKey, mdbValue, operation); if (result != MDB_SUCCESS) static_cast*>(storage)->throwNotFoundOrUnknown(result, operationName); static_cast*>(storage)->keySerializer.deserialize(mdbKey, key); static_cast*>(storage)->valueSerializer.deserialize(mdbValue, value); if (state == openedPrivate) static_cast*>(storage)->discoveredRecord(key, value); else static_cast*>(storage)->discoveredRecord(key, value, static_cast*>(storage)->_mdbCursorTxn(handle)); }