LMDBAL 0.6.0
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 <optional>
25#include <limits>
26#include <cstdint>
27#include <mutex>
28
29#include <QString>
30#include <QStandardPaths>
31#include <QDir>
32
33#include <lmdb.h>
34
35#include "exceptions.h"
36
37namespace LMDBAL {
38
39class StorageCommon;
40class Transaction;
42class Session;
43
44template<class T>
45class Serializer;
46
47template <class K, class V>
48class Storage;
49
50template <class K, class V>
51class Cache;
52
53typedef MDB_txn* TransactionID;
54typedef uint32_t SizeType;
55
56class Base {
57 friend class StorageCommon;
58 friend class Transaction;
59 friend class WriteTransaction;
60 friend class Session;
61
62public:
63 Base(const QString& name, uint16_t mapSize = 10);
64 ~Base();
65
66 Session open();
67 bool opened() const;
68 bool removeDirectory();
69 QString createDirectory();
70 QString getName() const;
71 QString getPath() const;
72 void drop();
73
74 Transaction beginReadOnlyTransaction() const;
75 WriteTransaction beginTransaction();
76
77 template <class K, class V>
78 LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
79
80 template <class K, class V>
81 LMDBAL::Cache<K, V>* addCache(const std::string& storageName);
82
83 template <class K, class V>
84 LMDBAL::Storage<K, V>* getStorage(const std::string& storageName);
85
86 template <class K, class V>
87 LMDBAL::Cache<K, V>* getCache(const std::string& storageName);
88
89private:
90 typedef std::map<std::string, LMDBAL::StorageCommon *> Storages;
91 typedef std::map<TransactionID, Transaction*> Transactions;
92 typedef std::set<Session*> Sessions;
93
94 void commitTransaction(TransactionID id);
95 void abortTransaction(TransactionID id) const;
96 TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
97 TransactionID beginTransaction(const std::string& storageName) const;
98 void commitTransaction(TransactionID id, const std::string& storageName);
99 void abortTransaction(TransactionID id, const std::string& storageName) const;
100
101 TransactionID beginPrivateReadOnlyTransaction(const std::string& storageName) const;
102 TransactionID beginPrivateTransaction(const std::string& storageName) const;
103 void commitPrivateTransaction(TransactionID id, const std::string& storageName);
104 void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
105
106 void registerSession(Session* session);
107 void unregisterSession(Session* session);
108 void replaceSession(Session* closing, Session* opening);
109
110 void activate();
111 void deactivate();
112
113private:
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 const std::string emptyName = "";
123};
124
125}
126#include "operators.hpp"
127
145template <class K, class V>
146LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
147 if (opened())
148 throw Opened(name, "add storage " + storageName);
149
150 auto storage = new Storage<K, V>(this, storageName, duplicates);
151 std::pair<Storages::const_iterator, bool> pair = storages.emplace(storageName, static_cast<StorageCommon *>(storage));
152 if (!pair.second)
153 throw StorageDuplicate(name, storageName);
154
155 return storage;
156}
157
173template<class K, class V>
174LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
175 if (opened())
176 throw Opened(name, "add cache " + storageName);
177
178 auto cache = new Cache<K, V>(this, storageName, false);
179 std::pair<Storages::const_iterator, bool> pair = storages.emplace(storageName, static_cast<StorageCommon *>(cache));
180 if (!pair.second)
181 throw StorageDuplicate(name, storageName);
182
183 return cache;
184}
185
203template <class K, class V>
204LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& storageName) {
205 return static_cast<Storage<K, V>*>(storages.at(storageName));
206}
207
225template <class K, class V>
226LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& storageName) {
227 return static_cast<Cache<K, V>*>(storages.at(storageName));
228}
bool opened() const
Returns database state.
Definition base.cpp:147
Transaction beginReadOnlyTransaction() const
Begins read-only transaction.
Definition base.cpp:193
LMDBAL::Cache< K, V > * getCache(const std::string &storageName)
Returns LMDBAL::Cache handle.
Definition base.h:226
void drop()
Drops the database.
Definition base.cpp:159
WriteTransaction beginTransaction()
Begins writable transaction.
Definition base.cpp:216
QString getName() const
Returns database name.
Definition base.cpp:127
Base(const QString &name, uint16_t mapSize=10)
Creates the database.
Definition base.cpp:42
LMDBAL::Cache< K, V > * addCache(const std::string &storageName)
Adds LMDBAL::Cache to the database.
Definition base.h:174
LMDBAL::Storage< K, V > * addStorage(const std::string &storageName, bool duplicates=false)
Adds LMDBAL::Storage to the database.
Definition base.h:146
LMDBAL::Storage< K, V > * getStorage(const std::string &storageName)
Returns LMDBAL::Storage handle.
Definition base.h:204
bool removeDirectory()
Removes database directory.
Definition base.cpp:80
~Base()
Destroys the database.
Definition base.cpp:55
QString getPath() const
Returns database name.
Definition base.cpp:135
QString createDirectory()
Creates database directory.
Definition base.cpp:107
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:119
A class handling serialization/deserialization.
Definition serializer.h:33
Definition session.h:25
Storage interface.
Definition storagecommon.h:33
Thrown if there was attempt to define storages with conflicting names.
Definition exceptions.h:159
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