1
0
forked from blue/lmdbal

lmdb calls are now compiled into .so

This commit is contained in:
Blue 2023-11-14 20:15:16 -03:00
parent 77ba8f9e7b
commit 6b475f615e
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
6 changed files with 118 additions and 35 deletions

View File

@ -1,6 +1,9 @@
# Changelog # 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 ### Bug fixes
- transaction error in LMDBAL::Cache::readAll - transaction error in LMDBAL::Cache::readAll

View File

@ -210,7 +210,7 @@ void LMDBAL::Cursor<K, V>::open () {
switch (state) { switch (state) {
case closed: { case closed: {
TransactionID txn = storage->beginReadOnlyTransaction(); TransactionID txn = storage->beginReadOnlyTransaction();
int result = mdb_cursor_open(txn, storage->dbi, &cursor); int result = storage->_mdbCursorOpen(txn, &cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn); storage->throwUnknown(result, txn);
@ -247,7 +247,7 @@ void LMDBAL::Cursor<K, V>::open (const Transaction& transaction) {
TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName); TransactionID txn = storage->extractTransactionId(transaction, openCursorMethodName);
switch (state) { switch (state) {
case closed: { case closed: {
int result = mdb_cursor_open(txn, storage->dbi, &cursor); int result = storage->_mdbCursorOpen(txn, &cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result); storage->throwUnknown(result);
@ -283,14 +283,14 @@ void LMDBAL::Cursor<K, V>::renew () {
storage->ensureOpened(renewCursorMethodName); storage->ensureOpened(renewCursorMethodName);
switch (state) { switch (state) {
case openedPrivate: { case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor); TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->abortTransaction(txn); storage->abortTransaction(txn);
storage->transactionAborted(txn); storage->transactionAborted(txn);
[[fallthrough]]; [[fallthrough]];
} }
case openedPublic: { case openedPublic: {
TransactionID txn = storage->beginReadOnlyTransaction(); TransactionID txn = storage->beginReadOnlyTransaction();
int result = mdb_cursor_renew(txn, cursor); int result = storage->_mdbCursorRenew(txn, cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result, txn); storage->throwUnknown(result, txn);
@ -331,13 +331,13 @@ void LMDBAL::Cursor<K, V>::renew (const Transaction& transaction) {
TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName); TransactionID txn = storage->extractTransactionId(transaction, renewCursorMethodName);
switch (state) { switch (state) {
case openedPrivate: { case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor); TransactionID txn = storage->_mdbCursorTxn(cursor);
storage->abortTransaction(txn); storage->abortTransaction(txn);
storage->transactionAborted(txn); storage->transactionAborted(txn);
[[fallthrough]]; [[fallthrough]];
} }
case openedPublic: { case openedPublic: {
int result = mdb_cursor_renew(txn, cursor); int result = storage->_mdbCursorRenew(txn, cursor);
if (result != MDB_SUCCESS) if (result != MDB_SUCCESS)
storage->throwUnknown(result); storage->throwUnknown(result);
@ -362,13 +362,13 @@ template<class K, class V>
void LMDBAL::Cursor<K, V>::close () { void LMDBAL::Cursor<K, V>::close () {
switch (state) { switch (state) {
case openedPublic: { case openedPublic: {
mdb_cursor_close(cursor); storage->_mdbCursorClose(cursor);
state = closed; state = closed;
} break; } break;
case openedPrivate: { case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor); TransactionID txn = storage->_mdbCursorTxn(cursor);
mdb_cursor_close(cursor); storage->_mdbCursorClose(cursor);
storage->abortTransaction(txn); storage->abortTransaction(txn);
storage->transactionAborted(txn); storage->transactionAborted(txn);
@ -608,7 +608,7 @@ bool LMDBAL::Cursor<K, V>::set (const K& key) {
storage->throwCursorNotReady(setMethodName); storage->throwCursorNotReady(setMethodName);
MDB_val mdbKey = storage->keySerializer.setData(key); 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) if (result == MDB_SUCCESS)
return true; return true;
else if (result == MDB_NOTFOUND) else if (result == MDB_NOTFOUND)
@ -645,7 +645,7 @@ void LMDBAL::Cursor<K, V>::operateCursorRead(
storage->throwCursorNotReady(methodName); storage->throwCursorNotReady(methodName);
MDB_val mdbKey, mdbValue; 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) if (result != MDB_SUCCESS)
storage->throwNotFoundOrUnknown(result, operationName); storage->throwNotFoundOrUnknown(result, operationName);
@ -655,7 +655,7 @@ void LMDBAL::Cursor<K, V>::operateCursorRead(
if (state == openedPrivate) if (state == openedPrivate)
storage->discoveredRecord(key, value); storage->discoveredRecord(key, value);
else else
storage->discoveredRecord(key, value, mdb_cursor_txn(cursor)); storage->discoveredRecord(key, value, storage->_mdbCursorTxn(cursor));
} }
#endif //LMDBAL_CURSOR_HPP #endif //LMDBAL_CURSOR_HPP

View File

@ -446,3 +446,64 @@ void LMDBAL::iStorage::transactionAborted(LMDBAL::TransactionID txn) const {
* after the transaction is commited. Used just for optimisations. * after the transaction is commited. Used just for optimisations.
*/ */
void LMDBAL::iStorage::handleDrop() {} 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);
}

View File

@ -77,6 +77,26 @@ protected:
virtual int drop(TransactionID transaction); virtual int drop(TransactionID transaction);
virtual SizeType count(TransactionID txn) const; 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: public:
virtual void drop(); virtual void drop();
virtual int drop(const WriteTransaction& txn); virtual int drop(const WriteTransaction& txn);

View File

@ -115,7 +115,7 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionI
else else
flags |= MDB_NOOVERWRITE; flags |= MDB_NOOVERWRITE;
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, flags); int rc = _mdbPut(txn, lmdbKey, lmdbData, flags);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwDuplicateOrUnknown(rc, toString(key)); throwDuplicateOrUnknown(rc, toString(key));
} }
@ -209,7 +209,7 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); int rc = _mdbGet(txn, lmdbKey, lmdbData);
switch (rc) { switch (rc) {
case MDB_SUCCESS: case MDB_SUCCESS:
added = false; added = false;
@ -223,7 +223,7 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
} }
lmdbData = valueSerializer.setData(value); lmdbData = valueSerializer.setData(value);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0); rc = _mdbPut(txn, lmdbKey, lmdbData);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
} }
@ -314,13 +314,13 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
template<class K, class V> template<class K, class V>
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) { void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
MDB_cursor* cursor; MDB_cursor* cursor;
int rc = mdb_cursor_open(txn, dbi, &cursor); int rc = _mdbCursorOpen(txn, &cursor);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_SET); rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_SET);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key)); throwNotFoundOrUnknown(rc, toString(key));
@ -330,21 +330,21 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, Transacti
if (sameSize) { //can compare only if they are the same size if (sameSize) { //can compare only if they are the same size
firstDifferentByte = memcmp(lmdbData.mv_data, lmdbNewData.mv_data, lmdbData.mv_size); firstDifferentByte = memcmp(lmdbData.mv_data, lmdbNewData.mv_data, lmdbData.mv_size);
if (firstDifferentByte == 0) { //old and new is the same, nothing to do if (firstDifferentByte == 0) { //old and new is the same, nothing to do
mdb_cursor_close(cursor); _mdbCursorClose(cursor);
return; return;
} }
} }
unsigned int flags = MDB_CURRENT; unsigned int flags = MDB_CURRENT;
if (duplicates && (!sameSize || firstDifferentByte < 0)) { //if new value is greater than the old one 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; flags = MDB_NODUPDATA;
} }
if (rc == MDB_SUCCESS) 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) if (rc != MDB_SUCCESS)
throwDuplicateOrUnknown(rc, toString(key)); throwDuplicateOrUnknown(rc, toString(key));
} }
@ -517,7 +517,7 @@ void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn)
MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); int rc = _mdbGet(txn, lmdbKey, lmdbData);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key)); throwNotFoundOrUnknown(rc, toString(key));
@ -594,7 +594,7 @@ bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const {
MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData; MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); int rc = _mdbGet(txn, lmdbKey, lmdbData);
if (rc == MDB_SUCCESS) if (rc == MDB_SUCCESS)
return true; return true;
@ -729,11 +729,11 @@ void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) c
MDB_cursor* cursor; MDB_cursor* cursor;
MDB_val lmdbKey, lmdbData; MDB_val lmdbKey, lmdbData;
int rc = mdb_cursor_open(txn, dbi, &cursor); int rc = _mdbCursorOpen(txn, &cursor);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST); rc = _mdbCursorGet(cursor, lmdbKey, lmdbData, MDB_FIRST);
while (rc == MDB_SUCCESS) { while (rc == MDB_SUCCESS) {
K key; K key;
keySerializer.deserialize(lmdbKey, key); keySerializer.deserialize(lmdbKey, key);
@ -741,9 +741,9 @@ void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) c
if (probe.second) //I do this to avoid overwrites in case duplicates are enabled if (probe.second) //I do this to avoid overwrites in case duplicates are enabled
valueSerializer.deserialize(lmdbData, probe.first->second); 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) if (rc != MDB_NOTFOUND)
throwUnknown(rc); throwUnknown(rc);
} }
@ -817,7 +817,7 @@ void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data, TransactionID
lmdbKey = keySerializer.setData(pair.first); lmdbKey = keySerializer.setData(pair.first);
lmdbData = valueSerializer.setData(pair.second); 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) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
} }
@ -892,7 +892,7 @@ uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, Transacti
lmdbKey = keySerializer.setData(pair.first); lmdbKey = keySerializer.setData(pair.first);
lmdbData = valueSerializer.setData(pair.second); 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) if (rc == MDB_KEYEXIST)
throwDuplicate(toString(pair.first)); throwDuplicate(toString(pair.first));
@ -901,7 +901,7 @@ uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, Transacti
} }
MDB_stat stat; MDB_stat stat;
rc = mdb_stat(txn, dbi, &stat); rc = _mdbStat(txn, stat);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwUnknown(rc); throwUnknown(rc);
@ -971,7 +971,7 @@ void LMDBAL::Storage<K, V>::removeRecord(const K& key) {
template<class K, class V> template<class K, class V>
void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) { void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) {
MDB_val lmdbKey = keySerializer.setData(key); MDB_val lmdbKey = keySerializer.setData(key);
int rc = mdb_del(txn, dbi, &lmdbKey, NULL); int rc = _mdbDel(txn, lmdbKey);
if (rc != MDB_SUCCESS) if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key)); throwNotFoundOrUnknown(rc, toString(key));
} }
@ -1071,7 +1071,7 @@ uint32_t LMDBAL::Storage<K, V>::flags() const {
uint32_t result; uint32_t result;
TransactionID txn = beginReadOnlyTransaction(); TransactionID txn = beginReadOnlyTransaction();
int res = mdb_dbi_flags(txn, dbi, &result); int res = _mdbFlags(txn, result);
abortTransaction(txn); abortTransaction(txn);
if (res != MDB_SUCCESS) if (res != MDB_SUCCESS)
throwUnknown(res); throwUnknown(res);
@ -1140,7 +1140,7 @@ inline int LMDBAL::iStorage::makeStorage(MDB_txn* transaction, bool duplicates)
flags |= MDB_INTEGERDUP; flags |= MDB_INTEGERDUP;
} }
return mdb_dbi_open(transaction, name.c_str(), flags, &dbi); return _mdbOpen(transaction, flags);
} }
/** /**

View File

@ -23,7 +23,6 @@ target_link_libraries(
GTest::gtest_main GTest::gtest_main
${PROJECT_NAME} ${PROJECT_NAME}
Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Core
lmdb
) )
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(runUnitTests) gtest_discover_tests(runUnitTests)