1
0
Fork 0
forked from blue/lmdbal

finally methods that actually do something, some testing of them

This commit is contained in:
Blue 2023-08-09 14:41:15 -03:00
parent 5fba60f7f0
commit 8cb1e97e30
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 348 additions and 12 deletions

View file

@ -42,7 +42,7 @@ void LMDBAL::Cursor<K, V>::terminated () const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::open () const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(openCursorMethodName);
switch (state) {
case closed: {
TransactionID txn = storage->beginReadOnlyTransaction();
@ -60,7 +60,7 @@ void LMDBAL::Cursor<K, V>::open () const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(openCursorMethodName);
switch (state) {
case closed: {
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
@ -76,7 +76,7 @@ void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew () const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(renewCursorMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
@ -100,7 +100,7 @@ void LMDBAL::Cursor<K, V>::renew () const {
template<class K, class V>
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
storage->ensureOpened(openRecordMethodName);
storage->ensureOpened(renewCursorMethodName);
switch (state) {
case openedPrivate: {
TransactionID txn = mdb_cursor_txn(cursor);
@ -141,4 +141,90 @@ void LMDBAL::Cursor<K, V>::close () const {
}
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::first (K& key, V& value) const {
operateCursorRead(key, value, MDB_FIRST, firstMethodName, firstOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::last (K& key, V& value) const {
operateCursorRead(key, value, MDB_LAST, lastMethodName, lastOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::next (K& key, V& value) const {
operateCursorRead(key, value, MDB_NEXT, nextMethodName, nextOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::prev (K& key, V& value) const {
operateCursorRead(key, value, MDB_PREV, prevMethodName, prevOperationName);
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::current (K& key, V& value) const {
operateCursorRead(key, value, MDB_GET_CURRENT, currentMethodName, currentOperationName);
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::first () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_FIRST, firstMethodName, firstOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::last () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_LAST, lastMethodName, lastOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::next () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_NEXT, nextMethodName, nextOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::prev () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_PREV, prevMethodName, prevOperationName);
return result;
}
template<class K, class V>
std::pair<K, V> LMDBAL::Cursor<K, V>::current () const {
std::pair<K, V> result;
operateCursorRead(result.first, result.second, MDB_GET_CURRENT, currentMethodName, currentOperationName);
return result;
}
template<class K, class V>
void LMDBAL::Cursor<K, V>::operateCursorRead(
K& key,
V& value,
MDB_cursor_op operation,
const std::string& methodName,
const std::string& operationName
) const {
if (state == closed)
storage->throwCursorNotReady(methodName);
MDB_val mdbKey, mdbValue;
int result = mdb_cursor_get(cursor, &mdbKey, &mdbValue, operation);
if (result != MDB_SUCCESS)
storage->throwNotFoundOrUnknown(result, operationName);
storage->keySerializer.deserialize(mdbKey, key);
storage->valueSerializer.deserialize(mdbValue, value);
if (state == openedPrivate)
storage->discoveredRecord(key, value);
else
storage->discoveredRecord(key, value, mdb_cursor_txn(cursor));
}
#endif //LMDBAL_CURSOR_HPP