big refactoring part 1
This commit is contained in:
parent
6a8f67ac34
commit
19229f6c26
28 changed files with 867 additions and 795 deletions
151
database.cpp
151
database.cpp
|
@ -16,30 +16,30 @@
|
|||
|
||||
#include "database.h"
|
||||
#include "exceptions.h"
|
||||
#include "table.h"
|
||||
#include "storage.h"
|
||||
|
||||
DataBase::DataBase(const QString& p_name, uint16_t mapSize):
|
||||
LMDBDataBase::DataBase::DataBase(const QString& p_name, uint16_t mapSize):
|
||||
name(p_name.toStdString()),
|
||||
opened(false),
|
||||
size(mapSize),
|
||||
environment(),
|
||||
tables()
|
||||
{
|
||||
}
|
||||
tables(),
|
||||
transactions(new Transactions())
|
||||
{}
|
||||
|
||||
DataBase::~DataBase()
|
||||
{
|
||||
LMDBDataBase::DataBase::~DataBase() {
|
||||
close();
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
|
||||
delete transactions;
|
||||
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables)
|
||||
delete pair.second;
|
||||
}
|
||||
}
|
||||
|
||||
void DataBase::close()
|
||||
{
|
||||
void LMDBDataBase::DataBase::close() {
|
||||
if (opened) {
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
_Table* table = pair.second;
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
|
||||
StorageBase* table = pair.second;
|
||||
mdb_dbi_close(environment, table->dbi);
|
||||
}
|
||||
mdb_env_close(environment);
|
||||
|
@ -47,8 +47,7 @@ void DataBase::close()
|
|||
}
|
||||
}
|
||||
|
||||
void DataBase::open()
|
||||
{
|
||||
void LMDBDataBase::DataBase::open() {
|
||||
if (!opened) {
|
||||
mdb_env_create(&environment);
|
||||
QString path = createDirectory();
|
||||
|
@ -60,40 +59,34 @@ void DataBase::open()
|
|||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
_Table* table = pair.second;
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
|
||||
StorageBase* table = pair.second;
|
||||
int rc = table->createTable(txn);
|
||||
if (rc) {
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
}
|
||||
|
||||
}
|
||||
mdb_txn_commit(txn);
|
||||
opened = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool DataBase::removeDirectory()
|
||||
{
|
||||
if (opened) {
|
||||
bool LMDBDataBase::DataBase::removeDirectory() {
|
||||
if (opened)
|
||||
throw Opened(name, "remove database directory");
|
||||
}
|
||||
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + getName();
|
||||
QDir cache(path);
|
||||
|
||||
if (cache.exists()) {
|
||||
if (cache.exists())
|
||||
return cache.removeRecursively();
|
||||
} else {
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
QString DataBase::createDirectory()
|
||||
{
|
||||
if (opened) {
|
||||
QString LMDBDataBase::DataBase::createDirectory() {
|
||||
if (opened)
|
||||
throw Opened(name, "create database directory");
|
||||
}
|
||||
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + getName();
|
||||
|
@ -101,44 +94,100 @@ QString DataBase::createDirectory()
|
|||
|
||||
if (!cache.exists()) {
|
||||
bool res = cache.mkpath(path);
|
||||
if (!res) {
|
||||
if (!res)
|
||||
throw Directory(path.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
QString LMDBDataBase::DataBase::getName() const {
|
||||
return QString::fromStdString(name);}
|
||||
|
||||
QString DataBase::getName() const
|
||||
{
|
||||
return QString::fromStdString(name);
|
||||
}
|
||||
bool LMDBDataBase::DataBase::ready() const {
|
||||
return opened;}
|
||||
|
||||
bool DataBase::ready() const
|
||||
{
|
||||
return opened;
|
||||
}
|
||||
|
||||
void DataBase::drop()
|
||||
{
|
||||
if (!opened) {
|
||||
void LMDBDataBase::DataBase::drop() {
|
||||
if (!opened)
|
||||
throw Closed("drop", name);
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
}
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
|
||||
rc = pair.second->drop(txn);
|
||||
if (rc) {
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc), pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction() const {
|
||||
return beginReadOnlyTransaction(emptyName);}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction() const {
|
||||
return beginTransaction(emptyName);}
|
||||
|
||||
void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id) const {
|
||||
return abortTransaction(id, emptyName);}
|
||||
|
||||
void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id) const {
|
||||
return commitTransaction(id, emptyName);}
|
||||
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::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);
|
||||
}
|
||||
transactions->emplace(txn);
|
||||
return txn;
|
||||
}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction(const std::string& storageName) const {
|
||||
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);
|
||||
}
|
||||
transactions->emplace(txn);
|
||||
return txn;
|
||||
}
|
||||
|
||||
void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("abortTransaction", name, storageName);
|
||||
|
||||
Transactions::iterator itr = transactions->find(id);
|
||||
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);
|
||||
transactions->erase(itr);
|
||||
}
|
||||
|
||||
void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("abortTransaction", name, storageName);
|
||||
|
||||
Transactions::iterator itr = transactions->find(id);
|
||||
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);
|
||||
transactions->erase(itr);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue