2023-03-23 17:27:46 +00:00
|
|
|
/*
|
|
|
|
* LMDB Abstraction Layer.
|
|
|
|
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
#include "base.h"
|
2022-09-05 20:25:39 +00:00
|
|
|
#include "exceptions.h"
|
2023-03-20 15:37:13 +00:00
|
|
|
#include "storage.h"
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
LMDBAL::Base::Base(const QString& p_name, uint16_t mapSize):
|
2022-09-05 20:25:39 +00:00
|
|
|
name(p_name.toStdString()),
|
|
|
|
opened(false),
|
|
|
|
size(mapSize),
|
|
|
|
environment(),
|
2023-03-20 15:37:13 +00:00
|
|
|
tables(),
|
|
|
|
transactions(new Transactions())
|
|
|
|
{}
|
2022-09-05 20:25:39 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
LMDBAL::Base::~Base() {
|
2022-09-05 20:25:39 +00:00
|
|
|
close();
|
2023-03-20 15:37:13 +00:00
|
|
|
|
|
|
|
delete transactions;
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
for (const std::pair<const std::string, iStorage*>& pair : tables)
|
2022-09-05 20:25:39 +00:00
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::close() {
|
2022-09-05 20:25:39 +00:00
|
|
|
if (opened) {
|
2023-03-23 17:27:46 +00:00
|
|
|
for (LMDBAL::TransactionID id : *transactions)
|
|
|
|
mdb_txn_abort(id);
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
for (const std::pair<const std::string, iStorage*>& pair : tables) {
|
|
|
|
iStorage* table = pair.second;
|
2022-09-05 20:25:39 +00:00
|
|
|
mdb_dbi_close(environment, table->dbi);
|
|
|
|
}
|
|
|
|
mdb_env_close(environment);
|
2023-03-23 17:27:46 +00:00
|
|
|
transactions->clear();
|
2022-09-05 20:25:39 +00:00
|
|
|
opened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:27:46 +00:00
|
|
|
/**
|
|
|
|
* Almost every LMDBAL::Base require it to be opened, this function opens it. It laso creates the directory for the database if it was an initial launch
|
|
|
|
*/
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::open() {
|
2022-09-05 20:25:39 +00:00
|
|
|
if (!opened) {
|
|
|
|
mdb_env_create(&environment);
|
2022-12-17 10:06:37 +00:00
|
|
|
QString path = createDirectory();
|
2022-09-05 20:25:39 +00:00
|
|
|
|
|
|
|
mdb_env_set_maxdbs(environment, tables.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);
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
for (const std::pair<const std::string, iStorage*>& pair : tables) {
|
|
|
|
iStorage* table = pair.second;
|
2022-09-14 22:18:31 +00:00
|
|
|
int rc = table->createTable(txn);
|
2023-03-20 15:37:13 +00:00
|
|
|
if (rc)
|
2022-09-05 20:25:39 +00:00
|
|
|
throw Unknown(name, mdb_strerror(rc));
|
|
|
|
}
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
opened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
bool LMDBAL::Base::removeDirectory() {
|
2023-03-20 15:37:13 +00:00
|
|
|
if (opened)
|
2022-09-17 12:31:58 +00:00
|
|
|
throw Opened(name, "remove database directory");
|
2023-03-20 15:37:13 +00:00
|
|
|
|
2022-09-15 21:34:39 +00:00
|
|
|
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
|
|
|
path += "/" + getName();
|
|
|
|
QDir cache(path);
|
|
|
|
|
2023-03-20 15:37:13 +00:00
|
|
|
if (cache.exists())
|
2022-09-15 21:34:39 +00:00
|
|
|
return cache.removeRecursively();
|
2023-03-20 15:37:13 +00:00
|
|
|
else
|
2022-09-15 21:34:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
QString LMDBAL::Base::createDirectory() {
|
2023-03-20 15:37:13 +00:00
|
|
|
if (opened)
|
2022-12-17 10:06:37 +00:00
|
|
|
throw Opened(name, "create database directory");
|
|
|
|
|
|
|
|
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
|
|
|
path += "/" + getName();
|
|
|
|
QDir cache(path);
|
|
|
|
|
|
|
|
if (!cache.exists()) {
|
|
|
|
bool res = cache.mkpath(path);
|
2023-03-20 15:37:13 +00:00
|
|
|
if (!res)
|
2022-12-17 10:06:37 +00:00
|
|
|
throw Directory(path.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
QString LMDBAL::Base::getName() const {
|
2023-03-20 15:37:13 +00:00
|
|
|
return QString::fromStdString(name);}
|
2022-12-17 10:06:37 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
bool LMDBAL::Base::ready() const {
|
2023-03-20 15:37:13 +00:00
|
|
|
return opened;}
|
2022-09-14 22:18:31 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::drop() {
|
2023-03-20 15:37:13 +00:00
|
|
|
if (!opened)
|
2022-09-17 12:31:58 +00:00
|
|
|
throw Closed("drop", name);
|
|
|
|
|
|
|
|
MDB_txn *txn;
|
|
|
|
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
2023-03-20 15:37:13 +00:00
|
|
|
if (rc)
|
2022-09-17 12:31:58 +00:00
|
|
|
throw Unknown(name, mdb_strerror(rc));
|
2023-03-20 15:37:13 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
for (const std::pair<const std::string, iStorage*>& pair : tables) {
|
2022-09-17 12:31:58 +00:00
|
|
|
rc = pair.second->drop(txn);
|
2023-03-20 15:37:13 +00:00
|
|
|
if (rc)
|
2022-09-17 12:31:58 +00:00
|
|
|
throw Unknown(name, mdb_strerror(rc), pair.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const {
|
2023-03-20 15:37:13 +00:00
|
|
|
return beginReadOnlyTransaction(emptyName);}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const {
|
2023-03-20 15:37:13 +00:00
|
|
|
return beginTransaction(emptyName);}
|
2022-09-17 12:31:58 +00:00
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const {
|
2023-03-20 15:37:13 +00:00
|
|
|
return abortTransaction(id, emptyName);}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) const {
|
2023-03-20 15:37:13 +00:00
|
|
|
return commitTransaction(id, emptyName);}
|
|
|
|
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const {
|
2023-03-20 15:37:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageName) const {
|
2023-03-20 15:37:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
|
2023-03-20 15:37:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:05:54 +00:00
|
|
|
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
|
2023-03-20 15:37:13 +00:00
|
|
|
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);
|
|
|
|
}
|