2023-03-23 17:27:46 +00:00
|
|
|
/*
|
|
|
|
* LMDB Abstraction Layer.
|
|
|
|
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
#ifndef LMDBAL_BASE_H
|
|
|
|
#define LMDBAL_BASE_H
|
2022-09-05 20:25:39 +00:00
|
|
|
|
|
|
|
#include <map>
|
2023-03-20 15:37:13 +00:00
|
|
|
#include <set>
|
2022-09-05 20:25:39 +00:00
|
|
|
#include <string>
|
2022-10-10 20:51:48 +00:00
|
|
|
#include <optional>
|
2023-03-20 15:37:13 +00:00
|
|
|
#include <limits>
|
2022-09-05 20:25:39 +00:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QDir>
|
2023-03-20 15:37:13 +00:00
|
|
|
|
2022-09-05 20:25:39 +00:00
|
|
|
#include <lmdb.h>
|
|
|
|
|
2023-03-20 15:37:13 +00:00
|
|
|
#include "exceptions.h"
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
namespace LMDBAL {
|
2023-03-20 15:37:13 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
class iStorage;
|
2023-03-20 15:37:13 +00:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class Serializer;
|
|
|
|
|
|
|
|
template <class K, class V>
|
|
|
|
class Storage;
|
|
|
|
|
|
|
|
template <class K, class V>
|
|
|
|
class Cache;
|
|
|
|
|
|
|
|
typedef MDB_txn* TransactionID;
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
class Base {
|
|
|
|
friend class iStorage;
|
2022-09-05 20:25:39 +00:00
|
|
|
public:
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
Base(const QString& name, uint16_t mapSize = 10);
|
|
|
|
~Base();
|
2022-09-05 20:25:39 +00:00
|
|
|
|
|
|
|
void open();
|
|
|
|
void close();
|
2022-09-15 21:34:39 +00:00
|
|
|
bool ready() const;
|
|
|
|
bool removeDirectory();
|
2022-12-17 10:06:37 +00:00
|
|
|
QString createDirectory();
|
2022-09-05 20:25:39 +00:00
|
|
|
QString getName() const;
|
2022-09-17 12:31:58 +00:00
|
|
|
void drop();
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-03-20 15:37:13 +00:00
|
|
|
TransactionID beginReadOnlyTransaction() const;
|
|
|
|
TransactionID beginTransaction() const;
|
2023-04-03 18:48:13 +00:00
|
|
|
void commitTransaction(TransactionID id);
|
2023-03-20 15:37:13 +00:00
|
|
|
void abortTransaction(TransactionID id) const;
|
2022-12-17 10:06:37 +00:00
|
|
|
|
2022-09-05 20:25:39 +00:00
|
|
|
template <class K, class V>
|
2023-03-23 17:27:46 +00:00
|
|
|
LMDBAL::Storage<K, V>* addStorage(const std::string& name);
|
2022-09-15 21:34:39 +00:00
|
|
|
|
2022-09-20 17:16:48 +00:00
|
|
|
template <class K, class V>
|
2023-03-23 17:27:46 +00:00
|
|
|
LMDBAL::Cache<K, V>* addCache(const std::string& name);
|
2022-09-20 17:16:48 +00:00
|
|
|
|
2022-09-15 21:34:39 +00:00
|
|
|
template <class K, class V>
|
2023-03-23 17:27:46 +00:00
|
|
|
LMDBAL::Storage<K, V>* getStorage(const std::string& name);
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2022-09-20 17:16:48 +00:00
|
|
|
template <class K, class V>
|
2023-03-23 17:27:46 +00:00
|
|
|
LMDBAL::Cache<K, V>* getCache(const std::string& name);
|
2022-09-20 17:16:48 +00:00
|
|
|
|
2023-03-20 15:37:13 +00:00
|
|
|
private:
|
2023-04-02 13:00:21 +00:00
|
|
|
typedef std::map<std::string, LMDBAL::iStorage*> Storages;
|
2023-03-20 15:37:13 +00:00
|
|
|
typedef std::set<TransactionID> Transactions;
|
|
|
|
|
|
|
|
TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
|
|
|
|
TransactionID beginTransaction(const std::string& storageName) const;
|
2023-04-03 18:48:13 +00:00
|
|
|
void commitTransaction(TransactionID id, const std::string& storageName);
|
2023-03-20 15:37:13 +00:00
|
|
|
void abortTransaction(TransactionID id, const std::string& storageName) const;
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-04-02 13:00:21 +00:00
|
|
|
TransactionID beginPrivateReadOnlyTransaction(const std::string& storageName) const;
|
|
|
|
TransactionID beginPrivateTransaction(const std::string& storageName) const;
|
2023-04-03 18:48:13 +00:00
|
|
|
void commitPrivateTransaction(TransactionID id, const std::string& storageName);
|
2023-04-02 13:00:21 +00:00
|
|
|
void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
|
|
|
|
|
2022-09-05 20:25:39 +00:00
|
|
|
private:
|
|
|
|
std::string name;
|
|
|
|
bool opened;
|
|
|
|
uint16_t size;
|
|
|
|
MDB_env* environment;
|
2023-04-02 13:00:21 +00:00
|
|
|
Storages storages;
|
2023-03-20 15:37:13 +00:00
|
|
|
Transactions* transactions;
|
|
|
|
|
|
|
|
inline static const std::string emptyName = "";
|
2022-09-05 20:25:39 +00:00
|
|
|
};
|
|
|
|
|
2023-03-20 15:37:13 +00:00
|
|
|
}
|
2022-12-18 14:45:12 +00:00
|
|
|
#include "operators.hpp"
|
2022-09-14 22:18:31 +00:00
|
|
|
|
2023-03-23 17:27:46 +00:00
|
|
|
/**
|
2023-04-10 21:01:19 +00:00
|
|
|
* \brief Adds LMDBAL::Storage to the database
|
|
|
|
*
|
|
|
|
* Defines that the database is going to have the following storage.
|
|
|
|
* The LMDBAL::Base must be closed
|
|
|
|
*
|
2023-04-11 15:11:27 +00:00
|
|
|
* \param[in] _name - storage name
|
2023-04-10 21:01:19 +00:00
|
|
|
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
|
|
|
|
*
|
|
|
|
* \tparam K - key type of the storage
|
|
|
|
* \tparam V - value type of the storage
|
|
|
|
*
|
|
|
|
* \exception LMDBAL::Opened thrown if this method is called on the opened database
|
|
|
|
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add storage with repeating name
|
2023-03-23 17:27:46 +00:00
|
|
|
*/
|
2022-09-14 22:18:31 +00:00
|
|
|
template <class K, class V>
|
2023-04-11 15:11:27 +00:00
|
|
|
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& _name) {
|
2022-09-14 22:18:31 +00:00
|
|
|
if (opened) {
|
2023-04-11 15:11:27 +00:00
|
|
|
throw Opened(name, "add storage " + _name);
|
2022-09-14 22:18:31 +00:00
|
|
|
}
|
2023-04-11 15:11:27 +00:00
|
|
|
Storage<K, V>* storage = new Storage<K, V>(_name, this);
|
|
|
|
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(_name, (iStorage*)storage));
|
2023-04-10 21:01:19 +00:00
|
|
|
if (!pair.second)
|
2023-04-11 15:11:27 +00:00
|
|
|
throw StorageDuplicate(name, _name);
|
2023-04-10 21:01:19 +00:00
|
|
|
|
2023-04-02 13:00:21 +00:00
|
|
|
return storage;
|
2022-09-05 20:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 17:27:46 +00:00
|
|
|
/**
|
2023-04-10 21:01:19 +00:00
|
|
|
* \brief Adds LMDBAL::Cache to the database
|
|
|
|
*
|
|
|
|
* Defines that the database is going to have the following cache.
|
|
|
|
* The LMDBAL::Base must be closed
|
|
|
|
*
|
2023-04-11 15:11:27 +00:00
|
|
|
* \param[in] _name - cache name
|
2023-04-10 21:01:19 +00:00
|
|
|
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it
|
|
|
|
*
|
|
|
|
* \tparam K - key type of the cache
|
|
|
|
* \tparam V - value type of the cahce
|
|
|
|
*
|
|
|
|
* \exception LMDBAL::Opened thrown if this method is called on the opened database
|
|
|
|
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add cache with repeating name
|
2023-03-23 17:27:46 +00:00
|
|
|
*/
|
2022-09-20 17:16:48 +00:00
|
|
|
template<class K, class V>
|
2023-04-11 15:11:27 +00:00
|
|
|
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& _name) {
|
2022-09-20 17:16:48 +00:00
|
|
|
if (opened) {
|
2023-04-11 15:11:27 +00:00
|
|
|
throw Opened(name, "add cache " + _name);
|
2022-09-20 17:16:48 +00:00
|
|
|
}
|
2023-04-11 15:11:27 +00:00
|
|
|
Cache<K, V>* cache = new Cache<K, V>(_name, this);
|
|
|
|
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(_name, (iStorage*)cache));
|
2023-04-10 21:01:19 +00:00
|
|
|
if (!pair.second)
|
2023-04-11 15:11:27 +00:00
|
|
|
throw StorageDuplicate(name, _name);
|
2023-04-10 21:01:19 +00:00
|
|
|
|
2022-09-20 17:16:48 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:27:46 +00:00
|
|
|
/**
|
2023-04-10 21:01:19 +00:00
|
|
|
* \brief Returns LMDBAL::Storage handle
|
|
|
|
*
|
|
|
|
* Requested storage must have been added before opening database
|
|
|
|
* Note that template parameters is user responsibility zone!
|
|
|
|
* If user, for instance, had added storage <int, int> but calling
|
|
|
|
* this method with template parameters <std::string, std::string>
|
|
|
|
* on the same name of the previously added storage, or calling it on cache - the behaviour is undefined
|
|
|
|
*
|
2023-04-11 15:11:27 +00:00
|
|
|
* \param[in] _name - storage name
|
2023-04-10 21:01:19 +00:00
|
|
|
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
|
|
|
|
*
|
|
|
|
* \tparam K - key type of the storage
|
|
|
|
* \tparam V - value type of the storage
|
|
|
|
*
|
|
|
|
* \exception std::out_of_range thrown if storage with the given name was not found
|
2023-03-23 17:27:46 +00:00
|
|
|
*/
|
2022-09-15 21:34:39 +00:00
|
|
|
template <class K, class V>
|
2023-04-11 15:11:27 +00:00
|
|
|
LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& _name) {
|
|
|
|
return static_cast<Storage<K, V>*>(storages.at(_name));
|
2022-09-15 21:34:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 17:27:46 +00:00
|
|
|
/**
|
2023-04-10 21:01:19 +00:00
|
|
|
* \brief Returns LMDBAL::Cache handle
|
|
|
|
*
|
|
|
|
* Requested cache must have been added before opening database
|
|
|
|
* Note that template parameters is user responsibility zone!
|
|
|
|
* If user, for instance, had added cache <int, int> but calling
|
|
|
|
* this method with template parameters <std::string, std::string>
|
|
|
|
* on the same name of the previously added cache, or calling it on storage - the behaviour is undefined
|
|
|
|
*
|
2023-04-11 15:11:27 +00:00
|
|
|
* \param[in] _name - cache name
|
2023-04-10 21:01:19 +00:00
|
|
|
* \returns cache pointer. LMDBAL::Base keeps the ownership of the added cache, you don't need to destoroy it
|
|
|
|
*
|
|
|
|
* \tparam K - key type of the cache
|
|
|
|
* \tparam V - value type of the cahce
|
|
|
|
*
|
|
|
|
* \exception std::out_of_range thrown if cache with the given name was not found
|
2023-03-23 17:27:46 +00:00
|
|
|
*/
|
2022-09-20 17:16:48 +00:00
|
|
|
template <class K, class V>
|
2023-04-11 15:11:27 +00:00
|
|
|
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& _name) {
|
|
|
|
return static_cast<Cache<K, V>*>(storages.at(_name));
|
2022-09-20 17:16:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
#endif //LMDBAL_BASE_H
|