LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
storagecommon.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#include <stdint.h>
23
24#include <lmdb.h>
25
26#include "base.h"
27#include "transaction.h"
28
29namespace LMDBAL {
30
31class CursorCommon;
32
34 friend class Base;
35 friend class CursorCommon;
36public:
37protected:
38 StorageCommon(Base* parent, const std::string& name, bool duplicates = false);
39 virtual ~ StorageCommon();
40
47 virtual int open(MDB_txn * transaction) = 0;
48 virtual void close();
49 virtual void handleDrop();
50
51 bool isDBOpened() const;
52 const std::string& dbName() const;
53
54 void ensureOpened(const std::string& methodName) const;
55 void throwDuplicateOrUnknown(int rc, const std::string& key) const;
56 void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const;
57 void throwNotFoundOrUnknown(int rc, const std::string& key) const;
58 void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const;
59 void throwUnknown(int rc, TransactionID txn) const;
60 void throwUnknown(int rc) const;
61 void throwUnknown(const std::string& message) const;
62 void throwDuplicate(const std::string& key) const;
63 void throwNotFound(const std::string& key) const;
64 void throwCursorNotReady(const std::string& method) const;
65
66 TransactionID extractTransactionId(const Transaction& txn, const std::string& action = "") const;
67 TransactionID beginReadOnlyTransaction() const;
68 TransactionID beginTransaction() const;
69 void commitTransaction(TransactionID id);
70 void abortTransaction(TransactionID id) const;
71 virtual void transactionStarted(TransactionID txn, bool readOnly) const;
72 virtual void transactionCommited(TransactionID txn);
73 virtual void transactionAborted(TransactionID txn) const;
74 virtual int drop(TransactionID transaction);
75 virtual SizeType count(TransactionID txn) const;
76
77 Transaction* getTransactionForCursor(MDB_cursor* cursor) const;
78
79 void openCursorTransaction(MDB_cursor** cursor, bool renew = false) const;
80 void closeCursorTransaction(MDB_cursor* cursor, bool closeCursor = false) const;
81
82 int _mdbOpen(MDB_txn* txn, unsigned int flags = 0);
83
84 int _mdbPut(MDB_txn* txn, MDB_val& key, MDB_val& data, unsigned int flags = 0);
85 int _mdbGet(MDB_txn* txn, MDB_val& key, MDB_val& data) const;
86 int _mdbDel(MDB_txn* txn, MDB_val& key);
87 int _mdbDel(MDB_txn* txn, MDB_val& key, MDB_val& data);
88
89 int _mdbStat(MDB_txn* txn, MDB_stat& stat) const;
90 int _mdbFlags(MDB_txn* txn, uint32_t& flags) const;
91
92 int _mdbCursorOpen(MDB_txn* txn, MDB_cursor** cursor) const;
93 void _mdbCursorClose(MDB_cursor* cursor) const;
94 int _mdbCursorGet(MDB_cursor* cursor, MDB_val& key, MDB_val& data, MDB_cursor_op operation) const;
95 int _mdbCursorSet(MDB_cursor* cursor, MDB_val& key) const;
96 int _mdbCursorDel(MDB_cursor* cursor, unsigned int flags = 0);
97 int _mdbCursorPut(MDB_cursor* cursor, MDB_val& key, MDB_val& data, unsigned int flags = 0);
98
99 int _mdbCursorRenew(MDB_txn* txn, MDB_cursor* cursor) const;
100 MDB_txn* _mdbCursorTxn(MDB_cursor* cursor) const;
101
102public:
103 virtual void drop();
104 virtual int drop(const WriteTransaction& txn);
105 virtual SizeType count() const;
106 virtual SizeType count(const Transaction& txn) const;
107
108protected:
109 MDB_dbi dbi;
110 Base* db;
111 const std::string name;
112 const bool duplicates;
113
114 inline static const std::string dropMethodName = "drop";
115 inline static const std::string countMethodName = "count";
116 inline static const std::string flagsMethodName = "flags";
117
118 inline static const std::string addRecordMethodName = "addRecord";
119 inline static const std::string forceRecordMethodName = "forceRecord";
120 inline static const std::string changeRecordMethodName = "changeRecord";
121 inline static const std::string removeRecordMethodName = "removeRecord";
122 inline static const std::string checkRecordMethodName = "checkRecord";
123 inline static const std::string getRecordMethodName = "getRecord";
124 inline static const std::string readAllMethodName = "readAllRecord";
125 inline static const std::string replaceAllMethodName = "replaceAll";
126 inline static const std::string addRecordsMethodName = "addRecords";
127 inline static const std::string openCursorTransactionMethodName = "openCursorTransaction";
128
129protected:
130 template <class K, class V>
131 int makeStorage(MDB_txn* transaction, bool duplicates = false);
132
133 template <class T>
134 static std::string toString(const T& value);
135};
136
137}
138
139#include "storagecommon.hpp"
An object to manage cursor internals and state.
Definition cursorcommon.h:31
StorageCommon(Base *parent, const std::string &name, bool duplicates=false)
Constructs a storage interface.
Definition storagecommon.cpp:42
void commitTransaction(TransactionID id)
Commits transaction.
Definition storagecommon.cpp:454
static const std::string getRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:123
virtual void transactionCommited(TransactionID txn)
called on commitment of public transaction
Definition storagecommon.cpp:485
void throwNotFound(const std::string &key) const
Throws LMDBAL::NotFound.
Definition storagecommon.cpp:404
void throwCursorNotReady(const std::string &method) const
Throws LMDBAL::CursorNotReady.
Definition storagecommon.cpp:416
TransactionID beginReadOnlyTransaction() const
Begins read-only transaction.
Definition storagecommon.cpp:426
static const std::string openCursorTransactionMethodName
member function name, just for exceptions
Definition storagecommon.h:127
int makeStorage(MDB_txn *transaction, bool duplicates=false)
A functiion to actually open MDB_dbi storage.
Definition storagecommon.hpp:38
void abortTransaction(TransactionID id) const
Aborts transaction.
Definition storagecommon.cpp:444
static std::string toString(const T &value)
A method to cast a value (which can be a value or a key) to string.
Definition storagecommon.hpp:71
static const std::string forceRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:119
Transaction * getTransactionForCursor(MDB_cursor *cursor) const
Retrieves public transaction object for a cursor handle.
Definition storagecommon.cpp:173
virtual SizeType count() const
Storage size.
Definition storagecommon.cpp:148
virtual void close()
A private virtual function to close each storage in the database.
Definition storagecommon.cpp:57
static const std::string countMethodName
member function name, just for exceptions
Definition storagecommon.h:115
static const std::string flagsMethodName
member function name, just for exceptions
Definition storagecommon.h:116
const std::string name
this storage name
Definition storagecommon.h:111
static const std::string checkRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:122
virtual void handleDrop()
A method where database additionally handles drop.
Definition storagecommon.cpp:508
virtual int open(MDB_txn *transaction)=0
A private virtual function I need to open each storage in the database.
const std::string & dbName() const
Database name.
Definition storagecommon.cpp:346
static const std::string dropMethodName
member function name, just for exceptions
Definition storagecommon.h:114
TransactionID extractTransactionId(const Transaction &txn, const std::string &action="") const
Checks if the transaction is still active, returns inner TransactionID.
Definition storagecommon.cpp:72
static const std::string readAllMethodName
member function name, just for exceptions
Definition storagecommon.h:124
void closeCursorTransaction(MDB_cursor *cursor, bool closeCursor=false) const
Closes transaction that is private to LMDBAL::Cursor, but public to the LMDBAL::Storage.
Definition storagecommon.cpp:213
static const std::string changeRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:120
void throwDuplicate(const std::string &key) const
Throws LMDBAL::Exist.
Definition storagecommon.cpp:392
virtual void transactionAborted(TransactionID txn) const
called on abortion of public transaction
Definition storagecommon.cpp:499
static const std::string addRecordsMethodName
member function name, just for exceptions
Definition storagecommon.h:126
void throwUnknown(int rc, TransactionID txn) const
Throws LMDBAL::Unknown (transaction vairiant)
Definition storagecommon.cpp:334
MDB_dbi dbi
lmdb storage handle
Definition storagecommon.h:109
void throwDuplicateOrUnknown(int rc, const std::string &key) const
Throws LMDBAL::Exist or LMDBAL::Unknown.
Definition storagecommon.cpp:299
void throwNotFoundOrUnknown(int rc, const std::string &key) const
Throws LMDBAL::NotFound or LMDBAL::Unknown (transaction variant)
Definition storagecommon.cpp:317
TransactionID beginTransaction() const
Begins writable transaction.
Definition storagecommon.cpp:436
static const std::string addRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:118
const bool duplicates
true if storage supports duplicates
Definition storagecommon.h:112
void ensureOpened(const std::string &methodName) const
Helper function, thows exception if the database is not opened.
Definition storagecommon.cpp:135
static const std::string replaceAllMethodName
member function name, just for exceptions
Definition storagecommon.h:125
static const std::string removeRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:121
bool isDBOpened() const
Is database opened.
Definition storagecommon.cpp:356
void openCursorTransaction(MDB_cursor **cursor, bool renew=false) const
Opens a transaction that is ment to be private to LMDBAL::Cursor, but public to the LMDBAL::Storage.
Definition storagecommon.cpp:189
virtual void transactionStarted(TransactionID txn, bool readOnly) const
called on beginning of public transaction
Definition storagecommon.cpp:469
Base * db
parent database pointer (borrowed)
Definition storagecommon.h:110
virtual void drop()
Drops content of a storage interface.
Definition storagecommon.cpp:87
Public read only transaction.
Definition transaction.h:27
Public writable transaction.
Definition transaction.h:54