1
0
forked from blue/lmdbal
lmdbal/src/cursor.hpp

392 lines
16 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/>.
*/
#pragma once
#include "cursor.h"
#include <iostream>
/**
* \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):
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<class K, class V>
LMDBAL::Cursor<K, V>::Cursor():
CursorCommon()
{}
/**
* \brief Moves from another cursor
*/
template<class K, class V>
LMDBAL::Cursor<K, V>::Cursor(Cursor&& other):
CursorCommon(std::move(other))
{
if (!empty())
static_cast<Storage<K, V>*>(storage)->cursors[id] = this;
}
/**
* \brief Move assignment operator
*
* Transfers other cursor into this one
*/
template<class K, class V>
LMDBAL::Cursor<K, V>& LMDBAL::Cursor<K, V>::operator = (Cursor&& other) {
if (!empty() && other.empty())
static_cast<Storage<K, V>*>(storage)->cursors.erase(id);
CursorCommon::operator=(std::move(other));
if (!empty())
static_cast<Storage<K, V>*>(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<class K, class V>
LMDBAL::Cursor<K, V>::~Cursor () {
if (id != 0)
static_cast<Storage<K, V>*>(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<class K, class V>
void LMDBAL::Cursor<K, V>::drop () {
close();
if (id != 0)
static_cast<Storage<K, V>*>(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<class K, class V>
void LMDBAL::Cursor<K, V>::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<class K, class V>
void LMDBAL::Cursor<K, V>::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<class K, class V>
void LMDBAL::Cursor<K, V>::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<class K, class V>
void LMDBAL::Cursor<K, V>::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<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
*
* \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<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::first () {
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
*
* \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<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::last () {
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
*
* \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<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::next () {
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
*
* \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<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::prev () {
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
*
* \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<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 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)
static_cast<Storage<K, V>*>(storage)->throwCursorNotReady(setMethodName);
MDB_val mdbKey = static_cast<Storage<K, V>*>(storage)->keySerializer.setData(key);
int result = static_cast<Storage<K, V>*>(storage)->_mdbCursorSet(handle, mdbKey);
if (result == MDB_SUCCESS)
return true;
else if (result == MDB_NOTFOUND)
return false;
static_cast<Storage<K, V>*>(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 <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
*
* \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<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)
static_cast<Storage<K, V>*>(storage)->throwCursorNotReady(methodName);
MDB_val mdbKey, mdbValue;
int result = static_cast<Storage<K, V>*>(storage)->_mdbCursorGet(handle, mdbKey, mdbValue, operation);
if (result != MDB_SUCCESS)
static_cast<Storage<K, V>*>(storage)->throwNotFoundOrUnknown(result, operationName);
static_cast<Storage<K, V>*>(storage)->keySerializer.deserialize(mdbKey, key);
static_cast<Storage<K, V>*>(storage)->valueSerializer.deserialize(mdbValue, value);
if (state == openedPrivate)
static_cast<Storage<K, V>*>(storage)->discoveredRecord(key, value);
else
static_cast<Storage<K, V>*>(storage)->discoveredRecord(key, value, static_cast<Storage<K, V>*>(storage)->_mdbCursorTxn(handle));
}