lmdbal/src/base.h

149 lines
3.9 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;
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) const;
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*> Tables;
typedef std::set<TransactionID> Transactions;
TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
TransactionID beginTransaction(const std::string& storageName) const;
void commitTransaction(TransactionID id, const std::string& storageName) const;
void abortTransaction(TransactionID id, const std::string& storageName) const;
private:
std::string name;
bool opened;
uint16_t size;
MDB_env* environment;
Tables tables;
Transactions* transactions;
inline static const std::string emptyName = "";
};
}
#include "operators.hpp"
/**
* Adds LMDBAL::Storage with the given name to the LMDBAL::Base, also returns it. The LMDBAL::Base must be closed
*/
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& p_name) {
if (opened) {
throw Opened(name, "add table " + p_name);
}
Storage<K, V>* table = new Storage<K, V>(p_name, this);
tables.insert(std::make_pair(p_name, (iStorage*)table));
return table;
}
/**
* Adds LMDBAL::Cache with given the name to the LMDBAL::Base, also returns it. The LMDBAL::Base must be closed
*/
template<class K, class V>
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& p_name) {
if (opened) {
throw Opened(name, "add cache " + p_name);
}
Cache<K, V>* cache = new Cache<K, V>(p_name, this);
tables.insert(std::make_pair(p_name, (iStorage*)cache));
return cache;
}
/**
* Returns LMDBAL::Storage with the given name
*/
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::getStorage(const std::string& p_name) {
return static_cast<Storage<K, V>*>(tables.at(p_name));
}
/**
* Returns LMDBAL::Cache with the given name
*/
template <class K, class V>
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& p_name) {
return static_cast<Cache<K, V>*>(tables.at(p_name));
}
#endif //LMDBAL_BASE_H