LMDBAL 0.6.2
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
100namespace LMDBAL {
101template<class K, class V>
102Cursor<K, V>::~Cursor() {
103 if (id != 0) {
104 static_cast<Storage<K, V>*>(storage)->cursors.erase(id);
105 }
106}
107}
108
115template<class K, class V>
117 close();
118
119 if (id != 0)
120 static_cast<Storage<K, V>*>(storage)->cursors.erase(id);
121
122 reset();
123}
124
137template<class K, class V>
138void LMDBAL::Cursor<K, V>::first (K& key, V& value) {
139 operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
140}
141
154template<class K, class V>
155void LMDBAL::Cursor<K, V>::last (K& key, V& value) {
156 operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
157}
158
177template<class K, class V>
178void LMDBAL::Cursor<K, V>::next (K& key, V& value) {
179 operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
180}
181
200template<class K, class V>
201void LMDBAL::Cursor<K, V>::prev (K& key, V& value) {
202 operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
203}
204
219template<class K, class V>
220void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
221 operateCursorRead(key, value, MDB_GET_CURRENT, currentMethodName, currentOperationName);
222}
223
235template<class K, class V>
236std::pair<K, V> LMDBAL::Cursor<K, V>::first () {
237 std::pair<K, V> result;
238 operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
239 return result;
240}
241
253template<class K, class V>
254std::pair<K, V> LMDBAL::Cursor<K, V>::last () {
255 std::pair<K, V> result;
256 operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
257 return result;
258}
259
277template<class K, class V>
278std::pair<K, V> LMDBAL::Cursor<K, V>::next () {
279 std::pair<K, V> result;
280 operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
281 return result;
282}
283
301template<class K, class V>
302std::pair<K, V> LMDBAL::Cursor<K, V>::prev () {
303 std::pair<K, V> result;
304 operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
305 return result;
306}
307
321template<class K, class V>
322std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
323 std::pair<K, V> result;
324 operateCursorRead(result.first, result.second, MDB_GET_CURRENT, currentMethodName, currentOperationName);
325 return result;
326}
327
340template<class K, class V>
341bool LMDBAL::Cursor<K, V>::set (const K& key) {
342 if (state == closed)
343 static_cast<Storage<K, V>*>(storage)->throwCursorNotReady(setMethodName);
344
345 MDB_val mdbKey = static_cast<Storage<K, V>*>(storage)->keySerializer.setData(key);
346 int result = static_cast<Storage<K, V>*>(storage)->_mdbCursorSet(handle, mdbKey);
347 if (result == MDB_SUCCESS)
348 return true;
349 else if (result == MDB_NOTFOUND)
350 return false;
351
352 static_cast<Storage<K, V>*>(storage)->throwUnknown(result);
353 return false; //unreachable, just to suppress the warning
354}
355
371template<class K, class V>
372void LMDBAL::Cursor<K, V>::operateCursorRead(
373 K& key,
374 V& value,
375 MDB_cursor_op operation,
376 std::string_view methodName,
377 std::string_view operationName
378) const {
379 if (state == closed)
380 static_cast<Storage<K, V>*>(storage)->throwCursorNotReady(methodName);
381
382 MDB_val mdbKey, mdbValue;
383 int result = static_cast<Storage<K, V>*>(storage)->_mdbCursorGet(handle, mdbKey, mdbValue, operation);
384 if (result != MDB_SUCCESS)
385 static_cast<Storage<K, V>*>(storage)->throwNotFoundOrUnknown(result, operationName);
386
387 static_cast<Storage<K, V>*>(storage)->keySerializer.deserialize(mdbKey, key);
388 static_cast<Storage<K, V>*>(storage)->valueSerializer.deserialize(mdbValue, value);
389
390 if (state == openedPrivate)
391 static_cast<Storage<K, V>*>(storage)->discoveredRecord(key, value);
392 else
393 static_cast<Storage<K, V>*>(storage)->discoveredRecord(key, value, static_cast<Storage<K, V>*>(storage)->_mdbCursorTxn(handle));
394}
bool empty() const
Returns true if the cursor is empty.
Definition cursorcommon.cpp:333
static constexpr std::string_view nextMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:83
void reset()
A private method that turns cursor into an empty one.
Definition cursorcommon.cpp:113
static constexpr std::string_view lastOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:89
@ closed
Definition cursorcommon.h:36
static constexpr std::string_view prevMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:84
static constexpr std::string_view nextOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:90
static constexpr std::string_view setMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:86
static constexpr std::string_view lastMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:82
static constexpr std::string_view firstOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:88
static constexpr std::string_view prevOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:91
CursorCommon()
Creates a empty class.
Definition cursorcommon.cpp:41
void close()
Termiates a sequence of operations with the cursor.
Definition cursorcommon.cpp:161
static constexpr std::string_view currentOperationName
member function name, just for exceptions in heir
Definition cursorcommon.h:92
static constexpr std::string_view firstMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:81
static constexpr std::string_view currentMethodName
member function name, just for exceptions in heir
Definition cursorcommon.h:85
An object to iterate storages.
Definition cursor.h:33
std::pair< K, V > first()
Queries the first element in the storage.
Definition cursor.hpp:236
void drop()
Turns cursor into an empty one, releasing resources.
Definition cursor.hpp:116
std::pair< K, V > prev()
Queries the previous element from the storage.
Definition cursor.hpp:302
std::pair< K, V > last()
Queries the last element in the storage.
Definition cursor.hpp:254
std::pair< K, V > current() const
Returns current cursor element from the storage.
Definition cursor.hpp:322
bool set(const K &target)
Sets cursors to the defined position.
Definition cursor.hpp:341
Cursor()
Creates an empty cursor.
Definition cursor.hpp:62
std::pair< K, V > next()
Queries the next element from the storage.
Definition cursor.hpp:278
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
Destroys a cache.
Definition base.h:36