forked from blue/lmdbal
some thoughts about cursors, nothing special yet
This commit is contained in:
parent
69bf1fcc3d
commit
5fba60f7f0
4 changed files with 144 additions and 11 deletions
109
src/cursor.hpp
109
src/cursor.hpp
|
@ -23,19 +23,122 @@
|
|||
|
||||
template<class K, class V>
|
||||
LMDBAL::Cursor<K, V>::Cursor(Storage<K, V>* parent):
|
||||
storage(parent)
|
||||
storage(parent),
|
||||
cursor(nullptr),
|
||||
state(closed)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
LMDBAL::Cursor<K, V>::~Cursor () {
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::terminated () {
|
||||
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 {
|
||||
storage->ensureOpened(openRecordMethodName);
|
||||
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 {
|
||||
storage->ensureOpened(openRecordMethodName);
|
||||
switch (state) {
|
||||
case closed: {
|
||||
int result = mdb_cursor_open(txn, storage->dbi, &cursor);
|
||||
if (result != MDB_SUCCESS)
|
||||
storage->throwUnknown(result);
|
||||
|
||||
state = openedPublic;
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void LMDBAL::Cursor<K, V>::renew () const {
|
||||
storage->ensureOpened(openRecordMethodName);
|
||||
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 {
|
||||
storage->ensureOpened(openRecordMethodName);
|
||||
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);
|
||||
|
||||
state = closed;
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LMDBAL_CURSOR_HPP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue