2019-04-14 20:37:02 +00:00
|
|
|
/*
|
|
|
|
* <one line to give the program's name and a brief idea of what it does.>
|
|
|
|
* 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 "archive.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QDebug>
|
2019-04-15 22:35:09 +00:00
|
|
|
#include <QDataStream>
|
|
|
|
#include <QDir>
|
2019-04-14 20:37:02 +00:00
|
|
|
|
|
|
|
Core::Archive::Archive(const QString& p_jid, QObject* parent):
|
|
|
|
QObject(parent),
|
|
|
|
jid(p_jid),
|
|
|
|
opened(false),
|
2019-04-16 16:29:24 +00:00
|
|
|
environment(),
|
|
|
|
main(),
|
|
|
|
order()
|
2019-04-14 20:37:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Archive::~Archive()
|
|
|
|
{
|
2019-04-17 20:08:56 +00:00
|
|
|
close();
|
2019-04-14 20:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Archive::open(const QString& account)
|
|
|
|
{
|
|
|
|
if (!opened) {
|
2019-04-17 20:08:56 +00:00
|
|
|
mdb_env_create(&environment);
|
2019-04-14 20:37:02 +00:00
|
|
|
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
2019-04-15 22:35:09 +00:00
|
|
|
path += "/" + account + "/" + jid;
|
|
|
|
QDir cache(path);
|
2019-04-14 20:37:02 +00:00
|
|
|
|
2019-04-15 22:35:09 +00:00
|
|
|
if (!cache.exists()) {
|
|
|
|
bool res = cache.mkpath(path);
|
|
|
|
if (!res) {
|
|
|
|
throw Directory(path.toStdString());
|
|
|
|
}
|
2019-04-14 20:37:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
mdb_env_set_maxdbs(environment, 2);
|
|
|
|
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
|
2019-04-14 20:37:02 +00:00
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_txn *txn;
|
2019-04-17 20:08:56 +00:00
|
|
|
mdb_txn_begin(environment, NULL, 0, &txn);
|
|
|
|
mdb_dbi_open(txn, "main", MDB_CREATE, &main);
|
|
|
|
mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order);
|
|
|
|
mdb_txn_commit(txn);
|
2019-04-15 22:35:09 +00:00
|
|
|
opened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:08:56 +00:00
|
|
|
void Core::Archive::close()
|
|
|
|
{
|
|
|
|
if (opened) {
|
|
|
|
mdb_dbi_close(environment, order);
|
|
|
|
mdb_dbi_close(environment, main);
|
|
|
|
mdb_env_close(environment);
|
|
|
|
opened = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:35:09 +00:00
|
|
|
void Core::Archive::addElement(const Shared::Message& message)
|
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Closed("addElement", jid.toStdString());
|
|
|
|
}
|
|
|
|
QByteArray ba;
|
|
|
|
QDataStream ds(&ba, QIODevice::WriteOnly);
|
|
|
|
message.serialize(ds);
|
|
|
|
quint64 stamp = message.getTime().toMSecsSinceEpoch();
|
|
|
|
const std::string& id = message.getId().toStdString();
|
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_val lmdbKey, lmdbData;
|
|
|
|
lmdbKey.mv_size = id.size();
|
2019-04-17 20:08:56 +00:00
|
|
|
lmdbKey.mv_data = (char*)id.c_str();
|
2019-04-16 16:29:24 +00:00
|
|
|
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, main, &lmdbKey, &lmdbData, 0);
|
|
|
|
if (rc == 0) {
|
|
|
|
MDB_val orderKey;
|
|
|
|
orderKey.mv_size = 8;
|
|
|
|
orderKey.mv_data = (uint8_t*) &stamp;
|
|
|
|
|
|
|
|
rc = mdb_put(txn, order, &orderKey, &lmdbKey, 0);
|
|
|
|
if (rc) {
|
|
|
|
qDebug() << "An element couldn't be inserted into the index" << mdb_strerror(rc);
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
} else {
|
|
|
|
rc = mdb_txn_commit(txn);
|
|
|
|
if (rc) {
|
|
|
|
qDebug() << "A transaction error: " << mdb_strerror(rc);
|
|
|
|
}
|
2019-04-15 22:35:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-16 16:29:24 +00:00
|
|
|
qDebug() << "An element couldn't been added to the archive, skipping" << mdb_strerror(rc);
|
|
|
|
mdb_txn_abort(txn);
|
2019-04-15 22:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Archive::clear()
|
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Closed("clear", jid.toStdString());
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
mdb_txn_begin(environment, NULL, 0, &txn);
|
|
|
|
mdb_drop(txn, main, 0);
|
|
|
|
mdb_drop(txn, order, 0);
|
|
|
|
mdb_txn_commit(txn);
|
2019-04-15 22:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Message Core::Archive::getElement(const QString& id)
|
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Closed("getElement", jid.toStdString());
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:08:56 +00:00
|
|
|
std::string strKey = id.toStdString();
|
2019-04-15 22:35:09 +00:00
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_val lmdbKey, lmdbData;
|
2019-04-17 20:08:56 +00:00
|
|
|
lmdbKey.mv_size = strKey.size();
|
|
|
|
lmdbKey.mv_data = (char*)strKey.c_str();
|
2019-04-15 22:35:09 +00:00
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
int rc;
|
2019-04-17 20:08:56 +00:00
|
|
|
mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
2019-04-16 16:29:24 +00:00
|
|
|
rc = mdb_get(txn, main, &lmdbKey, &lmdbData);
|
|
|
|
if (rc) {
|
|
|
|
qDebug() <<"Get error: " << mdb_strerror(rc);
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
throw NotFound(id.toStdString(), jid.toStdString());
|
|
|
|
} else {
|
|
|
|
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
2019-04-15 22:35:09 +00:00
|
|
|
QDataStream ds(&ba, QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
Shared::Message msg;
|
|
|
|
msg.deserialize(ds);
|
2019-04-16 16:29:24 +00:00
|
|
|
mdb_txn_abort(txn);
|
2019-04-15 22:35:09 +00:00
|
|
|
return msg;
|
2019-04-14 20:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:35:09 +00:00
|
|
|
Shared::Message Core::Archive::newest()
|
2019-04-14 20:37:02 +00:00
|
|
|
{
|
2019-04-15 22:35:09 +00:00
|
|
|
QString id = newestId();
|
|
|
|
return getElement(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Core::Archive::newestId()
|
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Closed("newestId", jid.toStdString());
|
|
|
|
}
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
int rc;
|
|
|
|
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
|
|
|
MDB_cursor* cursor;
|
|
|
|
rc = mdb_cursor_open(txn, order, &cursor);
|
|
|
|
MDB_val lmdbKey, lmdbData;
|
2019-04-14 20:37:02 +00:00
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
|
|
|
|
if (rc) {
|
2019-04-17 20:08:56 +00:00
|
|
|
qDebug() << "Error geting newestId " << mdb_strerror(rc);
|
2019-04-16 16:29:24 +00:00
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
mdb_txn_abort(txn);
|
2019-04-17 20:08:56 +00:00
|
|
|
throw new Empty(jid.toStdString());
|
2019-04-15 22:35:09 +00:00
|
|
|
} else {
|
2019-04-17 20:08:56 +00:00
|
|
|
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
2019-04-16 16:29:24 +00:00
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
mdb_txn_abort(txn);
|
2019-04-17 20:08:56 +00:00
|
|
|
return sId.c_str();
|
2019-04-15 22:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Message Core::Archive::oldest()
|
|
|
|
{
|
|
|
|
return getElement(oldestId());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Core::Archive::oldestId()
|
|
|
|
{
|
|
|
|
if (!opened) {
|
|
|
|
throw Closed("oldestId", jid.toStdString());
|
|
|
|
}
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
int rc;
|
|
|
|
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
|
|
|
MDB_cursor* cursor;
|
|
|
|
rc = mdb_cursor_open(txn, order, &cursor);
|
|
|
|
MDB_val lmdbKey, lmdbData;
|
2019-04-15 22:35:09 +00:00
|
|
|
|
2019-04-16 16:29:24 +00:00
|
|
|
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
|
|
|
|
if (rc) {
|
2019-04-17 20:08:56 +00:00
|
|
|
qDebug() << "Error geting oldestId " << mdb_strerror(rc);
|
2019-04-16 16:29:24 +00:00
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
mdb_txn_abort(txn);
|
2019-04-17 20:08:56 +00:00
|
|
|
throw new Empty(jid.toStdString());
|
2019-04-15 22:35:09 +00:00
|
|
|
} else {
|
2019-04-17 20:08:56 +00:00
|
|
|
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
|
2019-04-16 16:29:24 +00:00
|
|
|
mdb_cursor_close(cursor);
|
|
|
|
mdb_txn_abort(txn);
|
2019-04-17 20:08:56 +00:00
|
|
|
return sId.c_str();
|
2019-04-15 22:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long unsigned int Core::Archive::size() const
|
|
|
|
{
|
2019-04-17 20:08:56 +00:00
|
|
|
if (!opened) {
|
|
|
|
throw Closed("size", jid.toStdString());
|
|
|
|
}
|
2019-04-16 16:29:24 +00:00
|
|
|
MDB_txn *txn;
|
|
|
|
int rc;
|
|
|
|
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
|
|
|
MDB_stat stat;
|
|
|
|
mdb_stat(txn, order, &stat);
|
|
|
|
mdb_txn_abort(txn);
|
|
|
|
return stat.ms_entries;
|
2019-04-14 20:37:02 +00:00
|
|
|
}
|