Persist event order

This commit is contained in:
Nicolas Werner 2020-07-04 02:09:12 +02:00
parent 233b3c06ce
commit 79a29953dd
2 changed files with 26 additions and 0 deletions

View file

@ -1957,9 +1957,21 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
auto db = getMessagesDb(txn, room_id); auto db = getMessagesDb(txn, room_id);
auto eventsDb = getEventsDb(txn, room_id); auto eventsDb = getEventsDb(txn, room_id);
auto orderDb = getEventOrderDb(txn, room_id);
if (res.limited)
lmdb::dbi_drop(txn, orderDb, false);
using namespace mtx::events; using namespace mtx::events;
using namespace mtx::events::state; using namespace mtx::events::state;
lmdb::val indexVal, val;
int64_t index = 0;
auto cursor = lmdb::cursor::open(txn, orderDb);
if (cursor.get(indexVal, val, MDB_LAST)) {
index = *indexVal.data<int64_t>();
}
bool first = true;
for (const auto &e : res.events) { for (const auto &e : res.events) {
auto event = mtx::accessors::serialize_event(e); auto event = mtx::accessors::serialize_event(e);
if (auto redaction = if (auto redaction =
@ -1982,6 +1994,14 @@ Cache::saveTimelineMessages(lmdb::txn &txn,
eventsDb, eventsDb,
lmdb::val(event["event_id"].get<std::string>()), lmdb::val(event["event_id"].get<std::string>()),
lmdb::val(event.dump())); lmdb::val(event.dump()));
++index;
lmdb::cursor_put(cursor.handle(),
lmdb::val(&index, sizeof(index)),
lmdb::val(first ? res.prev_batch : ""),
MDB_APPEND);
first = false;
} }
} }
} }

View file

@ -416,6 +416,12 @@ private:
return lmdb::dbi::open(txn, std::string(room_id + "/events").c_str(), MDB_CREATE); return lmdb::dbi::open(txn, std::string(room_id + "/events").c_str(), MDB_CREATE);
} }
lmdb::dbi getEventOrderDb(lmdb::txn &txn, const std::string &room_id)
{
return lmdb::dbi::open(
txn, std::string(room_id + "/event_order").c_str(), MDB_CREATE | MDB_INTEGERKEY);
}
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(