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
27#include <QString>
28#include <QStandardPaths>
29#include <QDir>
30
31#include <lmdb.h>
32
33#include "exceptions.h"
34
35namespace LMDBAL {
36
37class iStorage;
38class Transaction;
39class WriteTransaction;
40
41template<class T>
42class Serializer;
43
44template <class K, class V>
45class Storage;
46
47template <class K, class V>
48class Cache;
49
50typedef MDB_txn* TransactionID;
51typedef uint32_t SizeType;
53class Base {
54 friend class iStorage;
55 friend class Transaction;
56 friend class WriteTransaction;
57public:
58
59 Base(const QString& name, uint16_t mapSize = 10);
60 ~Base();
61
62 void open();
63 void close();
64 bool ready() const;
65 bool removeDirectory();
66 QString createDirectory();
67 QString getName() const;
68 QString getPath() const;
69 void drop();
70
73
74 template <class K, class V>
75 LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
76
77 template <class K, class V>
78 LMDBAL::Cache<K, V>* addCache(const std::string& storageName);
79
80 template <class K, class V>
81 LMDBAL::Storage<K, V>* getStorage(const std::string& storageName);
82
83 template <class K, class V>
84 LMDBAL::Cache<K, V>* getCache(const std::string& storageName);
85
86private:
87 typedef std::map<std::string, LMDBAL::iStorage*> Storages;
88 typedef std::map<TransactionID, Transaction*> Transactions;
90 void commitTransaction(TransactionID id);
91 void abortTransaction(TransactionID id) const;
92 TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
93 TransactionID beginTransaction(const std::string& storageName) const;
94 void commitTransaction(TransactionID id, const std::string& storageName);
95 void abortTransaction(TransactionID id, const std::string& storageName) const;
96
97 TransactionID beginPrivateReadOnlyTransaction(const std::string& storageName) const;
98 TransactionID beginPrivateTransaction(const std::string& storageName) const;
99 void commitPrivateTransaction(TransactionID id, const std::string& storageName);
100 void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
101
102private:
103 std::string name;
104 bool opened;
105 uint16_t size;
106 MDB_env* environment;
107 Storages storages;
108 mutable Transactions transactions;
110 inline static const std::string emptyName = "";
111};
112
113}
114#include "operators.hpp"
115
133template <class K, class V>
134LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
135 if (opened)
136 throw Opened(name, "add storage " + storageName);
137
138 Storage<K, V>* storage = new Storage<K, V>(this, storageName, duplicates);
139 std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)storage));
140 if (!pair.second)
141 throw StorageDuplicate(name, storageName);
142
143 return storage;
144}
145
161template<class K, class V>
162LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
163 if (opened)
164 throw Opened(name, "add cache " + storageName);
165
166 Cache<K, V>* cache = new Cache<K, V>(this, storageName, false);
167 std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)cache));
168 if (!pair.second)
169 throw StorageDuplicate(name, storageName);
170
171 return cache;
172}
173
191template <class K, class V>
192LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& storageName) {
193 return static_cast<Storage<K, V>*>(storages.at(storageName));
194}
195
213template <class K, class V>
214LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& storageName) {
215 return static_cast<Cache<K, V>*>(storages.at(storageName));
216}
Database abstraction.
Definition base.h:53
void close()
Closes the database.
Definition base.cpp:67
Transaction beginReadOnlyTransaction() const
Begins read-only transaction.
Definition base.cpp:232
LMDBAL::Cache< K, V > * getCache(const std::string &storageName)
Returns LMDBAL::Cache handle.
Definition base.h:214
void drop()
Drops the database.
Definition base.cpp:198
WriteTransaction beginTransaction()
Begins writable transaction.
Definition base.cpp:255
QString getName() const
Returns database name.
Definition base.cpp:167
Base(const QString &name, uint16_t mapSize=10)
Creates the database.
Definition base.cpp:40
LMDBAL::Cache< K, V > * addCache(const std::string &storageName)
Adds LMDBAL::Cache to the database.
Definition base.h:162
LMDBAL::Storage< K, V > * addStorage(const std::string &storageName, bool duplicates=false)
Adds LMDBAL::Storage to the database.
Definition base.h:134
bool ready() const
Returns database state.
Definition base.cpp:187
void open()
Opens the database.
Definition base.cpp:92
LMDBAL::Storage< K, V > * getStorage(const std::string &storageName)
Returns LMDBAL::Storage handle.
Definition base.h:192
bool removeDirectory()
Removes database directory.
Definition base.cpp:120
~Base()
Destroys the database.
Definition base.cpp:52
QString getPath() const
Returns database name.
Definition base.cpp:175
QString createDirectory()
Creates database directory.
Definition base.cpp:147
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
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:134
Public read only transaction.
Definition transaction.h:26
Public writable transaction.
Definition transaction.h:50
Storage interface.
Definition storage.h:36