got rid of lmdb++, but it still doesn't work

This commit is contained in:
Blue 2019-04-16 19:29:24 +03:00
parent 0b4714688f
commit 7f2657a5b6
3 changed files with 111 additions and 2029 deletions

View File

@ -28,11 +28,11 @@ Core::Archive::Archive(const QString& p_jid, QObject* parent):
QObject(parent), QObject(parent),
jid(p_jid), jid(p_jid),
opened(false), opened(false),
environment(lmdb::env::create()), environment(),
dbi(0), main(),
order(0) order()
{ {
mdb_env_create(&environment);
} }
Core::Archive::~Archive() Core::Archive::~Archive()
@ -53,14 +53,27 @@ void Core::Archive::open(const QString& account)
} }
} }
environment.set_mapsize(1UL * 1024UL * 1024UL * 1024UL); mdb_env_set_maxdbs(environment, 2);
environment.set_max_dbs(2); mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
environment.open(path.toStdString().c_str(), 0, 0664);
lmdb::txn wTrans = lmdb::txn::begin(environment); int rc;
dbi = lmdb::dbi::open(wTrans, "main", MDB_CREATE); MDB_txn *txn;
order = lmdb::dbi::open(wTrans, "order", MDB_CREATE | MDB_INTEGERKEY); rc = mdb_txn_begin(environment, NULL, 0, &txn);
wTrans.commit(); if (rc) {
qDebug() << "opening transaction error " << mdb_strerror(rc);
}
rc = mdb_dbi_open(txn, "main", MDB_CREATE, &main);
if (rc) {
qDebug() << "main opening error " << mdb_strerror(rc);
}
rc = mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order);
if (rc) {
qDebug() << "order opening error " << mdb_strerror(rc);
}
rc = mdb_txn_commit(txn);
if (rc) {
qDebug() << "opening commit transaction error " << mdb_strerror(rc);
}
opened = true; opened = true;
} }
} }
@ -77,21 +90,36 @@ void Core::Archive::addElement(const Shared::Message& message)
const std::string& id = message.getId().toStdString(); const std::string& id = message.getId().toStdString();
qDebug() << "inserting element with id " << id.c_str(); qDebug() << "inserting element with id " << id.c_str();
qDebug() << "data size is " << ba.size();
lmdb::val key((quint8*)id.c_str(), 36); MDB_val lmdbKey, lmdbData;
lmdb::val value(ba.data(), ba.size()); lmdbKey.mv_size = id.size();
lmdb::txn wTrans = lmdb::txn::begin(environment); lmdbKey.mv_data = (uint8_t*)id.c_str();
bool result = dbi.put(wTrans, key, value); lmdbData.mv_size = ba.size();
if (result) { lmdbData.mv_data = (uint8_t*)ba.data();
lmdb::val oKey((quint8*) &stamp, 8); MDB_txn *txn;
bool oResult = order.put(wTrans, oKey, key); mdb_txn_begin(environment, NULL, 0, &txn);
if (!oResult) { int rc;
qDebug() << "An element couldn't be inserted into the index"; 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);
}
} }
} else { } else {
qDebug() << "An element couldn't been added to the archive, skipping"; qDebug() << "An element couldn't been added to the archive, skipping" << mdb_strerror(rc);
mdb_txn_abort(txn);
} }
wTrans.commit();
} }
void Core::Archive::clear() void Core::Archive::clear()
@ -100,10 +128,11 @@ void Core::Archive::clear()
throw Closed("clear", jid.toStdString()); throw Closed("clear", jid.toStdString());
} }
lmdb::txn transaction = lmdb::txn::begin(environment); MDB_txn *txn;
dbi.drop(transaction); mdb_txn_begin(environment, NULL, 0, &txn);
order.drop(transaction); mdb_drop(txn, main, 0);
transaction.commit(); mdb_drop(txn, order, 0);
mdb_txn_commit(txn);
} }
Shared::Message Core::Archive::getElement(const QString& id) Shared::Message Core::Archive::getElement(const QString& id)
@ -113,78 +142,32 @@ Shared::Message Core::Archive::getElement(const QString& id)
} }
qDebug() << "getting an element with id " << id.toStdString().c_str(); 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;
MDB_val lmdbKey, lmdbData;
lmdbKey.mv_size = id.toStdString().size();
lmdbKey.mv_data = (uint8_t*)id.toStdString().c_str();
int rc = 0; MDB_txn *txn;
char *c_key=(char *)id.toStdString().c_str(); int rc;
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); 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) { if (rc) {
qDebug() <<"Data Can't be Found, Error: "<<mdb_strerror(rc); qDebug() <<"Get error: " << mdb_strerror(rc);
mdb_txn_abort(txn);
throw NotFound(id.toStdString(), jid.toStdString());
} }
else if(rc==0) rc = mdb_get(txn, main, &lmdbKey, &lmdbData);
qDebug() << "Data Found.\n"; 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);
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); QDataStream ds(&ba, QIODevice::ReadOnly);
Shared::Message msg; Shared::Message msg;
msg.deserialize(ds); msg.deserialize(ds);
rtxn.abort(); mdb_txn_abort(txn);
return msg; return msg;
} else {
rtxn.abort();
throw NotFound(id.toStdString(), jid.toStdString());
} }
} }
@ -199,19 +182,24 @@ QString Core::Archive::newestId()
if (!opened) { if (!opened) {
throw Closed("newestId", jid.toStdString()); throw Closed("newestId", jid.toStdString());
} }
lmdb::txn rtxn = lmdb::txn::begin(environment, nullptr, MDB_RDONLY); MDB_txn *txn;
lmdb::cursor cursor = lmdb::cursor::open(rtxn, order); int rc;
lmdb::val key; rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
lmdb::val value; MDB_cursor* cursor;
rc = mdb_cursor_open(txn, order, &cursor);
MDB_val lmdbKey, lmdbData;
bool result = cursor.get(key, value, MDB_LAST); rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
if (result) { if (rc) {
std::string sId(value.data(), 36); std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
cursor.close(); mdb_cursor_close(cursor);
rtxn.abort(); mdb_txn_abort(txn);
qDebug() << "newest id is " << sId.c_str(); qDebug() << "newest id is " << sId.c_str();
return sId.c_str(); return sId.c_str();
} else { } else {
qDebug() << "Error geting newestId " << mdb_strerror(rc);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw new Empty(jid.toStdString()); throw new Empty(jid.toStdString());
} }
} }
@ -226,28 +214,35 @@ QString Core::Archive::oldestId()
if (!opened) { if (!opened) {
throw Closed("oldestId", jid.toStdString()); throw Closed("oldestId", jid.toStdString());
} }
lmdb::txn rtxn = lmdb::txn::begin(environment, nullptr, MDB_RDONLY); MDB_txn *txn;
lmdb::cursor cursor = lmdb::cursor::open(rtxn, order); int rc;
lmdb::val key; rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
lmdb::val value; MDB_cursor* cursor;
rc = mdb_cursor_open(txn, order, &cursor);
MDB_val lmdbKey, lmdbData;
bool result = cursor.get(key, value, MDB_FIRST); rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
if (result) { if (rc) {
std::string sId; std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size);
sId.assign(value.data(), value.size()); mdb_cursor_close(cursor);
cursor.close(); mdb_txn_abort(txn);
rtxn.abort(); qDebug() << "oldest id is " << sId.c_str();
qDebug() << "Oldest id is " << sId.c_str();
return sId.c_str(); return sId.c_str();
} else { } else {
qDebug() << "Error geting oldestId " << mdb_strerror(rc);
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw new Empty(jid.toStdString()); throw new Empty(jid.toStdString());
} }
} }
long unsigned int Core::Archive::size() const long unsigned int Core::Archive::size() const
{ {
lmdb::txn rtxn = lmdb::txn::begin(environment, nullptr, MDB_RDONLY); MDB_txn *txn;
long unsigned int s = order.size(rtxn); int rc;
rtxn.abort(); rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
return s; MDB_stat stat;
mdb_stat(txn, order, &stat);
mdb_txn_abort(txn);
return stat.ms_entries;
} }

View File

@ -21,7 +21,7 @@
#include <QObject> #include <QObject>
#include "../global.h" #include "../global.h"
#include "lmdb++.h" #include <lmdb.h>
#include "../exception.h" #include "../exception.h"
namespace Core { namespace Core {
@ -96,9 +96,9 @@ public:
private: private:
bool opened; bool opened;
lmdb::env environment; MDB_env* environment;
lmdb::dbi dbi; MDB_dbi main;
lmdb::dbi order; MDB_dbi order;
}; };
} }

File diff suppressed because it is too large Load Diff