From 233b3c06ce79ee1c4445863430c0ebbee1f8faf1 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 1 Jul 2020 20:15:39 +0200 Subject: [PATCH] Store events in room specific db --- src/Cache.cpp | 30 ++++++++++++++++++++---------- src/Cache_p.h | 5 +++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index d9d1134e..27f4e694 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1955,24 +1955,34 @@ Cache::saveTimelineMessages(lmdb::txn &txn, const mtx::responses::Timeline &res) { auto db = getMessagesDb(txn, room_id); + auto eventsDb = getEventsDb(txn, room_id); using namespace mtx::events; using namespace mtx::events::state; for (const auto &e : res.events) { - if (std::holds_alternative>(e)) - continue; + auto event = mtx::accessors::serialize_event(e); + if (auto redaction = + std::get_if>(&e)) { + lmdb::dbi_put( + txn, eventsDb, lmdb::val(redaction->redacts), lmdb::val(event.dump())); + } else { + json obj = json::object(); - json obj = json::object(); + obj["event"] = event; + obj["token"] = res.prev_batch; - obj["event"] = mtx::accessors::serialize_event(e); - obj["token"] = res.prev_batch; + lmdb::dbi_put( + txn, + db, + lmdb::val(std::to_string(event["origin_server_ts"].get())), + lmdb::val(obj.dump())); - lmdb::dbi_put( - txn, - db, - lmdb::val(std::to_string(obj["event"]["origin_server_ts"].get())), - lmdb::val(obj.dump())); + lmdb::dbi_put(txn, + eventsDb, + lmdb::val(event["event_id"].get()), + lmdb::val(event.dump())); + } } } diff --git a/src/Cache_p.h b/src/Cache_p.h index 892b66a5..e657447b 100644 --- a/src/Cache_p.h +++ b/src/Cache_p.h @@ -411,6 +411,11 @@ private: return db; } + lmdb::dbi getEventsDb(lmdb::txn &txn, const std::string &room_id) + { + return lmdb::dbi::open(txn, std::string(room_id + "/events").c_str(), MDB_CREATE); + } + lmdb::dbi getInviteStatesDb(lmdb::txn &txn, const std::string &room_id) { return lmdb::dbi::open(