111 lines
3.9 KiB
C++
111 lines
3.9 KiB
C++
// 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/>.
|
|
|
|
#ifndef LMDBDATABASE_STORAGE_H
|
|
#define LMDBDATABASE_STORAGE_H
|
|
|
|
#include "database.h"
|
|
#include "serializer.h"
|
|
|
|
namespace LMDBDataBase {
|
|
|
|
class StorageBase {
|
|
friend class DataBase;
|
|
protected:
|
|
StorageBase(const std::string& name, DataBase* parent);
|
|
virtual ~StorageBase();
|
|
|
|
virtual int createTable(MDB_txn * transaction) = 0;
|
|
virtual int drop(MDB_txn * transaction);
|
|
|
|
bool isDBOpened() const;
|
|
const std::string& dbName() const;
|
|
|
|
void ensureOpened(const std::string& methodName) const;
|
|
void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const;
|
|
void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const;
|
|
void throwUnknown(int rc, TransactionID txn) const;
|
|
void throwUnknown(int rc) const;
|
|
void throwDuplicate(const std::string& key) const;
|
|
void throwNotFound(const std::string& key)const ;
|
|
|
|
public:
|
|
virtual void drop();
|
|
virtual uint32_t count() const;
|
|
|
|
TransactionID beginReadOnlyTransaction() const;
|
|
TransactionID beginTransaction() const;
|
|
void commitTransaction(TransactionID id) const;
|
|
void abortTransaction(TransactionID id) const;
|
|
|
|
protected:
|
|
MDB_dbi dbi;
|
|
DataBase* db;
|
|
const std::string name;
|
|
|
|
inline static const std::string dropMethodName = "drop";
|
|
inline static const std::string countMethodName = "count";
|
|
|
|
inline static const std::string addRecordMethodName = "addRecord";
|
|
inline static const std::string forceRecordMethodName = "forceRecord";
|
|
inline static const std::string changeRecordMethodName = "changeRecord";
|
|
inline static const std::string removeRecordMethodName = "removeRecord";
|
|
inline static const std::string checkRecordMethodName = "checkRecord";
|
|
inline static const std::string getRecordMethodName = "getRecord";
|
|
inline static const std::string readAllMethodName = "readAllRecord";
|
|
inline static const std::string replaceAllMethodName = "replaceAll";
|
|
inline static const std::string addRecordsMethodName = "addRecords";
|
|
|
|
protected:
|
|
template <class T>
|
|
int makeTable(MDB_txn* transaction);
|
|
|
|
template <class T>
|
|
static std::string toString(const T& value);
|
|
};
|
|
|
|
template <class K, class V>
|
|
class Storage : public StorageBase {
|
|
friend class DataBase;
|
|
protected:
|
|
Storage(const std::string& name, DataBase* parent);
|
|
~Storage() override;
|
|
|
|
public:
|
|
using StorageBase::drop;
|
|
virtual void addRecord(const K& key, const V& value);
|
|
virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change
|
|
virtual void changeRecord(const K& key, const V& value);
|
|
virtual void removeRecord(const K& key);
|
|
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
|
|
virtual V getRecord(const K& key) const;
|
|
virtual std::map<K, V> readAll() const;
|
|
virtual void replaceAll(const std::map<K, V>& data);
|
|
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
|
|
|
|
protected:
|
|
Serializer<K>* keySerializer;
|
|
Serializer<V>* valueSerializer;
|
|
|
|
int createTable(MDB_txn* transaction) override;
|
|
};
|
|
|
|
}
|
|
|
|
#include "storage.hpp"
|
|
|
|
#endif // LMDBDATABASE_STORAGE_H
|