Optimize fetching olm session from the db

This commit is contained in:
Nicolas Werner 2022-11-01 21:13:11 +01:00
parent 676a6506cb
commit 54931cb21b
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9

View file

@ -962,19 +962,20 @@ Cache::getOlmSession(const std::string &curve25519, const std::string &session_i
{ {
using namespace mtx::crypto; using namespace mtx::crypto;
auto txn = lmdb::txn::begin(env_); try {
auto db = getOlmSessionsDb(txn, curve25519); auto txn = ro_txn(env_);
auto db = getOlmSessionsDb(txn, curve25519);
std::string_view pickled; std::string_view pickled;
bool found = db.get(txn, session_id, pickled); bool found = db.get(txn, session_id, pickled);
txn.commit(); if (found) {
auto data = nlohmann::json::parse(pickled).get<StoredOlmSession>();
return unpickle<SessionObject>(data.pickled_session, pickle_secret_);
}
if (found) { } catch (...) {
auto data = nlohmann::json::parse(pickled).get<StoredOlmSession>();
return unpickle<SessionObject>(data.pickled_session, pickle_secret_);
} }
return std::nullopt; return std::nullopt;
} }
@ -983,26 +984,28 @@ Cache::getLatestOlmSession(const std::string &curve25519)
{ {
using namespace mtx::crypto; using namespace mtx::crypto;
auto txn = lmdb::txn::begin(env_); try {
auto db = getOlmSessionsDb(txn, curve25519); auto txn = ro_txn(env_);
auto db = getOlmSessionsDb(txn, curve25519);
std::string_view session_id, pickled_session; std::string_view session_id, pickled_session;
std::optional<StoredOlmSession> currentNewest; std::optional<StoredOlmSession> currentNewest;
auto cursor = lmdb::cursor::open(txn, db); auto cursor = lmdb::cursor::open(txn, db);
while (cursor.get(session_id, pickled_session, MDB_NEXT)) { while (cursor.get(session_id, pickled_session, MDB_NEXT)) {
auto data = nlohmann::json::parse(pickled_session).get<StoredOlmSession>(); auto data = nlohmann::json::parse(pickled_session).get<StoredOlmSession>();
if (!currentNewest || currentNewest->last_message_ts < data.last_message_ts) if (!currentNewest || currentNewest->last_message_ts < data.last_message_ts)
currentNewest = data; currentNewest = data;
}
cursor.close();
return currentNewest ? std::optional(unpickle<SessionObject>(currentNewest->pickled_session,
pickle_secret_))
: std::nullopt;
} catch (...) {
return std::nullopt;
} }
cursor.close();
txn.commit();
return currentNewest ? std::optional(unpickle<SessionObject>(currentNewest->pickled_session,
pickle_secret_))
: std::nullopt;
} }
std::vector<std::string> std::vector<std::string>