2023-08-05 20:13:43 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LMDBAL_CURSOR_HPP
|
|
|
|
#define LMDBAL_CURSOR_HPP
|
|
|
|
|
|
|
|
#include "cursor.h"
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
|
2023-08-07 21:27:44 +00:00
|
|
|
storage(parent),
|
|
|
|
cursor(nullptr),
|
|
|
|
state(closed)
|
2023-08-05 20:13:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
LMDBAL::Cursor<K, V>::~Cursor () {
|
2023-08-07 21:27:44 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void LMDBAL::Cursor<K, V>::terminated () const {
|
|
|
|
close(); //for now it's the same, but if I ever going to make writable cursor - here is where it's gonna be different
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void LMDBAL::Cursor<K, V>::open () const {
|
2023-08-09 17:41:15 +00:00
|
|
|
storage->ensureOpened(openCursorMethodName);
|
2023-08-07 21:27:44 +00:00
|
|
|
switch (state) {
|
|
|
|
case closed: {
|
|
|
|
TransactionID txn = storage->beginReadOnlyTransaction();
|
|
|
|
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
|
|
|
|
if (result != MDB_SUCCESS)
|
|
|
|
storage->throwUnknown(result, txn);
|
|
|
|
|
|
|
|
storage->transactionStarted(txn, true);
|
|
|
|
state = openedPrivate;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void LMDBAL::Cursor<K, V>::open (TransactionID txn) const {
|
2023-08-09 17:41:15 +00:00
|
|
|
storage->ensureOpened(openCursorMethodName);
|
2023-08-07 21:27:44 +00:00
|
|
|
switch (state) {
|
|
|
|
case closed: {
|
|
|
|
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
|
|
|
|
if (result != MDB_SUCCESS)
|
|
|
|
storage->throwUnknown(result);
|
2023-08-05 20:13:43 +00:00
|
|
|
|
2023-08-07 21:27:44 +00:00
|
|
|
state = openedPublic;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-08-05 20:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
2023-08-07 21:27:44 +00:00
|
|
|
void LMDBAL::Cursor<K, V>::renew () const {
|
2023-08-09 17:41:15 +00:00
|
|
|
storage->ensureOpened(renewCursorMethodName);
|
2023-08-07 21:27:44 +00:00
|
|
|
switch (state) {
|
|
|
|
case openedPrivate: {
|
|
|
|
TransactionID txn = mdb_cursor_txn(cursor);
|
|
|
|
storage->abortTransaction(txn);
|
|
|
|
storage->transactionAborted(txn);
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case openedPublic: {
|
|
|
|
TransactionID txn = storage->beginReadOnlyTransaction();
|
|
|
|
int result = mdb_cursor_renew(txn, cursor);
|
|
|
|
if (result != MDB_SUCCESS)
|
|
|
|
storage->throwUnknown(result, txn);
|
|
|
|
|
|
|
|
storage->transactionStarted(txn, true);
|
|
|
|
state = openedPrivate;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void LMDBAL::Cursor<K, V>::renew (TransactionID txn) const {
|
2023-08-09 17:41:15 +00:00
|
|
|
storage->ensureOpened(renewCursorMethodName);
|
2023-08-07 21:27:44 +00:00
|
|
|
switch (state) {
|
|
|
|
case openedPrivate: {
|
|
|
|
TransactionID txn = mdb_cursor_txn(cursor);
|
|
|
|
storage->abortTransaction(txn);
|
|
|
|
storage->transactionAborted(txn);
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case openedPublic: {
|
|
|
|
int result = mdb_cursor_renew(txn, cursor);
|
|
|
|
if (result != MDB_SUCCESS)
|
|
|
|
storage->throwUnknown(result);
|
|
|
|
|
|
|
|
state = openedPublic;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class V>
|
|
|
|
void LMDBAL::Cursor<K, V>::close () const {
|
|
|
|
switch (state) {
|
|
|
|
case openedPublic: {
|
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
|
|
|
|
state = closed;
|
|
|
|
} break;
|
|
|
|
case openedPrivate: {
|
|
|
|
TransactionID txn = mdb_cursor_txn(cursor);
|
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
storage->abortTransaction(txn);
|
|
|
|
storage->transactionAborted(txn);
|
2023-08-05 20:13:43 +00:00
|
|
|
|
2023-08-07 21:27:44 +00:00
|
|
|
state = closed;
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-08-05 20:13:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 17:41:15 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2023-08-05 20:13:43 +00:00
|
|
|
#endif //LMDBAL_CURSOR_HPP
|