some ideas about transaction
This commit is contained in:
parent
a4bb7e6269
commit
f0779ae2aa
7 changed files with 125 additions and 65 deletions
101
src/base.cpp
101
src/base.cpp
|
@ -20,12 +20,14 @@
|
|||
#include "exceptions.h"
|
||||
#include "storage.h"
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
LMDBAL::Base::Base(const QString& p_name, uint16_t mapSize):
|
||||
name(p_name.toStdString()),
|
||||
opened(false),
|
||||
size(mapSize),
|
||||
environment(),
|
||||
tables(),
|
||||
storages(),
|
||||
transactions(new Transactions())
|
||||
{}
|
||||
|
||||
|
@ -34,18 +36,18 @@ LMDBAL::Base::~Base() {
|
|||
|
||||
delete transactions;
|
||||
|
||||
for (const std::pair<const std::string, iStorage*>& pair : tables)
|
||||
for (const std::pair<const std::string, iStorage*>& pair : storages)
|
||||
delete pair.second;
|
||||
}
|
||||
|
||||
void LMDBAL::Base::close() {
|
||||
if (opened) {
|
||||
for (LMDBAL::TransactionID id : *transactions)
|
||||
mdb_txn_abort(id);
|
||||
abortTransaction(id, emptyName);
|
||||
|
||||
for (const std::pair<const std::string, iStorage*>& pair : tables) {
|
||||
iStorage* table = pair.second;
|
||||
mdb_dbi_close(environment, table->dbi);
|
||||
for (const std::pair<const std::string, iStorage*>& pair : storages) {
|
||||
iStorage* storage = pair.second;
|
||||
mdb_dbi_close(environment, storage->dbi);
|
||||
}
|
||||
mdb_env_close(environment);
|
||||
transactions->clear();
|
||||
|
@ -61,20 +63,18 @@ void LMDBAL::Base::open() {
|
|||
mdb_env_create(&environment);
|
||||
QString path = createDirectory();
|
||||
|
||||
mdb_env_set_maxdbs(environment, tables.size());
|
||||
mdb_env_set_maxdbs(environment, storages.size());
|
||||
mdb_env_set_mapsize(environment, size * 1024UL * 1024UL);
|
||||
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
|
||||
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
|
||||
for (const std::pair<const std::string, iStorage*>& pair : tables) {
|
||||
iStorage* table = pair.second;
|
||||
int rc = table->createTable(txn);
|
||||
TransactionID txn = beginPrivateTransaction(emptyName);
|
||||
for (const std::pair<const std::string, iStorage*>& pair : storages) {
|
||||
iStorage* storage = pair.second;
|
||||
int rc = storage->createStorage(txn);
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
}
|
||||
mdb_txn_commit(txn);
|
||||
commitPrivateTransaction(txn, emptyName);
|
||||
opened = true;
|
||||
}
|
||||
}
|
||||
|
@ -120,18 +120,13 @@ void LMDBAL::Base::drop() {
|
|||
if (!opened)
|
||||
throw Closed("drop", name);
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
|
||||
for (const std::pair<const std::string, iStorage*>& pair : tables) {
|
||||
rc = pair.second->drop(txn);
|
||||
TransactionID txn = beginPrivateTransaction(emptyName);
|
||||
for (const std::pair<const std::string, iStorage*>& pair : storages) {
|
||||
int rc = pair.second->drop(txn);
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc), pair.first);
|
||||
}
|
||||
|
||||
mdb_txn_commit(txn);
|
||||
commitPrivateTransaction(txn, emptyName);
|
||||
}
|
||||
|
||||
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const {
|
||||
|
@ -146,18 +141,15 @@ void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const {
|
|||
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) const {
|
||||
return commitTransaction(id, emptyName);}
|
||||
|
||||
|
||||
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("beginReadOnlyTransaction", name, storageName);
|
||||
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
TransactionID txn = beginPrivateReadOnlyTransaction(storageName);
|
||||
transactions->emplace(txn);
|
||||
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
|
||||
pair.second->transactionStarted(txn, true);
|
||||
|
||||
return txn;
|
||||
}
|
||||
|
||||
|
@ -165,13 +157,11 @@ LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageN
|
|||
if (!opened)
|
||||
throw Closed("beginTransaction", name, storageName);
|
||||
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
TransactionID txn = beginPrivateTransaction(storageName);
|
||||
transactions->emplace(txn);
|
||||
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
|
||||
pair.second->transactionStarted(txn, false);
|
||||
|
||||
return txn;
|
||||
}
|
||||
|
||||
|
@ -183,7 +173,10 @@ void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string&
|
|||
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
|
||||
throw Unknown(name, "unable to abort transaction: transaction was not found", storageName);
|
||||
|
||||
mdb_txn_abort(id);
|
||||
abortPrivateTransaction(id, storageName);
|
||||
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
|
||||
pair.second->transactionAborted(id);
|
||||
|
||||
transactions->erase(itr);
|
||||
}
|
||||
|
||||
|
@ -195,8 +188,40 @@ void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string
|
|||
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
|
||||
throw Unknown(name, "unable to commit transaction: transaction was not found", storageName);
|
||||
|
||||
int rc = mdb_txn_commit(id);
|
||||
commitPrivateTransaction(id, storageName);
|
||||
for (const std::pair<const std::string, LMDBAL::iStorage*>& pair : storages)
|
||||
pair.second->transactionCommited(id);
|
||||
|
||||
transactions->erase(itr);
|
||||
}
|
||||
|
||||
LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::string& storageName) const {
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
return txn;
|
||||
}
|
||||
|
||||
LMDBAL::TransactionID LMDBAL::Base::beginPrivateTransaction(const std::string& storageName) const {
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
return txn;
|
||||
}
|
||||
|
||||
void LMDBAL::Base::abortPrivateTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
|
||||
UNUSED(storageName);
|
||||
mdb_txn_abort(id);
|
||||
}
|
||||
|
||||
void LMDBAL::Base::commitPrivateTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
|
||||
int rc = mdb_txn_commit(id);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue