lmdbal/src/storage.h

136 lines
5.1 KiB
C
Raw Normal View History

/*
* 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/>.
*/
2022-09-05 20:25:39 +00:00
2023-03-21 11:05:54 +00:00
#ifndef LMDBAL_STORAGE_H
#define LMDBAL_STORAGE_H
2022-09-05 20:25:39 +00:00
2023-03-21 11:05:54 +00:00
#include "base.h"
2022-09-12 15:16:18 +00:00
#include "serializer.h"
2022-09-05 20:25:39 +00:00
2023-03-21 11:05:54 +00:00
namespace LMDBAL {
2023-03-20 15:37:13 +00:00
typedef uint32_t SizeType;
2023-03-21 11:05:54 +00:00
class iStorage {
friend class Base;
public:
protected:
2023-03-21 11:05:54 +00:00
iStorage(const std::string& name, Base* parent);
virtual ~iStorage();
2022-09-05 20:25:39 +00:00
2023-04-02 13:00:21 +00:00
virtual int createStorage(MDB_txn * transaction) = 0;
2023-03-20 15:37:13 +00:00
bool isDBOpened() const;
const std::string& dbName() const;
void ensureOpened(const std::string& methodName) const;
void throwDuplicateOrUnknown(int rc, const std::string& key) const;
2023-03-20 15:37:13 +00:00
void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const;
void throwNotFoundOrUnknown(int rc, const std::string& key) const;
2023-03-20 15:37:13 +00:00
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;
2023-04-02 13:00:21 +00:00
void throwNotFound(const std::string& key) const;
2023-03-20 15:37:13 +00:00
TransactionID beginReadOnlyTransaction() const;
TransactionID beginTransaction() const;
2023-04-03 18:48:13 +00:00
void commitTransaction(TransactionID id);
2023-03-20 15:37:13 +00:00
void abortTransaction(TransactionID id) const;
2023-04-03 18:48:13 +00:00
virtual void transactionStarted(TransactionID txn, bool readOnly) const;
virtual void transactionCommited(TransactionID txn);
virtual void transactionAborted(TransactionID txn) const;
2023-04-02 13:00:21 +00:00
public:
virtual void drop();
2023-04-03 18:48:13 +00:00
virtual int drop(TransactionID transaction);
2023-04-02 13:00:21 +00:00
virtual SizeType count() const;
virtual SizeType count(TransactionID txn) const;
2023-03-20 15:37:13 +00:00
protected:
2022-09-05 20:25:39 +00:00
MDB_dbi dbi;
2023-03-21 11:05:54 +00:00
Base* db;
2022-09-09 17:15:40 +00:00
const std::string name;
2022-09-14 22:18:31 +00:00
2023-03-20 15:37:13 +00:00
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";
2022-09-14 22:18:31 +00:00
protected:
template <class T>
int makeStorage(MDB_txn* transaction);
2022-09-14 22:18:31 +00:00
template <class T>
static std::string toString(const T& value);
2022-09-05 20:25:39 +00:00
};
template <class K, class V>
2023-03-21 11:05:54 +00:00
class Storage : public iStorage {
friend class Base;
2022-09-20 17:16:48 +00:00
protected:
2023-03-21 11:05:54 +00:00
Storage(const std::string& name, Base* parent);
2023-03-20 15:37:13 +00:00
~Storage() override;
2022-09-05 20:25:39 +00:00
public:
2023-03-21 11:05:54 +00:00
using iStorage::drop;
2022-09-20 17:16:48 +00:00
virtual void addRecord(const K& key, const V& value);
virtual void addRecord(const K& key, const V& value, TransactionID txn);
2022-12-13 22:57:49 +00:00
virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change
virtual bool forceRecord(const K& key, const V& value, TransactionID txn);
2022-09-20 17:16:48 +00:00
virtual void changeRecord(const K& key, const V& value);
virtual void changeRecord(const K& key, const V& value, TransactionID txn);
2022-09-20 17:16:48 +00:00
virtual void removeRecord(const K& key);
virtual void removeRecord(const K& key, TransactionID txn);
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
virtual bool checkRecord(const K& key, TransactionID txn) const;
virtual void getRecord(const K& key, V& value) const;
virtual void getRecord(const K& key, V& value, TransactionID txn) const;
2022-09-20 17:16:48 +00:00
virtual V getRecord(const K& key) const;
virtual V getRecord(const K& key, TransactionID txn) const;
virtual std::map<K, V> readAll() const;
virtual std::map<K, V> readAll(TransactionID txn) const;
virtual void readAll(std::map<K, V>& result) const;
virtual void readAll(std::map<K, V>& result, TransactionID txn) const;
virtual void replaceAll(const std::map<K, V>& data);
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn);
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
2022-09-12 15:16:18 +00:00
2022-09-20 17:16:48 +00:00
protected:
2022-09-14 22:18:31 +00:00
Serializer<K>* keySerializer;
Serializer<V>* valueSerializer;
2022-09-05 20:25:39 +00:00
2023-04-02 13:00:21 +00:00
int createStorage(MDB_txn* transaction) override;
2022-09-14 22:18:31 +00:00
};
2022-09-09 17:15:40 +00:00
2023-03-20 15:37:13 +00:00
}
#include "storage.hpp"
2022-09-05 20:25:39 +00:00
2023-03-21 11:05:54 +00:00
#endif //LMDBAL_STORAGE_H