LMDBAL 0.6.2
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
base.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 <map>
22#include <set>
23#include <string>
24#include <string_view>
25#include <limits>
26#include <cstdint>
27#include <mutex>
28
29#include <QString>
30#include <QDir>
31
32#include <lmdb.h>
33
34#include "exceptions.h"
35
36namespace LMDBAL {
37
38class StorageCommon;
39class Transaction;
41class Session;
42
43template<class T>
44class Serializer;
45
46template <class K, class V>
47class Storage;
48
49template <class K, class V>
50class Cache;
51
52typedef MDB_txn* TransactionID;
53typedef uint32_t SizeType;
54
55class Base {
56 friend class StorageCommon;
57 friend class Transaction;
58 friend class WriteTransaction;
59 friend class Session;
60
61public:
62 Base(const QString& path, const QString& name = QString(), uint16_t mapSize = 10);
63 ~Base();
64
65 Session open();
66 bool opened() const;
67 bool removeDirectory();
68 bool createDirectory();
69 QString getName() const;
70 QString getPath() const;
71 void drop();
72
73 Transaction beginReadOnlyTransaction() const;
74 WriteTransaction beginTransaction();
75
76 template <class K, class V>
77 LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
78
79 template <class K, class V>
80 LMDBAL::Cache<K, V>* addCache(const std::string& storageName);
81
82 template <class K, class V>
83 LMDBAL::Storage<K, V>* getStorage(const std::string& storageName);
84
85 template <class K, class V>
86 LMDBAL::Cache<K, V>* getCache(const std::string& storageName);
87
88private:
89 typedef std::map<std::string, LMDBAL::StorageCommon *> Storages;
90 typedef std::map<TransactionID, Transaction*> Transactions;
91 typedef std::set<Session*> Sessions;
92
93 void commitTransaction(TransactionID id);
94 void abortTransaction(TransactionID id) const;
95 TransactionID beginReadOnlyTransaction(std::string_view storageName) const;
96 TransactionID beginTransaction(std::string_view storageName) const;
97 void commitTransaction(TransactionID id, std::string_view storageName);
98 void abortTransaction(TransactionID id, std::string_view storageName) const;
99
100 TransactionID beginPrivateReadOnlyTransaction(std::string_view storageName) const;
101 TransactionID beginPrivateTransaction(std::string_view storageName) const;
102 void commitPrivateTransaction(TransactionID id, std::string_view storageName);
103 void abortPrivateTransaction(TransactionID id, std::string_view storageName) const;
104
105 void registerSession(Session* session);
106 void unregisterSession(Session* session);
107 void replaceSession(Session* closing, Session* opening);
108
109 void activate();
110 void deactivate();
111
112private:
113 std::string path;
114 std::string name;
115 Sessions sessions;
116 uint16_t size;
117 MDB_env* environment;
118 Storages storages;
119 mutable Transactions transactions;
120 std::mutex mutex;
121
122 inline static constexpr std::string_view emptyName = "";
123 inline static constexpr std::string_view beginReadOnlyTransactionMethodName = "beginReadOnlyTransaction";
124 inline static constexpr std::string_view beginTransactionMethodName = "beginTransaction";
125 inline static constexpr std::string_view abortTransactionMethodName = "abortTransaction";
126 inline static constexpr std::string_view commitTransactionMethodName = "commitTransaction";
127};
128
129}
130#include "operators.hpp"
131
149template <class K, class V>
150LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
151 if (opened())
152 throw Opened(name, "add storage " + storageName);
153
154 auto storage = new Storage<K, V>(this, storageName, duplicates);
155 std::pair<Storages::const_iterator, bool> pair = storages.emplace(storageName, static_cast<StorageCommon *>(storage));
156 if (!pair.second)
157 throw StorageDuplicate(name, storageName);
158
159 return storage;
160}
161
177template<class K, class V>
178LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
179 if (opened())
180 throw Opened(name, "add cache " + storageName);
181
182 auto cache = new Cache<K, V>(this, storageName, false);
183 std::pair<Storages::const_iterator, bool> pair = storages.emplace(storageName, static_cast<StorageCommon *>(cache));
184 if (!pair.second)
185 throw StorageDuplicate(name, storageName);
186
187 return cache;
188}
189
207template <class K, class V>
208LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& storageName) {
209 return static_cast<Storage<K, V>*>(storages.at(storageName));
210}
211
229template <class K, class V>
230LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& storageName) {
231 return static_cast<Cache<K, V>*>(storages.at(storageName));
232}
bool opened() const
Returns database state.
Definition base.cpp:149
Transaction beginReadOnlyTransaction() const
Begins read-only transaction.
Definition base.cpp:195
LMDBAL::Cache< K, V > * getCache(const std::string &storageName)
Returns LMDBAL::Cache handle.
Definition base.h:230
void drop()
Drops the database.
Definition base.cpp:161
WriteTransaction beginTransaction()
Begins writable transaction.
Definition base.cpp:218
QString getName() const
Returns database name.
Definition base.cpp:132
LMDBAL::Cache< K, V > * addCache(const std::string &storageName)
Adds LMDBAL::Cache to the database.
Definition base.h:178
LMDBAL::Storage< K, V > * addStorage(const std::string &storageName, bool duplicates=false)
Adds LMDBAL::Storage to the database.
Definition base.h:150
LMDBAL::Storage< K, V > * getStorage(const std::string &storageName)
Returns LMDBAL::Storage handle.
Definition base.h:208
bool removeDirectory()
Removes database directory.
Definition base.cpp:85
~Base()
Destroys the database.
Definition base.cpp:60
Base(const QString &path, const QString &name=QString(), uint16_t mapSize=10)
Creates the database.
Definition base.cpp:43
QString getPath() const
Returns database name.
Definition base.cpp:140
bool createDirectory()
Creates database directory.
Definition base.cpp:111
Storage with additional caching in std::map.
Definition cache.h:30
Thrown if something in the database was called on opened state and it is not supported.
Definition exceptions.h:118
A class handling serialization/deserialization.
Definition serializer.h:35
RAII guard that keeps the database environment active.
Definition session.h:25
Storage interface.
Definition storagecommon.h:34
Thrown if there was attempt to define storages with conflicting names.
Definition exceptions.h:154
This is a basic key value storage.
Definition storage.h:38
Public read only transaction.
Definition transaction.h:27
Public writable transaction.
Definition transaction.h:54
Destroys a cache.
Definition base.h:36
MDB_txn * TransactionID
I'm going to use transaction pointers as transaction IDs.
Definition base.h:52
uint32_t SizeType
All LMDBAL sizes are uint32.
Definition base.h:53