forked from blue/lmdbal
212 lines
7.6 KiB
C++
212 lines
7.6 KiB
C++
/*
|
|
* 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/>.
|
|
*/
|
|
|
|
#ifndef LMDBAL_BASE_H
|
|
#define LMDBAL_BASE_H
|
|
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <optional>
|
|
#include <limits>
|
|
|
|
#include <QString>
|
|
#include <QStandardPaths>
|
|
#include <QDir>
|
|
|
|
#include <lmdb.h>
|
|
|
|
#include "exceptions.h"
|
|
|
|
namespace LMDBAL {
|
|
|
|
class iStorage;
|
|
|
|
template<class T>
|
|
class Serializer;
|
|
|
|
template <class K, class V>
|
|
class Storage;
|
|
|
|
template <class K, class V>
|
|
class Cache;
|
|
|
|
typedef MDB_txn* TransactionID; /**<I'm going to use transaction pointers as transaction IDs*/
|
|
|
|
class Base {
|
|
friend class iStorage;
|
|
public:
|
|
|
|
Base(const QString& name, uint16_t mapSize = 10);
|
|
~Base();
|
|
|
|
void open();
|
|
void close();
|
|
bool ready() const;
|
|
bool removeDirectory();
|
|
QString createDirectory();
|
|
QString getName() const;
|
|
void drop();
|
|
|
|
TransactionID beginReadOnlyTransaction() const;
|
|
TransactionID beginTransaction() const;
|
|
void commitTransaction(TransactionID id);
|
|
void abortTransaction(TransactionID id) const;
|
|
|
|
template <class K, class V>
|
|
LMDBAL::Storage<K, V>* addStorage(const std::string& name);
|
|
|
|
template <class K, class V>
|
|
LMDBAL::Cache<K, V>* addCache(const std::string& name);
|
|
|
|
template <class K, class V>
|
|
LMDBAL::Storage<K, V>* getStorage(const std::string& name);
|
|
|
|
template <class K, class V>
|
|
LMDBAL::Cache<K, V>* getCache(const std::string& name);
|
|
|
|
private:
|
|
typedef std::map<std::string, LMDBAL::iStorage*> Storages; /**<Storage and Cache pointers are saved in the std::map*/
|
|
typedef std::set<TransactionID> Transactions; /**<Piblic transaction IDs are saved in the std::set*/
|
|
|
|
TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
|
|
TransactionID beginTransaction(const std::string& storageName) const;
|
|
void commitTransaction(TransactionID id, const std::string& storageName);
|
|
void abortTransaction(TransactionID id, const std::string& storageName) const;
|
|
|
|
TransactionID beginPrivateReadOnlyTransaction(const std::string& storageName) const;
|
|
TransactionID beginPrivateTransaction(const std::string& storageName) const;
|
|
void commitPrivateTransaction(TransactionID id, const std::string& storageName);
|
|
void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
|
|
|
|
private:
|
|
std::string name; /**<\brief Name of this database*/
|
|
bool opened; /**<\brief State of this database*/
|
|
uint16_t size; /**<\brief lmdb map size in MiB*/
|
|
MDB_env* environment; /**<\brief lmdb environment handle*/
|
|
Storages storages; /**<\brief Registered storages and caches*/
|
|
Transactions* transactions; /**<\brief Active public transactions*/
|
|
|
|
inline static const std::string emptyName = ""; /**<\brief Empty string for general fallback purposes*/
|
|
};
|
|
|
|
}
|
|
#include "operators.hpp"
|
|
|
|
/**
|
|
* \brief Adds LMDBAL::Storage to the database
|
|
*
|
|
* Defines that the database is going to have the following storage.
|
|
* The LMDBAL::Base must be closed
|
|
*
|
|
* \param[in] _name - storage name
|
|
* \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
|
|
*/
|
|
template <class K, class V>
|
|
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& _name) {
|
|
if (opened) {
|
|
throw Opened(name, "add storage " + _name);
|
|
}
|
|
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));
|
|
if (!pair.second)
|
|
throw StorageDuplicate(name, _name);
|
|
|
|
return storage;
|
|
}
|
|
|
|
/**
|
|
* \brief Adds LMDBAL::Cache to the database
|
|
*
|
|
* Defines that the database is going to have the following cache.
|
|
* The LMDBAL::Base must be closed
|
|
*
|
|
* \param[in] _name - cache name
|
|
* \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
|
|
*/
|
|
template<class K, class V>
|
|
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& _name) {
|
|
if (opened) {
|
|
throw Opened(name, "add cache " + _name);
|
|
}
|
|
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));
|
|
if (!pair.second)
|
|
throw StorageDuplicate(name, _name);
|
|
|
|
return cache;
|
|
}
|
|
|
|
/**
|
|
* \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
|
|
*
|
|
* \param[in] _name - storage name
|
|
* \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
|
|
*/
|
|
template <class K, class V>
|
|
LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& _name) {
|
|
return static_cast<Storage<K, V>*>(storages.at(_name));
|
|
}
|
|
|
|
/**
|
|
* \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
|
|
*
|
|
* \param[in] _name - cache name
|
|
* \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
|
|
*/
|
|
template <class K, class V>
|
|
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& _name) {
|
|
return static_cast<Cache<K, V>*>(storages.at(_name));
|
|
}
|
|
|
|
#endif //LMDBAL_BASE_H
|