not working lmdb
This commit is contained in:
parent
e04f7db7c2
commit
0b4714688f
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.0)
|
||||
project(squawkCORE)
|
||||
|
||||
# Instruct CMake to run moc automatically when needed.
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
find_package(Qt5Widgets CONFIG REQUIRED)
|
||||
@ -10,6 +11,7 @@ find_package(Qt5Network CONFIG REQUIRED)
|
||||
set(squawkCORE_SRC
|
||||
squawk.cpp
|
||||
account.cpp
|
||||
archive.cpp
|
||||
)
|
||||
|
||||
# Tell CMake to create the helloworld executable
|
||||
@ -19,6 +21,7 @@ add_library(squawkCORE ${squawkCORE_SRC})
|
||||
target_link_libraries(squawkCORE Qt5::Core)
|
||||
target_link_libraries(squawkCORE Qt5::Network)
|
||||
target_link_libraries(squawkCORE qxmpp)
|
||||
target_link_libraries(squawkCORE lmdb)
|
||||
|
||||
# Install the executable
|
||||
install(TARGETS squawkCORE DESTINATION lib)
|
||||
|
212
core/archive.cpp
212
core/archive.cpp
@ -21,13 +21,16 @@
|
||||
#include <sys/types.h>
|
||||
#include <QStandardPaths>
|
||||
#include <QDebug>
|
||||
#include <QDataStream>
|
||||
#include <QDir>
|
||||
|
||||
Core::Archive::Archive(const QString& p_jid, QObject* parent):
|
||||
QObject(parent),
|
||||
jid(p_jid),
|
||||
opened(false),
|
||||
environment(lmdb::env::create()),
|
||||
dbi(0)
|
||||
dbi(0),
|
||||
order(0)
|
||||
{
|
||||
|
||||
}
|
||||
@ -40,32 +43,211 @@ void Core::Archive::open(const QString& account)
|
||||
{
|
||||
if (!opened) {
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + account;
|
||||
int state1 = mkdir(path.toStdString().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
if (state1 != 0 && errno != EEXIST) {
|
||||
qDebug() << "Failed to create account " << account << " database folder";
|
||||
throw 1;
|
||||
path += "/" + account + "/" + jid;
|
||||
QDir cache(path);
|
||||
|
||||
if (!cache.exists()) {
|
||||
bool res = cache.mkpath(path);
|
||||
if (!res) {
|
||||
throw Directory(path.toStdString());
|
||||
}
|
||||
|
||||
path += "/" + jid;
|
||||
|
||||
int state2 = mkdir(path.toStdString().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
if (state2 != 0 && errno != EEXIST) {
|
||||
qDebug() << "Failed to create " << jid.toStdString().c_str() << " database folder in account" << account;
|
||||
throw 1;
|
||||
}
|
||||
|
||||
environment.set_mapsize(1UL * 1024UL * 1024UL * 1024UL);
|
||||
environment.set_max_dbs(10);
|
||||
environment.set_max_dbs(2);
|
||||
environment.open(path.toStdString().c_str(), 0, 0664);
|
||||
|
||||
lmdb::txn wTrans = lmdb::txn::begin(environment);
|
||||
dbi = lmdb::dbi::open(wTrans, "main", MDB_CREATE);
|
||||
order = lmdb::dbi::open(wTrans, "order", MDB_CREATE | MDB_INTEGERKEY);
|
||||
wTrans.commit();
|
||||
opened = true;
|
||||
}
|
||||
}
|
||||
|
||||
QString Core::Archive::addElement(const Shared::Message& message)
|
||||
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();
|
||||
|
||||
qDebug() << "inserting element with id " << id.c_str();
|
||||
|
||||
lmdb::val key((quint8*)id.c_str(), 36);
|
||||
lmdb::val value(ba.data(), ba.size());
|
||||
lmdb::txn wTrans = lmdb::txn::begin(environment);
|
||||
bool result = dbi.put(wTrans, key, value);
|
||||
if (result) {
|
||||
lmdb::val oKey((quint8*) &stamp, 8);
|
||||
bool oResult = order.put(wTrans, oKey, key);
|
||||
if (!oResult) {
|
||||
qDebug() << "An element couldn't be inserted into the index";
|
||||
}
|
||||
} else {
|
||||
qDebug() << "An element couldn't been added to the archive, skipping";
|
||||
}
|
||||
wTrans.commit();
|
||||
}
|
||||
|
||||
void Core::Archive::clear()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("clear", jid.toStdString());
|
||||
}
|
||||
|
||||
lmdb::txn transaction = lmdb::txn::begin(environment);
|
||||
dbi.drop(transaction);
|
||||
order.drop(transaction);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::getElement(const QString& id)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("getElement", jid.toStdString());
|
||||
}
|
||||
|
||||
qDebug() << "getting an element with id " << id.toStdString().c_str();
|
||||
lmdb::txn rtxn = lmdb::txn::begin(environment);
|
||||
lmdb::val key((quint8*)id.toStdString().c_str(), 36);
|
||||
lmdb::val value;
|
||||
|
||||
|
||||
int rc = 0;
|
||||
char *c_key=(char *)id.toStdString().c_str();
|
||||
MDB_val d_key, data;
|
||||
data.mv_data = nullptr;
|
||||
data.mv_size = 0;
|
||||
MDB_txn *txn = nullptr;
|
||||
rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
d_key.mv_size = key.size();
|
||||
d_key.mv_data = c_key;
|
||||
rc= mdb_get(txn,dbi ,&d_key, &data);
|
||||
if (rc) {
|
||||
qDebug() <<"Data Can't be Found, Error: "<<mdb_strerror(rc);
|
||||
}
|
||||
else if(rc==0)
|
||||
qDebug() << "Data Found.\n";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
lmdb::cursor cursor = lmdb::cursor::open(rtxn, dbi);
|
||||
lmdb::val tKey;
|
||||
lmdb::val tValue;
|
||||
|
||||
while(cursor.get(tKey, tValue, MDB_NEXT)) {
|
||||
std::string sId(tKey.data(), tKey.size());
|
||||
qDebug() << "comparing " << id.toStdString().c_str() << " with " << sId.c_str();
|
||||
if (sId == id.toStdString()) {
|
||||
qDebug() << "EQUALS";
|
||||
} else {
|
||||
qDebug() << "NOT";
|
||||
}
|
||||
qDebug() << "sizes: " << key.size() << " : " << tKey.size();
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (dbi.get(rtxn, key, value)) {
|
||||
QByteArray ba(value.data(), value.size());
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
|
||||
Shared::Message msg;
|
||||
msg.deserialize(ds);
|
||||
rtxn.abort();
|
||||
|
||||
return msg;
|
||||
} else {
|
||||
rtxn.abort();
|
||||
throw NotFound(id.toStdString(), jid.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::newest()
|
||||
{
|
||||
QString id = newestId();
|
||||
return getElement(id);
|
||||
}
|
||||
|
||||
QString Core::Archive::newestId()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("newestId", jid.toStdString());
|
||||
}
|
||||
lmdb::txn rtxn = lmdb::txn::begin(environment, nullptr, MDB_RDONLY);
|
||||
lmdb::cursor cursor = lmdb::cursor::open(rtxn, order);
|
||||
lmdb::val key;
|
||||
lmdb::val value;
|
||||
|
||||
bool result = cursor.get(key, value, MDB_LAST);
|
||||
if (result) {
|
||||
std::string sId(value.data(), 36);
|
||||
cursor.close();
|
||||
rtxn.abort();
|
||||
qDebug() << "newest id is " << sId.c_str();
|
||||
return sId.c_str();
|
||||
} else {
|
||||
throw new Empty(jid.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
Shared::Message Core::Archive::oldest()
|
||||
{
|
||||
return getElement(oldestId());
|
||||
}
|
||||
|
||||
QString Core::Archive::oldestId()
|
||||
{
|
||||
if (!opened) {
|
||||
throw Closed("oldestId", jid.toStdString());
|
||||
}
|
||||
lmdb::txn rtxn = lmdb::txn::begin(environment, nullptr, MDB_RDONLY);
|
||||
lmdb::cursor cursor = lmdb::cursor::open(rtxn, order);
|
||||
lmdb::val key;
|
||||
lmdb::val value;
|
||||
|
||||
bool result = cursor.get(key, value, MDB_FIRST);
|
||||
if (result) {
|
||||
std::string sId;
|
||||
sId.assign(value.data(), value.size());
|
||||
cursor.close();
|
||||
rtxn.abort();
|
||||
qDebug() << "Oldest id is " << sId.c_str();
|
||||
return sId.c_str();
|
||||
} else {
|
||||
throw new Empty(jid.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
long unsigned int Core::Archive::size() const
|
||||
{
|
||||
lmdb::txn rtxn = lmdb::txn::begin(environment, nullptr, MDB_RDONLY);
|
||||
long unsigned int s = order.size(rtxn);
|
||||
rtxn.abort();
|
||||
return s;
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include <QObject>
|
||||
#include "../global.h"
|
||||
#include "lmdb++.h"
|
||||
#include "../exception.h"
|
||||
|
||||
namespace Core {
|
||||
|
||||
@ -29,27 +30,75 @@ class Archive : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Archive(const QString& jid, QObject* parent);
|
||||
Archive(const QString& jid, QObject* parent = 0);
|
||||
~Archive();
|
||||
|
||||
void open(const QString& account);
|
||||
|
||||
QString addElement(const Shared::Message& message);
|
||||
Shared::Message getElement(const QString& id) const;
|
||||
Shared::Message oldest() const;
|
||||
Shared::Message newest() const;
|
||||
void removeElement(const QString& id);
|
||||
void addElement(const Shared::Message& message);
|
||||
Shared::Message getElement(const QString& id);
|
||||
Shared::Message oldest();
|
||||
QString oldestId();
|
||||
Shared::Message newest();
|
||||
QString newestId();
|
||||
void clear();
|
||||
void modifyElement(const QString& id, const Shared::Message& newValue);
|
||||
unsigned int size() const;
|
||||
long unsigned int size() const;
|
||||
|
||||
public:
|
||||
const QString jid;
|
||||
|
||||
public:
|
||||
class Directory:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Directory(const std::string& p_path):Exception(), path(p_path){}
|
||||
|
||||
std::string getMessage() const{return "Can't create directory for database at " + path;}
|
||||
private:
|
||||
std::string path;
|
||||
};
|
||||
|
||||
class Closed:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Closed(const std::string& op, const std::string& acc):Exception(), operation(op), account(acc){}
|
||||
|
||||
std::string getMessage() const{return "An attempt to perform operation " + operation + " on closed archive for " + account;}
|
||||
private:
|
||||
std::string operation;
|
||||
std::string account;
|
||||
};
|
||||
|
||||
class NotFound:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
NotFound(const std::string& k, const std::string& acc):Exception(), key(k), account(acc){}
|
||||
|
||||
std::string getMessage() const{return "Element for id " + key + " wasn't found in database " + account;}
|
||||
private:
|
||||
std::string key;
|
||||
std::string account;
|
||||
};
|
||||
|
||||
class Empty:
|
||||
public Utils::Exception
|
||||
{
|
||||
public:
|
||||
Empty(const std::string& acc):Exception(), account(acc){}
|
||||
|
||||
std::string getMessage() const{return "An attempt to read ordered elements from database " + account + " but it's empty";}
|
||||
private:
|
||||
std::string account;
|
||||
};
|
||||
|
||||
private:
|
||||
bool opened;
|
||||
lmdb::env environment;
|
||||
lmdb::dbi dbi;
|
||||
lmdb::dbi order;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1558,7 +1558,7 @@ public:
|
||||
lmdb::val v{};
|
||||
const bool result = lmdb::dbi_get(txn, handle(), k, v);
|
||||
if (result) {
|
||||
val = *v.data<const V>();
|
||||
val = std::move(*v.data<const V>());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
35
global.cpp
35
global.cpp
@ -179,7 +179,7 @@ void Shared::Message::generateRandomId()
|
||||
uuid_t uuid;
|
||||
uuid_generate(uuid);
|
||||
|
||||
char uuid_str[37];
|
||||
char uuid_str[36];
|
||||
uuid_unparse_lower(uuid, uuid_str);
|
||||
id = uuid_str;
|
||||
}
|
||||
@ -208,3 +208,36 @@ void Shared::Message::setType(Shared::Message::Type t)
|
||||
{
|
||||
type = t;
|
||||
}
|
||||
|
||||
void Shared::Message::serialize(QDataStream& data) const
|
||||
{
|
||||
data << jFrom;
|
||||
data << rFrom;
|
||||
data << jTo;
|
||||
data << rTo;
|
||||
data << id;
|
||||
data << body;
|
||||
data << time;
|
||||
data << thread;
|
||||
quint8 t = type;
|
||||
data << t;
|
||||
data << outgoing;
|
||||
data << forwarded;
|
||||
}
|
||||
|
||||
void Shared::Message::deserialize(QDataStream& data)
|
||||
{
|
||||
data >> jFrom;
|
||||
data >> rFrom;
|
||||
data >> jTo;
|
||||
data >> rTo;
|
||||
data >> id;
|
||||
data >> body;
|
||||
data >> time;
|
||||
data >> thread;
|
||||
quint8 t;
|
||||
data >> t;
|
||||
type = static_cast<Type>(t);
|
||||
data >> outgoing;
|
||||
data >> forwarded;
|
||||
}
|
||||
|
4
global.h
4
global.h
@ -4,6 +4,7 @@
|
||||
#include <QString>
|
||||
#include <deque>
|
||||
#include <QDateTime>
|
||||
#include <QDataStream>
|
||||
|
||||
namespace Shared {
|
||||
|
||||
@ -96,6 +97,9 @@ public:
|
||||
QString getPenPalResource() const;
|
||||
void generateRandomId();
|
||||
|
||||
void serialize(QDataStream& data) const;
|
||||
void deserialize(QDataStream& data);
|
||||
|
||||
private:
|
||||
QString jFrom;
|
||||
QString rFrom;
|
||||
|
27
main.cpp
27
main.cpp
@ -5,6 +5,7 @@
|
||||
#include <QtCore/QThread>
|
||||
#include <QtCore/QObject>
|
||||
#include <QSettings>
|
||||
#include "core/archive.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@ -56,6 +57,32 @@ int main(int argc, char *argv[])
|
||||
|
||||
//qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
|
||||
|
||||
Core::Archive ar("test1@macaw.me");
|
||||
ar.open("Test");
|
||||
Shared::Message msg1;
|
||||
msg1.generateRandomId();
|
||||
msg1.setBody("oldest");
|
||||
msg1.setTime(QDateTime::currentDateTime().addDays(-7));
|
||||
Shared::Message msg2;
|
||||
msg2.generateRandomId();
|
||||
msg2.setBody("Middle");
|
||||
msg2.setTime(QDateTime::currentDateTime().addDays(-4));
|
||||
Shared::Message msg3;
|
||||
msg3.generateRandomId();
|
||||
msg3.setBody("newest");
|
||||
msg3.setTime(QDateTime::currentDateTime());
|
||||
|
||||
ar.addElement(msg2);
|
||||
ar.addElement(msg3);
|
||||
ar.addElement(msg1);
|
||||
|
||||
Shared::Message d0 = ar.getElement(msg1.getId());
|
||||
|
||||
Shared::Message d1 = ar.newest();
|
||||
Shared::Message d2 = ar.oldest();
|
||||
|
||||
qDebug() << d1.getBody() << ", " << d2.getBody();
|
||||
|
||||
coreThread->start();
|
||||
|
||||
int result = app.exec();
|
||||
|
Loading…
Reference in New Issue
Block a user