22#include "exceptions.h"
24#define UNUSED(x) (void)(x)
47template<
class K,
class V>
58template<
class K,
class V>
61 pair.second->dropped();
78template<
class K,
class V>
106template<
class K,
class V>
111 unsigned int flags = 0;
113 flags |= MDB_NODUPDATA;
115 flags |= MDB_NOOVERWRITE;
117 int rc = _mdbPut(txn, lmdbKey, lmdbData,
flags);
118 if (rc != MDB_SUCCESS)
141template<
class K,
class V>
163template<
class K,
class V>
197template<
class K,
class V>
211 int rc = _mdbGet(txn, lmdbKey, lmdbData);
225 rc = _mdbPut(txn, lmdbKey, lmdbData);
226 if (rc != MDB_SUCCESS)
253template<
class K,
class V>
278template<
class K,
class V>
313template<
class K,
class V>
316 int rc = _mdbCursorOpen(txn, &cursor);
317 if (rc != MDB_SUCCESS)
322 rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_SET);
323 if (rc != MDB_SUCCESS)
327 bool sameSize = lmdbData.mv_size == lmdbNewData.mv_size;
328 int firstDifferentByte = 0;
330 firstDifferentByte = memcmp(lmdbData.mv_data, lmdbNewData.mv_data, lmdbData.mv_size);
331 if (firstDifferentByte == 0) {
332 _mdbCursorClose(cursor);
337 unsigned int flags = MDB_CURRENT;
338 if (
duplicates && (!sameSize || firstDifferentByte < 0)) {
339 rc = _mdbCursorDel(cursor);
340 flags = MDB_NODUPDATA;
343 if (rc == MDB_SUCCESS)
344 rc = _mdbCursorPut(cursor, lmdbKey, lmdbNewData,
flags);
346 _mdbCursorClose(cursor);
347 if (rc != MDB_SUCCESS)
376template<
class K,
class V>
400template<
class K,
class V>
427template<
class K,
class V>
460template<
class K,
class V>
490template<
class K,
class V>
514template<
class K,
class V>
519 int rc = _mdbGet(txn, lmdbKey, lmdbData);
520 if (rc != MDB_SUCCESS)
550template<
class K,
class V>
565template<
class K,
class V>
591template<
class K,
class V>
596 int rc = _mdbGet(txn, lmdbKey, lmdbData);
597 if (rc == MDB_SUCCESS)
600 if (rc != MDB_NOTFOUND)
620template<
class K,
class V>
637template<
class K,
class V>
641 std::map<K, V> result;
659template<
class K,
class V>
685template<
class K,
class V>
687 std::map<K, V> result;
708template<
class K,
class V>
726template<
class K,
class V>
729 MDB_val lmdbKey, lmdbData;
731 int rc = _mdbCursorOpen(txn, &cursor);
732 if (rc != MDB_SUCCESS)
735 rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_FIRST);
736 while (rc == MDB_SUCCESS) {
739 std::pair<typename std::map<K, V>::iterator,
bool> probe = result.emplace(key, V{});
743 rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_NEXT);
745 _mdbCursorClose(cursor);
746 if (rc != MDB_NOTFOUND)
767template<
class K,
class V>
783template<
class K,
class V>
808template<
class K,
class V>
811 if (rc != MDB_SUCCESS)
814 MDB_val lmdbKey, lmdbData;
815 for (
const std::pair<const K, V>& pair : data) {
819 rc = _mdbPut(txn, lmdbKey, lmdbData, MDB_NOOVERWRITE);
820 if (rc != MDB_SUCCESS)
839template<
class K,
class V>
856template<
class K,
class V>
886template<
class K,
class V>
888 MDB_val lmdbKey, lmdbData;
890 for (
const std::pair<const K, V>& pair : data) {
894 rc = _mdbPut(txn, lmdbKey, lmdbData, overwrite ? 0 : MDB_NOOVERWRITE);
895 if (rc == MDB_KEYEXIST)
898 if (rc != MDB_SUCCESS)
903 rc = _mdbStat(txn, stat);
904 if (rc != MDB_SUCCESS)
907 return stat.ms_entries;
926template<
class K,
class V>
943template<
class K,
class V>
970template<
class K,
class V>
973 int rc = _mdbDel(txn, lmdbKey);
974 if (rc != MDB_SUCCESS)
993template<
class K,
class V>
1005template<
class K,
class V>
1013template<
class K,
class V>
1016 pair.second->terminated();
1029template<
class K,
class V>
1046template<
class K,
class V>
1048 typename std::map<uint32_t, Cursor<K, V>*>::iterator itr =
cursors.find(cursor.id);
1050 throwUnknown(
"An attempt to destroy a cursor the storage doesn't own");
1067template<
class K,
class V>
1073 int res = _mdbFlags(txn, result);
1075 if (res != MDB_SUCCESS)
1087template<
class K,
class V>
1100template<
class K,
class V>
void reset()
A private method that turns cursor into an empty one.
Definition cursorcommon.cpp:113
void close()
Termiates a sequence of operations with the cursor.
Definition cursorcommon.cpp:161
An object to iterate storages.
Definition cursor.h:32
Thrown if there was a key conflict in one of the storages.
Definition exceptions.h:178
StorageCommon(Base *parent, const std::string &name, bool duplicates=false)
Constructs a storage interface.
Definition storagecommon.cpp:42
void commitTransaction(TransactionID id)
Commits transaction.
Definition storagecommon.cpp:454
static const std::string getRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:123
TransactionID beginReadOnlyTransaction() const
Begins read-only transaction.
Definition storagecommon.cpp:426
int makeStorage(MDB_txn *transaction, bool duplicates=false)
A functiion to actually open MDB_dbi storage.
Definition storagecommon.hpp:38
void abortTransaction(TransactionID id) const
Aborts transaction.
Definition storagecommon.cpp:444
static std::string toString(const T &value)
A method to cast a value (which can be a value or a key) to string.
Definition storagecommon.hpp:71
static const std::string forceRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:119
virtual void close()
A private virtual function to close each storage in the database.
Definition storagecommon.cpp:57
static const std::string flagsMethodName
member function name, just for exceptions
Definition storagecommon.h:116
const std::string name
this storage name
Definition storagecommon.h:111
static const std::string checkRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:122
TransactionID extractTransactionId(const Transaction &txn, const std::string &action="") const
Checks if the transaction is still active, returns inner TransactionID.
Definition storagecommon.cpp:72
static const std::string readAllMethodName
member function name, just for exceptions
Definition storagecommon.h:124
static const std::string changeRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:120
void throwDuplicate(const std::string &key) const
Throws LMDBAL::Exist.
Definition storagecommon.cpp:392
static const std::string addRecordsMethodName
member function name, just for exceptions
Definition storagecommon.h:126
void throwUnknown(int rc, TransactionID txn) const
Throws LMDBAL::Unknown (transaction vairiant)
Definition storagecommon.cpp:334
void throwDuplicateOrUnknown(int rc, const std::string &key) const
Throws LMDBAL::Exist or LMDBAL::Unknown.
Definition storagecommon.cpp:299
void throwNotFoundOrUnknown(int rc, const std::string &key) const
Throws LMDBAL::NotFound or LMDBAL::Unknown (transaction variant)
Definition storagecommon.cpp:317
TransactionID beginTransaction() const
Begins writable transaction.
Definition storagecommon.cpp:436
static const std::string addRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:118
const bool duplicates
true if storage supports duplicates
Definition storagecommon.h:112
void ensureOpened(const std::string &methodName) const
Helper function, thows exception if the database is not opened.
Definition storagecommon.cpp:135
static const std::string replaceAllMethodName
member function name, just for exceptions
Definition storagecommon.h:125
static const std::string removeRecordMethodName
member function name, just for exceptions
Definition storagecommon.h:121
virtual bool checkRecord(const K &key, TransactionID txn) const
Chechs if storage has value (private transaction variant)
Definition storage.hpp:592
Serializer< V > valueSerializer
internal object that would serialize and deserialize values
Definition storage.h:95
virtual void addRecord(const K &key, const V &value, TransactionID txn)
Adds a key-value record to the storage (private transaction variant)
Definition storage.hpp:107
virtual std::map< K, V > readAll() const
Reads whole storage into a map.
Definition storage.hpp:638
virtual int drop(TransactionID transaction)
Drops content of a storage interface (transaction variant)
Definition storagecommon.cpp:109
~Storage() override
Destroys a storage.
Definition storage.hpp:59
Serializer< K > keySerializer
internal object that would serialize and deserialize keys
Definition storage.h:94
std::map< uint32_t, Cursor< K, V > * > cursors
a set of cursors that has been created under this storage
Definition storage.h:96
virtual void discoveredRecord(const K &key, const V &value) const
A private virtual method that cursor calls when he reads a record, does nothing here but populates th...
Definition storage.hpp:1088
virtual void replaceAll(const std::map< K, V > &data, TransactionID txn)
Replaces the content of the whole storage with the given (private transaction variant)
Definition storage.hpp:809
void destroyCursor(Cursor< K, V > &cursor)
Frees cursor.
Definition storage.hpp:1047
uint32_t flags() const
Reads current storage flags it was opened with.
Definition storage.hpp:1068
virtual void removeRecord(const K &key, TransactionID txn)
Removes one of the records (private transaction variant)
Definition storage.hpp:971
void close() override
A private virtual method I need to close each storage in the database.
Definition storage.hpp:1014
Cursor< K, V > createCursor()
Creates cursor.
Definition storage.hpp:1030
virtual std::map< K, V > readAll(TransactionID txn) const
Reads whole storage into a map (private transaction variant)
Definition storage.hpp:686
virtual bool forceRecord(const K &key, const V &value, TransactionID txn)
Adds a key-value record to the storage, overwrites if it already exists (private transaction variant)
Definition storage.hpp:198
virtual void changeRecord(const K &key, const V &value, TransactionID txn)
Changes key-value record to the storage (private transaction variant)
Definition storage.hpp:314
virtual void getRecord(const K &key, V &value, TransactionID txn) const
Gets the record from the database (private transaction, reference variant)
Definition storage.hpp:515
Storage(Base *parent, const std::string &name, bool duplicates=false)
Creates a storage.
Definition storage.hpp:48
virtual uint32_t addRecords(const std::map< K, V > &data, TransactionID txn, bool overwrite=false)
Adds records in bulk (private transaction variant)
Definition storage.hpp:887
int open(MDB_txn *transaction) override
A private virtual method I need to open each storage in the database.
Definition storage.hpp:1006
Public read only transaction.
Definition transaction.h:27
Public writable transaction.
Definition transaction.h:54