2019-09-11 15:03:52 +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/>.
|
|
|
|
*/
|
2022-08-22 20:29:43 +00:00
|
|
|
#ifndef CORE_STORAGE_HPP
|
|
|
|
#define CORE_STORAGE_HPP
|
2019-09-11 15:03:52 +00:00
|
|
|
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#include "storage.h"
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
Core::Storage<T>::Storage(const QString& p_name):
|
2019-09-11 15:03:52 +00:00
|
|
|
name(p_name),
|
|
|
|
opened(false),
|
|
|
|
environment(),
|
|
|
|
base()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
Core::Storage<T>::~Storage()
|
2019-09-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
void Core::Storage<T>::open()
|
2019-09-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
mdb_env_create(&environment);
|
|
|
|
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
|
|
|
path += "/" + name;
|
|
|
|
QDir cache(path);
|
|
|
|
|
|
|
|
if (!cache.exists()) {
|
|
|
|
bool res = cache.mkpath(path);
|
|
|
|
if (!res) {
|
|
|
|
throw Archive::Directory(path.toStdString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb_env_set_maxdbs(environment, 1);
|
|
|
|
mdb_env_set_mapsize(environment, 10UL * 1024UL * 1024UL);
|
|
|
|
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
|
|
|
|
|
|
|
|
MDB_txn *txn;
|
|
|
|
mdb_txn_begin(environment, NULL, 0, &txn);
|
|
|
|
mdb_dbi_open(txn, "base", MDB_CREATE, &base);
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
opened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
void Core::Storage<T>::close()
|
2019-09-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
mdb_dbi_close(environment, base);
|
|
|
|
mdb_env_close(environment);
|
|
|
|
opened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
void Core::Storage<T>::addRecord(const QString& key, const T& value)
|
2019-09-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
if (!opened) {
|
2019-09-23 12:09:15 +00:00
|
|
|
throw Archive::Closed("addRecord", name.toStdString());
|
2019-09-11 15:03:52 +00:00
|
|
|
}
|
2022-08-19 21:28:59 +00:00
|
|
|
QByteArray ba;
|
|
|
|
QDataStream ds(&ba, QIODevice::WriteOnly);
|
2019-09-11 15:03:52 +00:00
|
|
|
const std::string& id = key.toStdString();
|
2022-08-19 21:28:59 +00:00
|
|
|
ds << value;
|
2019-09-11 15:03:52 +00:00
|
|
|
|
|
|
|
MDB_val lmdbKey, lmdbData;
|
|
|
|
lmdbKey.mv_size = id.size();
|
|
|
|
lmdbKey.mv_data = (char*)id.c_str();
|
2022-08-19 21:28:59 +00:00
|
|
|
lmdbData.mv_size = ba.size();
|
|
|
|
lmdbData.mv_data = (uint8_t*)ba.data();
|
2019-09-11 15:03:52 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
mdb_txn_begin(environment, NULL, 0, &txn);
|
|
|
|
int rc;
|
|
|
|
rc = mdb_put(txn, base, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
|
|
|
|
if (rc != 0) {
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
if (rc == MDB_KEYEXIST) {
|
|
|
|
throw Archive::Exist(name.toStdString(), id);
|
|
|
|
} else {
|
|
|
|
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
void Core::Storage<T>::changeRecord(const QString& key, const T& value)
|
2019-09-23 12:09:15 +00:00
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Archive::Closed("changeRecord", name.toStdString());
|
|
|
|
}
|
2022-08-19 21:28:59 +00:00
|
|
|
|
|
|
|
QByteArray ba;
|
|
|
|
QDataStream ds(&ba, QIODevice::WriteOnly);
|
2019-09-23 12:09:15 +00:00
|
|
|
const std::string& id = key.toStdString();
|
2022-08-19 21:28:59 +00:00
|
|
|
ds << value;
|
2019-09-23 12:09:15 +00:00
|
|
|
|
|
|
|
MDB_val lmdbKey, lmdbData;
|
|
|
|
lmdbKey.mv_size = id.size();
|
|
|
|
lmdbKey.mv_data = (char*)id.c_str();
|
2022-08-19 21:28:59 +00:00
|
|
|
lmdbData.mv_size = ba.size();
|
|
|
|
lmdbData.mv_data = (uint8_t*)ba.data();
|
2019-09-23 12:09:15 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
mdb_txn_begin(environment, NULL, 0, &txn);
|
|
|
|
int rc;
|
|
|
|
rc = mdb_put(txn, base, &lmdbKey, &lmdbData, 0);
|
|
|
|
if (rc != 0) {
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
if (rc) {
|
|
|
|
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
T Core::Storage<T>::getRecord(const QString& key) const
|
2019-09-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Archive::Closed("addElement", name.toStdString());
|
|
|
|
}
|
|
|
|
const std::string& id = key.toStdString();
|
|
|
|
|
|
|
|
MDB_val lmdbKey, lmdbData;
|
|
|
|
lmdbKey.mv_size = id.size();
|
|
|
|
lmdbKey.mv_data = (char*)id.c_str();
|
|
|
|
|
|
|
|
MDB_txn *txn;
|
|
|
|
int rc;
|
|
|
|
mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
|
|
|
rc = mdb_get(txn, base, &lmdbKey, &lmdbData);
|
|
|
|
if (rc) {
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
if (rc == MDB_NOTFOUND) {
|
|
|
|
throw Archive::NotFound(id, name.toStdString());
|
|
|
|
} else {
|
|
|
|
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-19 21:28:59 +00:00
|
|
|
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
|
|
|
QDataStream ds(&ba, QIODevice::ReadOnly);
|
|
|
|
T value;
|
|
|
|
ds >> value;
|
2019-09-11 15:03:52 +00:00
|
|
|
mdb_txn_abort(txn);
|
2022-08-19 21:28:59 +00:00
|
|
|
|
2019-09-11 15:03:52 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:28:59 +00:00
|
|
|
template <class T>
|
|
|
|
void Core::Storage<T>::removeRecord(const QString& key)
|
2019-09-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Archive::Closed("addElement", name.toStdString());
|
|
|
|
}
|
|
|
|
const std::string& id = key.toStdString();
|
|
|
|
|
|
|
|
MDB_val lmdbKey;
|
|
|
|
lmdbKey.mv_size = id.size();
|
|
|
|
lmdbKey.mv_data = (char*)id.c_str();
|
|
|
|
|
|
|
|
MDB_txn *txn;
|
|
|
|
int rc;
|
|
|
|
mdb_txn_begin(environment, NULL, 0, &txn);
|
|
|
|
rc = mdb_del(txn, base, &lmdbKey, NULL);
|
|
|
|
if (rc) {
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
if (rc == MDB_NOTFOUND) {
|
|
|
|
throw Archive::NotFound(id, name.toStdString());
|
|
|
|
} else {
|
|
|
|
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mdb_txn_commit(txn);
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 20:29:43 +00:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
QString Core::Storage<T>::getName() const {
|
|
|
|
return name;}
|
|
|
|
|
|
|
|
#endif //CORE_STORAGE_HPP
|