1
0
Fork 0
forked from blue/lmdbal

Transactions now get closed with the database

This commit is contained in:
Blue 2024-12-22 19:39:35 +02:00
parent 56d35d4832
commit 68ea7df6a9
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 118 additions and 34 deletions

View file

@ -50,7 +50,9 @@ LMDBAL::Transaction::Transaction(TransactionID txn, const Base* parent) :
txn(txn),
active(true),
parent(parent)
{}
{
parent->transactions[txn] = this;
}
/**
* \brief Moves transaction to a new object
@ -60,7 +62,11 @@ LMDBAL::Transaction::Transaction(Transaction&& other):
active(other.active),
parent(other.parent)
{
other.active = false;
if (active) {
parent->transactions[txn] = this;
other.reset();
}
}
/**
@ -74,13 +80,20 @@ LMDBAL::Transaction::~Transaction() {
* \brief Move-assigns transaction to the new object
*/
LMDBAL::Transaction& LMDBAL::Transaction::operator=(Transaction&& other) {
if (this == &other)
return *this;
terminate();
txn = other.txn;
active = other.active;
parent = other.parent;
other.active = false;
if (active) {
parent->transactions[txn] = this;
other.reset();
}
return *this;
}
@ -93,10 +106,22 @@ LMDBAL::Transaction& LMDBAL::Transaction::operator=(Transaction&& other) {
void LMDBAL::Transaction::terminate() {
if (active) {
parent->abortTransaction(txn);
active = false;
parent->transactions.erase(txn);
reset();
}
}
/**
* \brief Resets inner transaction properties to inactive state
*/
void LMDBAL::Transaction::reset() {
active = false;
txn = nullptr;
parent = nullptr;
}
/**
* \brief Returns transaction states
*
@ -150,6 +175,12 @@ LMDBAL::WriteTransaction::WriteTransaction(WriteTransaction&& other):
Transaction(std::move(other))
{}
LMDBAL::WriteTransaction& LMDBAL::WriteTransaction::operator=(WriteTransaction&& other) {
Transaction::operator=(std::move(other));
return *this;
}
/**
* \brief Aborts transaction cancelling all changes
*
@ -167,6 +198,8 @@ void LMDBAL::WriteTransaction::abort() {
void LMDBAL::WriteTransaction::commit() {
if (active) {
const_cast<Base*>(parent)->commitTransaction(txn);
active = false;
parent->transactions.erase(txn);
reset();
}
}