lmdbal/src/storage.h

184 lines
8.9 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-08-15 18:48:19 +00:00
#include <type_traits>
#include <cstring>
2023-08-15 18:48:19 +00:00
2023-03-21 11:05:54 +00:00
#include "base.h"
2022-09-12 15:16:18 +00:00
#include "serializer.h"
#include "cursor.h"
2023-10-17 21:06:11 +00:00
#include "transaction.h"
2022-09-05 20:25:39 +00:00
2023-08-15 18:48:19 +00:00
class BaseTest;
2023-08-17 14:45:11 +00:00
class DuplicatesTest;
class CacheCursorTest;
class StorageCursorTest;
2023-08-15 18:48:19 +00:00
2023-03-21 11:05:54 +00:00
namespace LMDBAL {
2023-03-20 15:37:13 +00:00
2023-03-21 11:05:54 +00:00
class iStorage {
friend class Base;
public:
protected:
iStorage(Base* parent, const std::string& name, bool duplicates = false);
2023-03-21 11:05:54 +00:00
virtual ~iStorage();
2022-09-05 20:25:39 +00:00
2023-08-13 17:53:07 +00:00
/**
* \brief A private virtual function I need to open each storage in the database
*
* \param[in] transaction - lmdb transaction to call <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a">mdb_dbi_open</a>
* \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code
*/
virtual int open(MDB_txn * transaction) = 0;
virtual void close();
2023-10-17 21:06:11 +00:00
virtual void handleDrop();
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 throwUnknown(const std::string& message) const;
2023-03-20 15:37:13 +00:00
void throwDuplicate(const std::string& key) const;
2023-04-02 13:00:21 +00:00
void throwNotFound(const std::string& key) const;
void throwCursorNotReady(const std::string& method) const;
2023-10-17 21:06:11 +00:00
TransactionID extractTransactionId(const Transaction& txn, const std::string& action = "") 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-10-17 21:06:11 +00:00
virtual int drop(TransactionID transaction);
virtual SizeType count(TransactionID txn) const;
2023-04-02 13:00:21 +00:00
public:
virtual void drop();
2023-10-17 21:06:11 +00:00
virtual int drop(const WriteTransaction& txn);
2023-04-02 13:00:21 +00:00
virtual SizeType count() const;
2023-10-17 21:06:11 +00:00
virtual SizeType count(const Transaction& txn) const;
2023-03-20 15:37:13 +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*/
const bool duplicates; /**<\brief true if storage supports duplicates*/
2023-04-12 15:36:33 +00:00
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*/
2023-08-15 18:48:19 +00:00
inline static const std::string flagsMethodName = "flags"; /**<\brief member function name, just for exceptions*/
2023-04-12 15:36:33 +00:00
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:
2023-08-15 18:48:19 +00:00
template <class K, class V>
int makeStorage(MDB_txn* transaction, bool duplicates = false);
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 {
2023-08-15 18:48:19 +00:00
friend class ::BaseTest;
2023-08-17 14:45:11 +00:00
friend class ::DuplicatesTest;
friend class ::CacheCursorTest;
friend class ::StorageCursorTest;
2023-03-21 11:05:54 +00:00
friend class Base;
friend class Cursor<K, V>;
2022-09-20 17:16:48 +00:00
protected:
Storage(Base* parent, const std::string& name, bool duplicates = false);
2023-03-20 15:37:13 +00:00
~Storage() override;
2022-09-05 20:25:39 +00:00
virtual void discoveredRecord(const K& key, const V& value) const;
virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const;
2023-08-15 18:48:19 +00:00
uint32_t flags() const;
2023-10-17 21:06:11 +00:00
virtual void addRecord(const K& key, const V& value, TransactionID txn);
virtual bool forceRecord(const K& key, const V& value, TransactionID txn);
virtual void changeRecord(const K& key, const V& value, TransactionID txn);
virtual void removeRecord(const K& key, TransactionID txn);
virtual bool checkRecord(const K& key, TransactionID txn) const;
virtual void getRecord(const K& key, V& value, TransactionID txn) const;
virtual V getRecord(const K& key, TransactionID txn) const;
virtual std::map<K, V> readAll(TransactionID txn) const;
virtual void readAll(std::map<K, V>& result, TransactionID txn) const;
virtual void replaceAll(const std::map<K, V>& data, TransactionID txn);
virtual uint32_t addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false);
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-10-17 21:06:11 +00:00
virtual void addRecord(const K& key, const V& value, const WriteTransaction& 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-10-17 21:06:11 +00:00
virtual bool forceRecord(const K& key, const V& value, const WriteTransaction& txn);
2022-09-20 17:16:48 +00:00
virtual void changeRecord(const K& key, const V& value);
2023-10-17 21:06:11 +00:00
virtual void changeRecord(const K& key, const V& value, const WriteTransaction& txn);
2022-09-20 17:16:48 +00:00
virtual void removeRecord(const K& key);
2023-10-17 21:06:11 +00:00
virtual void removeRecord(const K& key, const WriteTransaction& txn);
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
2023-10-17 21:06:11 +00:00
virtual bool checkRecord(const K& key, const Transaction& txn) const;
virtual void getRecord(const K& key, V& value) const;
2023-10-17 21:06:11 +00:00
virtual void getRecord(const K& key, V& value, const Transaction& txn) const;
2022-09-20 17:16:48 +00:00
virtual V getRecord(const K& key) const;
2023-10-17 21:06:11 +00:00
virtual V getRecord(const K& key, const Transaction& txn) const;
virtual std::map<K, V> readAll() const;
2023-10-17 21:06:11 +00:00
virtual std::map<K, V> readAll(const Transaction& txn) const;
virtual void readAll(std::map<K, V>& result) const;
2023-10-17 21:06:11 +00:00
virtual void readAll(std::map<K, V>& result, const Transaction& txn) const;
virtual void replaceAll(const std::map<K, V>& data);
2023-10-17 21:06:11 +00:00
virtual void replaceAll(const std::map<K, V>& data, const WriteTransaction& txn);
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
2023-10-17 21:06:11 +00:00
virtual uint32_t addRecords(const std::map<K, V>& data, const WriteTransaction& txn, bool overwrite = false);
2022-09-12 15:16:18 +00:00
Cursor<K, V> createCursor();
void destroyCursor(Cursor<K, V>& cursor);
2022-09-20 17:16:48 +00:00
protected:
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::map<uint32_t, Cursor<K, V>*> cursors; /**<\brief a set of cursors that has been created under this storage*/
2022-09-05 20:25:39 +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