From 6b475f615ea4936419bb1e001dd82a56323d61b0 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 14 Nov 2023 20:15:16 -0300 Subject: [PATCH] lmdb calls are now compiled into .so --- CHANGELOG.md | 5 +++- src/cursor.hpp | 24 +++++++++--------- src/storage.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ src/storage.h | 20 +++++++++++++++ src/storage.hpp | 42 +++++++++++++++---------------- test/CMakeLists.txt | 1 - 6 files changed, 118 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1480e8e..a70560d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog -# LMDBAL 0.5.3 (UNRELEASED) +# LMDBAL 0.5.3 (November 14, 2023) +### Improvements +- Now you don't need to link agains lmdb, just linking against LMDBAL is enough + ### Bug fixes - transaction error in LMDBAL::Cache::readAll diff --git a/src/cursor.hpp b/src/cursor.hpp index bfcc44a..519bd41 100644 --- a/src/cursor.hpp +++ b/src/cursor.hpp @@ -210,7 +210,7 @@ void LMDBAL::Cursor::open () { switch (state) { case closed: { TransactionID txn = storage->beginReadOnlyTransaction(); - int result = mdb_cursor_open(txn, storage->dbi, &cursor); + int result = storage->_mdbCursorOpen(txn, &cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result, txn); @@ -247,7 +247,7 @@ void LMDBAL::Cursor::open (const Transaction& transaction) { TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName); switch (state) { case closed: { - int result = mdb_cursor_open(txn, storage->dbi, &cursor); + int result = storage->_mdbCursorOpen(txn, &cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result); @@ -283,14 +283,14 @@ void LMDBAL::Cursor::renew () { storage->ensureOpened(renewCursorMethodName); switch (state) { case openedPrivate: { - TransactionID txn = mdb_cursor_txn(cursor); + TransactionID txn = storage->_mdbCursorTxn(cursor); storage->abortTransaction(txn); storage->transactionAborted(txn); [[fallthrough]]; } case openedPublic: { TransactionID txn = storage->beginReadOnlyTransaction(); - int result = mdb_cursor_renew(txn, cursor); + int result = storage->_mdbCursorRenew(txn, cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result, txn); @@ -331,13 +331,13 @@ void LMDBAL::Cursor::renew (const Transaction& transaction) { TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName); switch (state) { case openedPrivate: { - TransactionID txn = mdb_cursor_txn(cursor); + TransactionID txn = storage->_mdbCursorTxn(cursor); storage->abortTransaction(txn); storage->transactionAborted(txn); [[fallthrough]]; } case openedPublic: { - int result = mdb_cursor_renew(txn, cursor); + int result = storage->_mdbCursorRenew(txn, cursor); if (result != MDB_SUCCESS) storage->throwUnknown(result); @@ -362,13 +362,13 @@ template void LMDBAL::Cursor::close () { switch (state) { case openedPublic: { - mdb_cursor_close(cursor); + storage->_mdbCursorClose(cursor); state = closed; } break; case openedPrivate: { - TransactionID txn = mdb_cursor_txn(cursor); - mdb_cursor_close(cursor); + TransactionID txn = storage->_mdbCursorTxn(cursor); + storage->_mdbCursorClose(cursor); storage->abortTransaction(txn); storage->transactionAborted(txn); @@ -608,7 +608,7 @@ bool LMDBAL::Cursor::set (const K& key) { storage->throwCursorNotReady(setMethodName); MDB_val mdbKey = storage->keySerializer.setData(key); - int result = mdb_cursor_get(cursor, &mdbKey, nullptr, MDB_SET); + int result = storage->_mdbCursorSet(cursor, mdbKey); if (result == MDB_SUCCESS) return true; else if (result == MDB_NOTFOUND) @@ -645,7 +645,7 @@ void LMDBAL::Cursor::operateCursorRead( storage->throwCursorNotReady(methodName); MDB_val mdbKey, mdbValue; - int result = mdb_cursor_get(cursor, &mdbKey, &mdbValue, operation); + int result = storage->_mdbCursorGet(cursor, mdbKey, mdbValue, operation); if (result != MDB_SUCCESS) storage->throwNotFoundOrUnknown(result, operationName); @@ -655,7 +655,7 @@ void LMDBAL::Cursor::operateCursorRead( if (state == openedPrivate) storage->discoveredRecord(key, value); else - storage->discoveredRecord(key, value, mdb_cursor_txn(cursor)); + storage->discoveredRecord(key, value, storage->_mdbCursorTxn(cursor)); } #endif //LMDBAL_CURSOR_HPP diff --git a/src/storage.cpp b/src/storage.cpp index f1258fa..cea474d 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -446,3 +446,64 @@ void LMDBAL::iStorage::transactionAborted(LMDBAL::TransactionID txn) const { * after the transaction is commited. Used just for optimisations. */ void LMDBAL::iStorage::handleDrop() {} + +int LMDBAL::iStorage::_mdbOpen(MDB_txn *txn, unsigned int flags) { + return mdb_dbi_open(txn, name.c_str(), flags, &dbi); +} + +int LMDBAL::iStorage::_mdbPut(MDB_txn* txn, MDB_val& key, MDB_val& data, unsigned int flags) { + return mdb_put(txn, dbi, &key, &data, flags); +} + +int LMDBAL::iStorage::_mdbGet(MDB_txn* txn, MDB_val& key, MDB_val& data) const { + return mdb_get(txn, dbi, &key, &data); +} + +int LMDBAL::iStorage::_mdbDel(MDB_txn* txn, MDB_val& key) { + return mdb_del(txn, dbi, &key, NULL); +} + +int LMDBAL::iStorage::_mdbDel(MDB_txn* txn, MDB_val& key, MDB_val& data) { + return mdb_del(txn, dbi, &key, &data); +} + +int LMDBAL::iStorage::_mdbStat(MDB_txn* txn, MDB_stat& stat) const { + return mdb_stat(txn, dbi, &stat); +} + +int LMDBAL::iStorage::_mdbFlags(MDB_txn* txn, uint32_t& flags) const { + return mdb_dbi_flags(txn, dbi, &flags); +} + + +int LMDBAL::iStorage::_mdbCursorOpen(MDB_txn* txn, MDB_cursor** cursor) const { + return mdb_cursor_open(txn, dbi, cursor); +} + +void LMDBAL::iStorage::_mdbCursorClose(MDB_cursor *cursor) const { + mdb_cursor_close(cursor); +} + +int LMDBAL::iStorage::_mdbCursorGet(MDB_cursor* cursor, MDB_val& key, MDB_val& data, MDB_cursor_op operation) const { + return mdb_cursor_get(cursor, &key, &data, operation); +} + +int LMDBAL::iStorage::_mdbCursorSet(MDB_cursor* cursor, MDB_val& key) const { + return mdb_cursor_get(cursor, &key, NULL, MDB_SET); +} + +int LMDBAL::iStorage::_mdbCursorDel(MDB_cursor* cursor, unsigned int flags) { + return mdb_cursor_del(cursor, flags); +} + +int LMDBAL::iStorage::_mdbCursorPut(MDB_cursor* cursor, MDB_val& key, MDB_val& data, unsigned int flags) { + return mdb_cursor_put(cursor, &key, &data, flags); +} + +int LMDBAL::iStorage::_mdbCursorRenew(MDB_txn* txn, MDB_cursor* cursor) const { + return mdb_cursor_renew(txn, cursor); +} + +MDB_txn* LMDBAL::iStorage::_mdbCursorTxn(MDB_cursor* cursor) const { + return mdb_cursor_txn(cursor); +} diff --git a/src/storage.h b/src/storage.h index de38568..82735a7 100644 --- a/src/storage.h +++ b/src/storage.h @@ -77,6 +77,26 @@ protected: virtual int drop(TransactionID transaction); virtual SizeType count(TransactionID txn) const; + int _mdbOpen(MDB_txn* txn, unsigned int flags = 0); + + int _mdbPut(MDB_txn* txn, MDB_val& key, MDB_val& data, unsigned int flags = 0); + int _mdbGet(MDB_txn* txn, MDB_val& key, MDB_val& data) const; + int _mdbDel(MDB_txn* txn, MDB_val& key); + int _mdbDel(MDB_txn* txn, MDB_val& key, MDB_val& data); + + int _mdbStat(MDB_txn* txn, MDB_stat& stat) const; + int _mdbFlags(MDB_txn* txn, uint32_t& flags) const; + + int _mdbCursorOpen(MDB_txn* txn, MDB_cursor** cursor) const; + void _mdbCursorClose(MDB_cursor* cursor) const; + int _mdbCursorGet(MDB_cursor* cursor, MDB_val& key, MDB_val& data, MDB_cursor_op operation) const; + int _mdbCursorSet(MDB_cursor* cursor, MDB_val& key) const; + int _mdbCursorDel(MDB_cursor* cursor, unsigned int flags = 0); + int _mdbCursorPut(MDB_cursor* cursor, MDB_val& key, MDB_val& data, unsigned int flags = 0); + + int _mdbCursorRenew(MDB_txn* txn, MDB_cursor* cursor) const; + MDB_txn* _mdbCursorTxn(MDB_cursor* cursor) const; + public: virtual void drop(); virtual int drop(const WriteTransaction& txn); diff --git a/src/storage.hpp b/src/storage.hpp index 1bab63b..91756b3 100644 --- a/src/storage.hpp +++ b/src/storage.hpp @@ -115,7 +115,7 @@ void LMDBAL::Storage::addRecord(const K& key, const V& value, TransactionI else flags |= MDB_NOOVERWRITE; - int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, flags); + int rc = _mdbPut(txn, lmdbKey, lmdbData, flags); if (rc != MDB_SUCCESS) throwDuplicateOrUnknown(rc, toString(key)); } @@ -209,7 +209,7 @@ bool LMDBAL::Storage::forceRecord(const K& key, const V& value, Transactio MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbData; - int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); + int rc = _mdbGet(txn, lmdbKey, lmdbData); switch (rc) { case MDB_SUCCESS: added = false; @@ -223,7 +223,7 @@ bool LMDBAL::Storage::forceRecord(const K& key, const V& value, Transactio } lmdbData = valueSerializer.setData(value); - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0); + rc = _mdbPut(txn, lmdbKey, lmdbData); if (rc != MDB_SUCCESS) throwUnknown(rc); } @@ -314,13 +314,13 @@ void LMDBAL::Storage::changeRecord(const K& key, const V& value) { template void LMDBAL::Storage::changeRecord(const K& key, const V& value, TransactionID txn) { MDB_cursor* cursor; - int rc = mdb_cursor_open(txn, dbi, &cursor); + int rc = _mdbCursorOpen(txn, &cursor); if (rc != MDB_SUCCESS) throwUnknown(rc); MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbData; - rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_SET); + rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_SET); if (rc != MDB_SUCCESS) throwNotFoundOrUnknown(rc, toString(key)); @@ -330,21 +330,21 @@ void LMDBAL::Storage::changeRecord(const K& key, const V& value, Transacti if (sameSize) { //can compare only if they are the same size firstDifferentByte = memcmp(lmdbData.mv_data, lmdbNewData.mv_data, lmdbData.mv_size); if (firstDifferentByte == 0) { //old and new is the same, nothing to do - mdb_cursor_close(cursor); + _mdbCursorClose(cursor); return; } } unsigned int flags = MDB_CURRENT; if (duplicates && (!sameSize || firstDifferentByte < 0)) { //if new value is greater than the old one - rc = mdb_cursor_del(cursor, 0); //we need to initiate duplicates sort, for it to be in the correct place + rc = _mdbCursorDel(cursor); //we need to initiate duplicates sort, for it to be in the correct place flags = MDB_NODUPDATA; } if (rc == MDB_SUCCESS) - rc = mdb_cursor_put(cursor, &lmdbKey, &lmdbNewData, flags); + rc = _mdbCursorPut(cursor, lmdbKey, lmdbNewData, flags); - mdb_cursor_close(cursor); + _mdbCursorClose(cursor); if (rc != MDB_SUCCESS) throwDuplicateOrUnknown(rc, toString(key)); } @@ -517,7 +517,7 @@ void LMDBAL::Storage::getRecord(const K& key, V& value, TransactionID txn) MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbData; - int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); + int rc = _mdbGet(txn, lmdbKey, lmdbData); if (rc != MDB_SUCCESS) throwNotFoundOrUnknown(rc, toString(key)); @@ -594,7 +594,7 @@ bool LMDBAL::Storage::checkRecord(const K& key, TransactionID txn) const { MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbData; - int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); + int rc = _mdbGet(txn, lmdbKey, lmdbData); if (rc == MDB_SUCCESS) return true; @@ -729,11 +729,11 @@ void LMDBAL::Storage::readAll(std::map& result, TransactionID txn) c MDB_cursor* cursor; MDB_val lmdbKey, lmdbData; - int rc = mdb_cursor_open(txn, dbi, &cursor); + int rc = _mdbCursorOpen(txn, &cursor); if (rc != MDB_SUCCESS) throwUnknown(rc); - rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST); + rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_FIRST); while (rc == MDB_SUCCESS) { K key; keySerializer.deserialize(lmdbKey, key); @@ -741,9 +741,9 @@ void LMDBAL::Storage::readAll(std::map& result, TransactionID txn) c if (probe.second) //I do this to avoid overwrites in case duplicates are enabled valueSerializer.deserialize(lmdbData, probe.first->second); - rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT); + rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_NEXT); } - mdb_cursor_close(cursor); + _mdbCursorClose(cursor); if (rc != MDB_NOTFOUND) throwUnknown(rc); } @@ -817,7 +817,7 @@ void LMDBAL::Storage::replaceAll(const std::map& data, TransactionID lmdbKey = keySerializer.setData(pair.first); lmdbData = valueSerializer.setData(pair.second); - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here? + rc = _mdbPut(txn, lmdbKey, lmdbData, MDB_NOOVERWRITE); //TODO may be appending with cursor makes sence here? if (rc != MDB_SUCCESS) throwUnknown(rc); } @@ -892,7 +892,7 @@ uint32_t LMDBAL::Storage::addRecords(const std::map& data, Transacti lmdbKey = keySerializer.setData(pair.first); lmdbData = valueSerializer.setData(pair.second); - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE); + rc = _mdbPut(txn, lmdbKey, lmdbData, overwrite ? 0 : MDB_NOOVERWRITE); if (rc == MDB_KEYEXIST) throwDuplicate(toString(pair.first)); @@ -901,7 +901,7 @@ uint32_t LMDBAL::Storage::addRecords(const std::map& data, Transacti } MDB_stat stat; - rc = mdb_stat(txn, dbi, &stat); + rc = _mdbStat(txn, stat); if (rc != MDB_SUCCESS) throwUnknown(rc); @@ -971,7 +971,7 @@ void LMDBAL::Storage::removeRecord(const K& key) { template void LMDBAL::Storage::removeRecord(const K& key, TransactionID txn) { MDB_val lmdbKey = keySerializer.setData(key); - int rc = mdb_del(txn, dbi, &lmdbKey, NULL); + int rc = _mdbDel(txn, lmdbKey); if (rc != MDB_SUCCESS) throwNotFoundOrUnknown(rc, toString(key)); } @@ -1071,7 +1071,7 @@ uint32_t LMDBAL::Storage::flags() const { uint32_t result; TransactionID txn = beginReadOnlyTransaction(); - int res = mdb_dbi_flags(txn, dbi, &result); + int res = _mdbFlags(txn, result); abortTransaction(txn); if (res != MDB_SUCCESS) throwUnknown(res); @@ -1140,7 +1140,7 @@ inline int LMDBAL::iStorage::makeStorage(MDB_txn* transaction, bool duplicates) flags |= MDB_INTEGERDUP; } - return mdb_dbi_open(transaction, name.c_str(), flags, &dbi); + return _mdbOpen(transaction, flags); } /** diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3994aac..e9c7476 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,7 +23,6 @@ target_link_libraries( GTest::gtest_main ${PROJECT_NAME} Qt${QT_VERSION_MAJOR}::Core - lmdb ) include(GoogleTest) gtest_discover_tests(runUnitTests)