forked from blue/lmdbal
RAII transactions
This commit is contained in:
parent
a9aa6b549f
commit
de741eda21
22 changed files with 689 additions and 222 deletions
71
src/transaction.cpp
Normal file
71
src/transaction.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include "transaction.h"
|
||||
|
||||
LMDBAL::Transaction::Transaction():
|
||||
txn(nullptr),
|
||||
active(false),
|
||||
parent(nullptr)
|
||||
{}
|
||||
|
||||
LMDBAL::Transaction::Transaction(TransactionID txn, const Base* parent) :
|
||||
txn(txn),
|
||||
active(true),
|
||||
parent(parent)
|
||||
{}
|
||||
|
||||
LMDBAL::Transaction::Transaction(Transaction&& other):
|
||||
txn(other.txn),
|
||||
active(other.active),
|
||||
parent(other.parent)
|
||||
{
|
||||
other.active = false;
|
||||
}
|
||||
|
||||
LMDBAL::Transaction::~Transaction() {
|
||||
terminate();
|
||||
}
|
||||
|
||||
LMDBAL::Transaction& LMDBAL::Transaction::operator=(Transaction&& other) {
|
||||
terminate();
|
||||
|
||||
txn = other.txn;
|
||||
active = other.active;
|
||||
parent = other.parent;
|
||||
|
||||
other.active = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void LMDBAL::Transaction::terminate() {
|
||||
if (active) {
|
||||
parent->abortTransaction(txn);
|
||||
active = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool LMDBAL::Transaction::isActive() const {
|
||||
return active; //todo may be it's better if I query it from DB?
|
||||
}
|
||||
|
||||
LMDBAL::WriteTransaction::WriteTransaction(TransactionID txn, Base* parent):
|
||||
Transaction(txn, parent)
|
||||
{}
|
||||
|
||||
LMDBAL::WriteTransaction::WriteTransaction():
|
||||
Transaction()
|
||||
{}
|
||||
|
||||
LMDBAL::WriteTransaction::WriteTransaction(WriteTransaction&& other):
|
||||
Transaction(std::move(other))
|
||||
{}
|
||||
|
||||
void LMDBAL::WriteTransaction::abort() {
|
||||
terminate();
|
||||
}
|
||||
|
||||
void LMDBAL::WriteTransaction::commit() {
|
||||
if (active) {
|
||||
const_cast<Base*>(parent)->commitTransaction(txn);
|
||||
active = false;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue