2023-03-23 17:27:46 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2023-08-05 20:13:43 +00:00
|
|
|
#include "cursor.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
|
|
|
|
2023-04-01 14:45:20 +00:00
|
|
|
typedef uint32_t SizeType;
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
class iStorage {
|
|
|
|
friend class Base;
|
2023-04-01 14:45:20 +00:00
|
|
|
public:
|
|
|
|
|
2022-09-17 12:31:58 +00:00
|
|
|
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-08-05 20:13:43 +00:00
|
|
|
virtual int open(MDB_txn * transaction) = 0;
|
|
|
|
virtual void close();
|
2022-09-17 12:31:58 +00:00
|
|
|
|
2023-03-20 15:37:13 +00:00
|
|
|
bool isDBOpened() const;
|
|
|
|
const std::string& dbName() const;
|
|
|
|
|
|
|
|
void ensureOpened(const std::string& methodName) const;
|
2023-03-28 20:45:35 +00:00
|
|
|
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;
|
2023-03-28 20:45:35 +00:00
|
|
|
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;
|
2022-09-17 12:31:58 +00:00
|
|
|
|
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
|
|
|
|
2022-09-17 12:31:58 +00:00
|
|
|
protected:
|
2023-04-12 15:36:33 +00:00
|
|
|
MDB_dbi dbi; /**<\brief lmdb storage handle*/
|
|
|
|
Base* db; /**<\brief parent database pointer (borrowed)*/
|
|
|
|
const std::string name; /**<\brief this storage name*/
|
|
|
|
|
|
|
|
inline static const std::string dropMethodName = "drop"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string countMethodName = "count"; /**<\brief member function name, just for exceptions*/
|
|
|
|
|
|
|
|
inline static const std::string addRecordMethodName = "addRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string forceRecordMethodName = "forceRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string changeRecordMethodName = "changeRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string removeRecordMethodName = "removeRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string checkRecordMethodName = "checkRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string getRecordMethodName = "getRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string readAllMethodName = "readAllRecord"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string replaceAllMethodName = "replaceAll"; /**<\brief member function name, just for exceptions*/
|
|
|
|
inline static const std::string addRecordsMethodName = "addRecords"; /**<\brief member function name, just for exceptions*/
|
2023-03-20 15:37:13 +00:00
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
protected:
|
|
|
|
template <class T>
|
2023-04-10 21:01:19 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-08-05 20:13:43 +00:00
|
|
|
// template <class K, class V>
|
|
|
|
// class Cursor;
|
|
|
|
|
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;
|
2023-08-05 20:13:43 +00:00
|
|
|
friend class Cursor<K, V>;
|
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);
|
2023-03-28 20:45:35 +00:00
|
|
|
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
|
2023-03-28 20:45:35 +00:00
|
|
|
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);
|
2023-03-28 20:45:35 +00:00
|
|
|
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);
|
2023-03-28 20:45:35 +00:00
|
|
|
virtual void removeRecord(const K& key, TransactionID txn);
|
2022-12-15 17:29:49 +00:00
|
|
|
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
|
2023-03-28 20:45:35 +00:00
|
|
|
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;
|
2023-03-28 20:45:35 +00:00
|
|
|
virtual V getRecord(const K& key, TransactionID txn) const;
|
2022-10-15 10:54:34 +00:00
|
|
|
virtual std::map<K, V> readAll() const;
|
2023-03-28 20:45:35 +00:00
|
|
|
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;
|
2022-12-18 14:45:12 +00:00
|
|
|
virtual void replaceAll(const std::map<K, V>& data);
|
2023-03-28 20:45:35 +00:00
|
|
|
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn);
|
2022-12-18 14:45:12 +00:00
|
|
|
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
|
2023-03-28 20:45:35 +00:00
|
|
|
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
|
2022-09-12 15:16:18 +00:00
|
|
|
|
2023-08-05 20:13:43 +00:00
|
|
|
Cursor<K, V>* createCursor();
|
|
|
|
void destroyCursor(Cursor<K, V>* cursor);
|
|
|
|
|
2022-09-20 17:16:48 +00:00
|
|
|
protected:
|
2023-08-05 20:13:43 +00:00
|
|
|
mutable Serializer<K> keySerializer; /**<\brief internal object that would serialize and deserialize keys*/
|
|
|
|
mutable Serializer<V> valueSerializer; /**<\brief internal object that would serialize and deserialize values*/
|
|
|
|
std::set<Cursor<K,V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-08-05 20:13:43 +00:00
|
|
|
int open(MDB_txn* transaction) override;
|
|
|
|
void close() 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
|