LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
cursor.h
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 <string>
22
23#include "lmdb.h"
24#include "base.h"
25#include "storage.h"
26#include "transaction.h"
27
28namespace LMDBAL {
29
30template <class K, class V>
31class Cursor {
32 friend class Storage<K, V>;
33private:
34 enum State {
35 closed,
36 openedPublic,
37 openedPrivate
38 };
39
40public:
41 Cursor();
42 Cursor(Storage<K, V>* parent);
43 Cursor(const Cursor& other) = delete;
44 Cursor(Cursor&& other);
45 ~Cursor();
46
47 Cursor& operator = (const Cursor& other) = delete;
48 Cursor& operator = (Cursor&& other);
49
50 void open();
51 void open(const Transaction& transaction);
52 void renew();
53 void renew(const Transaction& transaction);
54 void close();
55 bool opened() const;
56 bool empty() const;
57
58 void drop();
59
60 std::pair<K, V> first();
61 std::pair<K, V> last();
62 std::pair<K, V> next();
63 std::pair<K, V> prev();
64 std::pair<K, V> current() const;
65 bool set(const K& target);
66
67 void first(K& key, V& value);
68 void last(K& key, V& value);
69 void next(K& key, V& value);
70 void prev(K& key, V& value);
71 void current(K& key, V& value) const;
72
73private:
74 void dropped();
75 void freed();
76 void terminated();
77 void operateCursorRead(K& key, V& value, MDB_cursor_op operation, const std::string& methodName, const std::string& operationName) const;
78
79private:
80 Storage<K, V>* storage;
81 MDB_cursor* cursor;
82 State state;
83 uint32_t id;
84
85 inline static const std::string openCursorMethodName = "Cursor::open";
86 inline static const std::string closeCursorMethodName = "Cursor::close";
87 inline static const std::string renewCursorMethodName = "Cursor::renew";
89 inline static const std::string firstMethodName = "first";
90 inline static const std::string lastMethodName = "last";
91 inline static const std::string nextMethodName = "next";
92 inline static const std::string prevMethodName = "prev";
93 inline static const std::string currentMethodName = "current";
94 inline static const std::string setMethodName = "set";
96 inline static const std::string firstOperationName = "Cursor::first";
97 inline static const std::string lastOperationName = "Cursor::last";
98 inline static const std::string nextOperationName = "Cursor::next";
99 inline static const std::string prevOperationName = "Cursor::prev";
100 inline static const std::string currentOperationName = "Cursor::current";
101};
102
103};
104
105
106#include "cursor.hpp"
An object to iterate storages.
Definition cursor.h:31
std::pair< K, V > first()
Queries the first element in the storage.
Definition cursor.hpp:500
void drop()
Turns cursor into an empty one, releasing resources.
Definition cursor.hpp:138
bool empty() const
Returns true if the cursor is empty.
Definition cursor.hpp:178
void renew()
Renews a cursor.
Definition cursor.hpp:278
~Cursor()
Destroys a cursor.
Definition cursor.hpp:124
void close()
Termiates a sequence of operations with the cursor.
Definition cursor.hpp:361
bool opened() const
Tells if the cursor is open.
Definition cursor.hpp:385
void open()
Opens the cursor for operations.
Definition cursor.hpp:204
std::pair< K, V > prev()
Queries the previous element from the storage.
Definition cursor.hpp:566
std::pair< K, V > last()
Queries the last element in the storage.
Definition cursor.hpp:518
std::pair< K, V > current() const
Returns current cursor element from the storage.
Definition cursor.hpp:586
bool set(const K &target)
Sets cursors to the defined position.
Definition cursor.hpp:605
Cursor()
Creates an empty cursor.
Definition cursor.hpp:66
std::pair< K, V > next()
Queries the next element from the storage.
Definition cursor.hpp:542
This is a basic key value storage.
Definition storage.h:134
Public read only transaction.
Definition transaction.h:26