Store events in room specific db

This commit is contained in:
Nicolas Werner 2020-07-01 20:15:39 +02:00
parent d72eb5eb2d
commit 233b3c06ce
2 changed files with 25 additions and 10 deletions

View file

@ -1955,24 +1955,34 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
const mtx::responses::Timeline &res) const mtx::responses::Timeline &res)
{ {
auto db = getMessagesDb(txn, room_id); auto db = getMessagesDb(txn, room_id);
auto eventsDb = getEventsDb(txn, room_id);
using namespace mtx::events; using namespace mtx::events;
using namespace mtx::events::state; using namespace mtx::events::state;
for (const auto &e : res.events) { for (const auto &e : res.events) {
if (std::holds_alternative<RedactionEvent<msg::Redaction>>(e)) auto event = mtx::accessors::serialize_event(e);
continue; if (auto redaction =
std::get_if<mtx::events::RedactionEvent<mtx::events::msg::Redaction>>(&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); lmdb::dbi_put(
obj["token"] = res.prev_batch; txn,
db,
lmdb::val(std::to_string(event["origin_server_ts"].get<uint64_t>())),
lmdb::val(obj.dump()));
lmdb::dbi_put( lmdb::dbi_put(txn,
txn, eventsDb,
db, lmdb::val(event["event_id"].get<std::string>()),
lmdb::val(std::to_string(obj["event"]["origin_server_ts"].get<uint64_t>())), lmdb::val(event.dump()));
lmdb::val(obj.dump())); }
} }
} }

View file

@ -411,6 +411,11 @@ private:
return db; 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) lmdb::dbi getInviteStatesDb(lmdb::txn &txn, const std::string &room_id)
{ {
return lmdb::dbi::open( return lmdb::dbi::open(