1
0
forked from blue/lmdbal
lmdbal/database.h

135 lines
3.7 KiB
C
Raw Normal View History

2022-09-05 20:25:39 +00:00
// Squawk messenger.
// Copyright (C) 2019 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/>.
2023-03-20 15:37:13 +00:00
#ifndef LMDBDATABASE_DATABASE_H
#define LMDBDATABASE_DATABASE_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>
#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"
namespace LMDBDataBase {
class StorageBase;
template<class T>
class Serializer;
template <class K, class V>
class Storage;
template <class K, class V>
class Cache;
typedef MDB_txn* TransactionID;
class DataBase {
friend class StorageBase;
2022-09-05 20:25:39 +00:00
public:
DataBase(const QString& name, uint16_t mapSize = 10);
~DataBase();
void open();
void close();
bool ready() const;
bool removeDirectory();
QString createDirectory();
2022-09-05 20:25:39 +00:00
QString getName() const;
void drop();
2022-09-05 20:25:39 +00:00
2023-03-20 15:37:13 +00:00
TransactionID beginReadOnlyTransaction() const;
TransactionID beginTransaction() const;
void commitTransaction(TransactionID id) const;
void abortTransaction(TransactionID id) const;
2022-09-05 20:25:39 +00:00
template <class K, class V>
2023-03-20 15:37:13 +00:00
Storage<K, V>* addTable(const std::string& name);
2022-09-20 17:16:48 +00:00
template <class K, class V>
Cache<K, V>* addCache(const std::string& name);
template <class K, class V>
2023-03-20 15:37:13 +00:00
Storage<K, V>* getTable(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>
Cache<K, V>* getCache(const std::string& name);
2023-03-20 15:37:13 +00:00
private:
typedef std::map<std::string, StorageBase*> 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;
2022-09-05 20:25:39 +00:00
private:
std::string name;
bool opened;
uint16_t size;
MDB_env* environment;
2023-03-20 15:37:13 +00:00
Tables tables;
Transactions* transactions;
inline static const std::string emptyName = "";
2022-09-05 20:25:39 +00:00
};
2023-03-20 15:37:13 +00:00
}
#include "operators.hpp"
2022-09-14 22:18:31 +00:00
template <class K, class V>
2023-03-20 15:37:13 +00:00
LMDBDataBase::Storage<K, V>* LMDBDataBase::DataBase::addTable(const std::string& p_name) {
2022-09-14 22:18:31 +00:00
if (opened) {
throw Opened(name, "add table " + p_name);
2022-09-14 22:18:31 +00:00
}
2023-03-20 15:37:13 +00:00
Storage<K, V>* table = new Storage<K, V>(p_name, this);
tables.insert(std::make_pair(p_name, (StorageBase*)table));
2022-09-14 22:18:31 +00:00
return table;
2022-09-05 20:25:39 +00:00
}
2022-09-20 17:16:48 +00:00
template<class K, class V>
2023-03-20 15:37:13 +00:00
LMDBDataBase::Cache<K, V> * LMDBDataBase::DataBase::addCache(const std::string& p_name) {
2022-09-20 17:16:48 +00:00
if (opened) {
throw Opened(name, "add cache " + p_name);
}
2023-03-20 15:37:13 +00:00
Cache<K, V>* cache = new Cache<K, V>(p_name, this);
tables.insert(std::make_pair(p_name, (StorageBase*)cache));
2022-09-20 17:16:48 +00:00
return cache;
}
template <class K, class V>
2023-03-20 15:37:13 +00:00
LMDBDataBase::Storage<K, V>* LMDBDataBase::DataBase::getTable(const std::string& p_name) {
return static_cast<Storage<K, V>*>(tables.at(p_name));
}
2022-09-20 17:16:48 +00:00
template <class K, class V>
2023-03-20 15:37:13 +00:00
LMDBDataBase::Cache<K, V>* LMDBDataBase::DataBase::getCache(const std::string& p_name) {
return static_cast<Cache<K, V>*>(tables.at(p_name));
2022-09-20 17:16:48 +00:00
}
2023-03-20 15:37:13 +00:00
#endif // LMDBDATABASE_DATABASE_H