2017-07-29 11:49:00 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* 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 <stdexcept>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
|
|
#include "Cache.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "RoomState.h"
|
2017-07-29 11:49:00 +03:00
|
|
|
|
|
|
|
static const lmdb::val NEXT_BATCH_KEY("next_batch");
|
|
|
|
static const lmdb::val transactionID("transaction_id");
|
|
|
|
|
|
|
|
Cache::Cache(const QString &userId)
|
2017-11-06 00:04:55 +03:00
|
|
|
: env_{nullptr}
|
|
|
|
, stateDb_{0}
|
|
|
|
, roomDb_{0}
|
|
|
|
, isMounted_{false}
|
|
|
|
, userId_{userId}
|
2017-10-15 22:08:51 +03:00
|
|
|
{}
|
2017-10-03 21:16:31 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Cache::setup()
|
|
|
|
{
|
|
|
|
qDebug() << "Setting up cache";
|
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto statePath = QString("%1/%2/state")
|
|
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
|
|
|
|
.arg(QString::fromUtf8(userId_.toUtf8().toHex()));
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-10-03 21:16:31 +03:00
|
|
|
cacheDirectory_ = QString("%1/%2")
|
|
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
|
|
|
|
.arg(QString::fromUtf8(userId_.toUtf8().toHex()));
|
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
bool isInitial = !QFile::exists(statePath);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
env_ = lmdb::env::create();
|
|
|
|
env_.set_mapsize(128UL * 1024UL * 1024UL); /* 128 MB */
|
|
|
|
env_.set_max_dbs(1024UL);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
if (isInitial) {
|
2017-10-03 21:16:31 +03:00
|
|
|
qDebug() << "First time initializing LMDB";
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
if (!QDir().mkpath(statePath)) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
("Unable to create state directory:" + statePath).toStdString().c_str());
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
|
|
|
env_.open(statePath.toStdString().c_str());
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
if (e.code() != MDB_VERSION_MISMATCH && e.code() != MDB_INVALID) {
|
|
|
|
throw std::runtime_error("LMDB initialization failed" +
|
|
|
|
std::string(e.what()));
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "Resetting cache due to LMDB version mismatch:" << e.what();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
QDir stateDir(statePath);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
for (const auto &file : stateDir.entryList(QDir::NoDotAndDotDot)) {
|
|
|
|
if (!stateDir.remove(file))
|
|
|
|
throw std::runtime_error(
|
|
|
|
("Unable to delete file " + file).toStdString().c_str());
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
env_.open(statePath.toStdString().c_str());
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_);
|
|
|
|
stateDb_ = lmdb::dbi::open(txn, "state", MDB_CREATE);
|
|
|
|
roomDb_ = lmdb::dbi::open(txn, "rooms", MDB_CREATE);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-10-03 21:16:31 +03:00
|
|
|
isMounted_ = true;
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::setState(const QString &nextBatchToken, const QMap<QString, RoomState> &states)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
2017-10-21 21:17:01 +03:00
|
|
|
try {
|
|
|
|
auto txn = lmdb::txn::begin(env_);
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-10-21 21:17:01 +03:00
|
|
|
setNextBatchToken(txn, nextBatchToken);
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-10-28 20:46:34 +03:00
|
|
|
for (auto it = states.constBegin(); it != states.constEnd(); ++it)
|
2017-10-21 21:17:01 +03:00
|
|
|
insertRoomState(txn, it.key(), it.value());
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-10-21 21:17:01 +03:00
|
|
|
txn.commit();
|
|
|
|
} catch (const lmdb::error &e) {
|
|
|
|
qCritical() << "The cache couldn't be updated: " << e.what();
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
deleteData();
|
|
|
|
}
|
2017-08-26 13:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cache::insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state)
|
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
auto stateEvents = state.serialize();
|
2017-08-26 13:49:16 +03:00
|
|
|
auto id = roomid.toUtf8();
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
lmdb::dbi_put(txn, roomDb_, lmdb::val(id.data(), id.size()), lmdb::val(stateEvents));
|
2017-08-26 13:49:16 +03:00
|
|
|
|
|
|
|
for (const auto &membership : state.memberships) {
|
|
|
|
lmdb::dbi membersDb =
|
|
|
|
lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE);
|
|
|
|
|
|
|
|
// The user_id this membership event relates to, is used
|
|
|
|
// as the index on the membership database.
|
2017-12-04 19:41:19 +03:00
|
|
|
auto key = membership.second.state_key;
|
|
|
|
|
|
|
|
// Serialize membership event.
|
|
|
|
nlohmann::json data = membership.second;
|
|
|
|
std::string memberEvent = data.dump();
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
switch (membership.second.content.membership) {
|
2017-10-01 12:11:33 +03:00
|
|
|
// We add or update (e.g invite -> join) a new user to the membership
|
|
|
|
// list.
|
2017-12-04 19:41:19 +03:00
|
|
|
case mtx::events::state::Membership::Invite:
|
|
|
|
case mtx::events::state::Membership::Join: {
|
|
|
|
lmdb::dbi_put(txn, membersDb, lmdb::val(key), lmdb::val(memberEvent));
|
2017-08-26 13:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We remove the user from the membership list.
|
2017-12-04 19:41:19 +03:00
|
|
|
case mtx::events::state::Membership::Leave:
|
|
|
|
case mtx::events::state::Membership::Ban: {
|
|
|
|
lmdb::dbi_del(txn, membersDb, lmdb::val(key), lmdb::val(memberEvent));
|
2017-08-26 13:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-12-04 19:41:19 +03:00
|
|
|
case mtx::events::state::Membership::Knock: {
|
|
|
|
qWarning()
|
|
|
|
<< "Skipping knock membership" << roomid << QString::fromStdString(key);
|
2017-08-26 13:49:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
void
|
|
|
|
Cache::removeRoom(const QString &roomid)
|
|
|
|
{
|
2017-10-03 21:16:31 +03:00
|
|
|
if (!isMounted_)
|
|
|
|
return;
|
|
|
|
|
2017-10-01 19:49:36 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, 0);
|
|
|
|
|
|
|
|
lmdb::dbi_del(txn, roomDb_, lmdb::val(roomid.toUtf8(), roomid.toUtf8().size()), nullptr);
|
|
|
|
|
|
|
|
txn.commit();
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QMap<QString, RoomState>
|
|
|
|
Cache::states()
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
QMap<QString, RoomState> states;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
auto cursor = lmdb::cursor::open(txn, roomDb_);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
std::string room;
|
|
|
|
std::string stateData;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
// Retrieve all the room names.
|
|
|
|
while (cursor.get(room, stateData, MDB_NEXT)) {
|
|
|
|
auto roomid = QString::fromUtf8(room.data(), room.size());
|
2017-12-04 19:41:19 +03:00
|
|
|
auto json = nlohmann::json::parse(stateData);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
RoomState state;
|
2017-12-04 19:41:19 +03:00
|
|
|
state.parse(json);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto memberDb = lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE);
|
2017-12-04 19:41:19 +03:00
|
|
|
std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> members;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
auto memberCursor = lmdb::cursor::open(txn, memberDb);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
std::string memberId;
|
|
|
|
std::string memberContent;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
while (memberCursor.get(memberId, memberContent, MDB_NEXT)) {
|
2017-12-04 19:41:19 +03:00
|
|
|
auto userid = QString::fromStdString(memberId);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
auto data = nlohmann::json::parse(memberContent);
|
|
|
|
mtx::events::StateEvent<mtx::events::state::Member> member = data;
|
|
|
|
members.emplace(memberId, member);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
qWarning() << "Fault while parsing member event" << e.what()
|
|
|
|
<< QString::fromStdString(memberContent);
|
2017-08-26 13:49:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
qDebug() << members.size() << "members for" << roomid;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
state.memberships = members;
|
|
|
|
states.insert(roomid, state);
|
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
qDebug() << "Retrieved" << states.size() << "rooms";
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
cursor.close();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
return states;
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::setNextBatchToken(lmdb::txn &txn, const QString &token)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
auto value = token.toUtf8();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
lmdb::dbi_put(txn, stateDb_, NEXT_BATCH_KEY, lmdb::val(value.data(), value.size()));
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
bool
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::isInitialized() const
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
lmdb::val token;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
bool res = lmdb::dbi_get(txn, stateDb_, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
return res;
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QString
|
2017-08-26 13:49:16 +03:00
|
|
|
Cache::nextBatchToken() const
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
|
|
lmdb::val token;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
lmdb::dbi_get(txn, stateDb_, NEXT_BATCH_KEY, token);
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
txn.commit();
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-08-26 13:49:16 +03:00
|
|
|
return QString::fromUtf8(token.data(), token.size());
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
2017-10-22 19:03:55 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Cache::deleteData()
|
|
|
|
{
|
|
|
|
qInfo() << "Deleting cache data";
|
|
|
|
|
|
|
|
if (!cacheDirectory_.isEmpty())
|
|
|
|
QDir(cacheDirectory_).removeRecursively();
|
|
|
|
}
|