1
0
forked from blue/squawk
squawk/core/storage/storage.hpp

227 lines
5.9 KiB
C++

/*
* 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/>.
*/
#ifndef CORE_STORAGE_HPP
#define CORE_STORAGE_HPP
#include <QStandardPaths>
#include <QDir>
#include "storage.h"
#include <cstring>
template <class K, class V>
Core::Storage<K, V>::Storage(const QString& p_name):
name(p_name),
opened(false),
environment(),
base()
{
}
template <class K, class V>
Core::Storage<K, V>::~Storage()
{
close();
}
template <class K, class V>
void Core::Storage<K, V>::open()
{
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;
}
}
template <class K, class V>
void Core::Storage<K, V>::close()
{
if (opened) {
mdb_dbi_close(environment, base);
mdb_env_close(environment);
opened = false;
}
}
template <class K, class V>
void Core::Storage<K, V>::addRecord(const K& key, const V& value)
{
if (!opened) {
throw Archive::Closed("addRecord", name.toStdString());
}
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << value;
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
lmdbData.mv_size = ba.size();
lmdbData.mv_data = (uint8_t*)ba.data();
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(), std::to_string(key));
} else {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
mdb_txn_commit(txn);
}
}
template <class K, class V>
void Core::Storage<K, V>::changeRecord(const K& key, const V& value)
{
if (!opened) {
throw Archive::Closed("changeRecord", name.toStdString());
}
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << value;
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
lmdbData.mv_size = ba.size();
lmdbData.mv_data = (uint8_t*)ba.data();
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);
}
}
template <class K, class V>
V Core::Storage<K, V>::getRecord(const K& key) const
{
if (!opened) {
throw Archive::Closed("addElement", name.toStdString());
}
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
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(std::to_string(key), name.toStdString());
} else {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
QDataStream ds(&ba, QIODevice::ReadOnly);
V value;
ds >> value;
mdb_txn_abort(txn);
return value;
}
}
template <class K, class V>
void Core::Storage<K, V>::removeRecord(const K& key)
{
if (!opened) {
throw Archive::Closed("addElement", name.toStdString());
}
MDB_val lmdbKey;
lmdbKey << key;
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(std::to_string(key), name.toStdString());
} else {
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
}
} else {
mdb_txn_commit(txn);
}
}
template <class K, class V>
QString Core::Storage<K, V>::getName() const {
return name;}
MDB_val& operator << (MDB_val& data, const QString& value) {
QByteArray ba = value.toUtf8();
data.mv_size = ba.size();
data.mv_data = ba.data();
return data;
}
MDB_val& operator >> (MDB_val& data, QString& value) {
value = QString::fromUtf8((const char*)data.mv_data, data.mv_size);
return data;
}
MDB_val& operator << (MDB_val& data, uint32_t& value) {
data.mv_size = 4;
data.mv_data = &value;
return data;
}
MDB_val& operator >> (MDB_val& data, uint32_t& value) {
std::memcpy(&value, data.mv_data, data.mv_size);
return data;
}
std::string std::to_string(const QString& str) {
return str.toStdString();
}
#endif //CORE_STORAGE_HPP