RAII transactions
All checks were successful
Main LMDBAL workfow / Archlinux (push) Successful in 40s

This commit is contained in:
Blue 2023-10-17 18:06:11 -03:00
parent a9aa6b549f
commit de741eda21
Signed by: blue
GPG key ID: 9B203B252A63EE38
22 changed files with 689 additions and 222 deletions

View file

@ -19,6 +19,7 @@
#include "base.h"
#include "exceptions.h"
#include "storage.h"
#include "transaction.h"
#define UNUSED(x) (void)(x)
@ -120,8 +121,7 @@ bool LMDBAL::Base::removeDirectory() {
if (opened)
throw Opened(name, "remove database directory");
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
QString path = getPath();
QDir cache(path);
if (cache.exists())
@ -148,10 +148,8 @@ QString LMDBAL::Base::createDirectory() {
if (opened)
throw Opened(name, "create database directory");
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
QString path = getPath();
QDir cache(path);
if (!cache.exists()) {
bool res = cache.mkpath(path);
if (!res)
@ -169,6 +167,17 @@ QString LMDBAL::Base::createDirectory() {
QString LMDBAL::Base::getName() const {
return QString::fromStdString(name);}
/**
* \brief Returns database name
*
* \returns database path
*/
QString LMDBAL::Base::getPath() const {
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
return path;
}
/**
* \brief Returns database state
@ -190,38 +199,45 @@ void LMDBAL::Base::drop() {
if (!opened)
throw Closed("drop", name);
TransactionID txn = beginTransaction();
TransactionID txn = beginPrivateTransaction(emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages) {
int rc = pair.second->drop(txn);
if (rc != MDB_SUCCESS) {
abortTransaction(txn);
abortPrivateTransaction(txn, emptyName);
throw Unknown(name, mdb_strerror(rc), pair.first);
}
}
commitTransaction(txn);
commitPrivateTransaction(txn, emptyName);
for (const std::pair<const std::string, iStorage*>& pair : storages)
pair.second->handleDrop();
}
/**
* \brief Begins read-only transaction
*
* \returns read-only transaction ID
* \returns read-only transaction
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const {
return beginReadOnlyTransaction(emptyName);}
LMDBAL::Transaction LMDBAL::Base::beginReadOnlyTransaction() const {
TransactionID id = beginReadOnlyTransaction(emptyName);
return Transaction(id, this);
}
/**
* \brief Begins writable transaction
*
* \returns writable transaction ID
* \returns writable transaction
*
* \exception LMDBAL::Closed - thrown if the database is closed
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const {
return beginTransaction(emptyName);}
LMDBAL::WriteTransaction LMDBAL::Base::beginTransaction() {
TransactionID id = beginTransaction(emptyName);
return WriteTransaction(id, this);
}
/**
* \brief Aborts transaction