LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
cursor.hpp
1/*
2 * LMDB Abstraction Layer.
3 * Copyright (C) 2023 Yury Gubich <blue@macaw.me>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "cursor.h"
22
23#include <iostream>
24
43
49template<class K, class V>
51 CursorCommon(parent)
52{
53 parent->cursors[id] = this;
54}
55
61template<class K, class V>
65
69template<class K, class V>
71 CursorCommon(std::move(other))
72{
73 if (!empty())
74 static_cast<Storage<K, V>*>(storage)->cursors[id] = this;
75}
76
82template<class K, class V>
84 if (!empty() && other.empty())
85 static_cast<Storage<K, V>*>(storage)->cursors.erase(id);
86
87 CursorCommon::operator=(std::move(other));
88
89 if (!empty())
90 static_cast<Storage<K, V>*>(storage)->cursors[id] = this;
91
92 return *this;
93}
94
100template<class K, class V>
102 if (id != 0)
103 static_cast<Storage<K, V>*>(storage)->cursors.erase(id);
104}
105
112template<class K, class V>
114 close();
115
116 if (id != 0)
117 static_cast<Storage<K, V>*>(storage)->cursors.erase(id);
118
119 reset();
120}
121
134template<class K, class V>
135void LMDBAL::Cursor<K, V>::first (K& key, V& value) {
136 operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
137}
138
151template<class K, class V>
152void LMDBAL::Cursor<K, V>::last (K& key, V& value) {
153 operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
154}
155
174template<class K, class V>
175void LMDBAL::Cursor<K, V>::next (K& key, V& value) {
176 operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
177}
178
197template<class K, class V>
198void LMDBAL::Cursor<K, V>::prev (K& key, V& value) {
199 operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
200}
201
216template<class K, class V>
217void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
218 operateCursorRead(key, value, MDB_GET_CURRENT, currentMethodName, currentOperationName);
219}
220
232template<class K, class V>
233std::pair<K, V> LMDBAL::Cursor<K, V>::first () {
234 std::pair<K, V> result;
235 operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
236 return result;
237}
238
250template<class K, class V>
251std::pair<K, V> LMDBAL::Cursor<K, V>::last () {
252 std::pair<K, V> result;
253 operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
254 return result;
255}
256
274template<class K, class V>
275std::pair<K, V> LMDBAL::Cursor<K, V>::next () {
276 std::pair<K, V> result;
277 operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
278 return result;
279}
280
298template<class K, class V>
299std::pair<K, V> LMDBAL::Cursor<K, V>::prev () {
300 std::pair<K, V> result;
301 operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
302 return result;
303}
304
318template<class K, class V>
319std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
320 std::pair<K, V> result;
321 operateCursorRead(result.first, result.second, MDB_GET_CURRENT, currentMethodName, currentOperationName);
322 return result;
323}
324
337template<class K, class V>
338bool LMDBAL::Cursor<K, V>::set (const K& key) {
339 if (state == closed)
340 static_cast<Storage<K, V>*>(storage)->throwCursorNotReady(setMethodName);
341
342 MDB_val mdbKey = static_cast<Storage<K, V>*>(storage)->keySerializer.setData(key);
343 int result = static_cast<Storage<K, V>*>(storage)->_mdbCursorSet(handle, mdbKey);
344 if (result == MDB_SUCCESS)
345 return true;
346 else if (result == MDB_NOTFOUND)
347 return false;
348
349 static_cast<Storage<K, V>*>(storage)->throwUnknown(result);
350 return false; //unreachable, just to suppress the warning
351}
352
368template<class K, class V>
369void LMDBAL::Cursor<K, V>::operateCursorRead(
370 K& key,
371 V& value,
372 MDB_cursor_op operation,
373 const std::string& methodName,
374 const std::string& operationName
375) const {
376 if (state == closed)
377 static_cast<Storage<K, V>*>(storage)->throwCursorNotReady(methodName);
378
379 MDB_val mdbKey, mdbValue;
380 int result = static_cast<Storage<K, V>*>(storage)->_mdbCursorGet(handle, mdbKey, mdbValue, operation);
381 if (result != MDB_SUCCESS)
382 static_cast<Storage<K, V>*>(storage)->throwNotFoundOrUnknown(result, operationName);
383
384 static_cast<Storage<K, V>*>(storage)->keySerializer.deserialize(mdbKey, key);
385 static_cast<Storage<K, V>*>(storage)->valueSerializer.deserialize(mdbValue, value);
386
387 if (state == openedPrivate)
388 static_cast<Storage<K, V>*>(storage)->discoveredRecord(key, value);
389 else
390 static_cast<Storage<K, V>*>(storage)->discoveredRecord(key, value, static_cast<Storage<K, V>*>(storage)->_mdbCursorTxn(handle));
391}
static const std::string lastOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:88
bool empty() const
Returns true if the cursor is empty.
Definition cursorcommon.cpp:333
void reset()
A private method that turns cursor into an empty one.
Definition cursorcommon.cpp:113
static const std::string lastMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:81
@ closed
Definition cursorcommon.h:35
static const std::string currentOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:91
static const std::string prevMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:83
static const std::string currentMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:84
static const std::string nextOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:89
static const std::string firstMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:80
CursorCommon()
Creates a empty class.
Definition cursorcommon.cpp:41
void close()
Termiates a sequence of operations with the cursor.
Definition cursorcommon.cpp:161
static const std::string firstOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:87
static const std::string nextMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:82
static const std::string prevOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:90
static const std::string setMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:85
An object to iterate storages.
Definition cursor.h:32
std::pair< K, V > first()
Queries the first element in the storage.
Definition cursor.hpp:233
void drop()
Turns cursor into an empty one, releasing resources.
Definition cursor.hpp:113
~Cursor()
Destroys this cursor.
Definition cursor.hpp:101
std::pair< K, V > prev()
Queries the previous element from the storage.
Definition cursor.hpp:299
std::pair< K, V > last()
Queries the last element in the storage.
Definition cursor.hpp:251
std::pair< K, V > current() const
Returns current cursor element from the storage.
Definition cursor.hpp:319
bool set(const K &target)
Sets cursors to the defined position.
Definition cursor.hpp:338
Cursor()
Creates an empty cursor.
Definition cursor.hpp:62
std::pair< K, V > next()
Queries the next element from the storage.
Definition cursor.hpp:275
This is a basic key value storage.
Definition storage.h:38
std::map< uint32_t, Cursor< K, V > * > cursors
a set of cursors that has been created under this storage
Definition storage.h:96