2022-09-05 20:25:39 +00:00
|
|
|
// Squawk messenger.
|
|
|
|
// Copyright (C) 2019 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/>.
|
|
|
|
|
|
|
|
#include "database.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "table.h"
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::DataBase(const QString& p_name, uint16_t mapSize):
|
2022-09-05 20:25:39 +00:00
|
|
|
name(p_name.toStdString()),
|
|
|
|
opened(false),
|
|
|
|
size(mapSize),
|
|
|
|
environment(),
|
|
|
|
tables()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
DataBase::~DataBase()
|
2022-09-05 20:25:39 +00:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
void DataBase::close()
|
2022-09-05 20:25:39 +00:00
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
|
|
|
_Table* table = pair.second;
|
|
|
|
mdb_dbi_close(environment, table->dbi);
|
|
|
|
}
|
|
|
|
mdb_env_close(environment);
|
|
|
|
opened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
void DataBase::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);
|
|
|
|
|
|
|
|
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
|
|
|
_Table* table = pair.second;
|
2022-09-14 22:18:31 +00:00
|
|
|
int rc = table->createTable(txn);
|
2022-09-05 20:25:39 +00:00
|
|
|
if (rc) {
|
|
|
|
throw Unknown(name, mdb_strerror(rc));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
opened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 21:34:39 +00:00
|
|
|
bool DataBase::removeDirectory()
|
|
|
|
{
|
|
|
|
if (opened) {
|
2022-09-17 12:31:58 +00:00
|
|
|
throw Opened(name, "remove database directory");
|
2022-09-15 21:34:39 +00:00
|
|
|
}
|
|
|
|
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
|
|
|
path += "/" + getName();
|
|
|
|
QDir cache(path);
|
|
|
|
|
|
|
|
if (cache.exists()) {
|
|
|
|
return cache.removeRecursively();
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 10:06:37 +00:00
|
|
|
QString DataBase::createDirectory()
|
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
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);
|
|
|
|
if (!res) {
|
|
|
|
throw Directory(path.toStdString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-14 22:18:31 +00:00
|
|
|
QString DataBase::getName() const
|
2022-09-05 20:25:39 +00:00
|
|
|
{
|
|
|
|
return QString::fromStdString(name);
|
|
|
|
}
|
|
|
|
|
2022-09-15 21:34:39 +00:00
|
|
|
bool DataBase::ready() const
|
|
|
|
{
|
|
|
|
return opened;
|
|
|
|
}
|
2022-09-14 22:18:31 +00:00
|
|
|
|
2022-09-17 12:31:58 +00:00
|
|
|
void DataBase::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, _Table*>& pair : tables) {
|
|
|
|
rc = pair.second->drop(txn);
|
|
|
|
if (rc) {
|
|
|
|
throw Unknown(name, mdb_strerror(rc), pair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|