/* * LMDB Abstraction Layer. * Copyright (C) 2023 Yury Gubich * * 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 . */ #ifndef LMDBAL_CURSOR_HPP #define LMDBAL_CURSOR_HPP #include "cursor.h" template LMDBAL::Cursor::Cursor(Storage* parent): storage(parent), cursor(nullptr), state(closed) { } template LMDBAL::Cursor::~Cursor () { close(); } template void LMDBAL::Cursor::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 void LMDBAL::Cursor::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 void LMDBAL::Cursor::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 void LMDBAL::Cursor::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 void LMDBAL::Cursor::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 void LMDBAL::Cursor::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