/* * LMDB Abstraction Layer. * Copyright (C) 2023 Yury Gubich * * 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 . */ #ifndef LMDBAL_STORAGE_H #define LMDBAL_STORAGE_H #include "base.h" #include "serializer.h" namespace LMDBAL { class iStorage { friend class Base; protected: iStorage(const std::string& name, Base* parent); virtual ~iStorage(); 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; Base* 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 int makeTable(MDB_txn* transaction); template static std::string toString(const T& value); }; template class Storage : public iStorage { friend class Base; protected: Storage(const std::string& name, Base* parent); ~Storage() override; public: using iStorage::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 readAll() const; virtual void replaceAll(const std::map& data); virtual uint32_t addRecords(const std::map& data, bool overwrite = false); protected: Serializer* keySerializer; Serializer* valueSerializer; int createTable(MDB_txn* transaction) override; }; } #include "storage.hpp" #endif //LMDBAL_STORAGE_H