diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2022-11-01 21:13:11 +0100 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2022-11-01 21:19:58 +0100 |
commit | 54931cb21b080d079a80978e88e8e79e6c43196f (patch) | |
tree | ff8508f758619a508cb1c51806ad169cccf58b06 /src | |
parent | Speedup sending encrypted messages after metasync was reenabled (diff) | |
download | nheko-54931cb21b080d079a80978e88e8e79e6c43196f.tar.xz |
Optimize fetching olm session from the db
Diffstat (limited to 'src')
-rw-r--r-- | src/Cache.cpp | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp index 2784cf50..cb4ee0b0 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -962,19 +962,20 @@ Cache::getOlmSession(const std::string &curve25519, const std::string &session_i { using namespace mtx::crypto; - auto txn = lmdb::txn::begin(env_); - auto db = getOlmSessionsDb(txn, curve25519); + try { + auto txn = ro_txn(env_); + auto db = getOlmSessionsDb(txn, curve25519); - std::string_view pickled; - bool found = db.get(txn, session_id, pickled); + std::string_view 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) { - auto data = nlohmann::json::parse(pickled).get<StoredOlmSession>(); - return unpickle<SessionObject>(data.pickled_session, pickle_secret_); + } catch (...) { } - return std::nullopt; } @@ -983,26 +984,28 @@ Cache::getLatestOlmSession(const std::string &curve25519) { using namespace mtx::crypto; - auto txn = lmdb::txn::begin(env_); - auto db = getOlmSessionsDb(txn, curve25519); - - std::string_view session_id, pickled_session; + try { + auto txn = ro_txn(env_); + auto db = getOlmSessionsDb(txn, curve25519); - std::optional<StoredOlmSession> currentNewest; + std::string_view session_id, pickled_session; - auto cursor = lmdb::cursor::open(txn, db); - while (cursor.get(session_id, pickled_session, MDB_NEXT)) { - auto data = nlohmann::json::parse(pickled_session).get<StoredOlmSession>(); - if (!currentNewest || currentNewest->last_message_ts < data.last_message_ts) - currentNewest = data; - } - cursor.close(); + std::optional<StoredOlmSession> currentNewest; - txn.commit(); + auto cursor = lmdb::cursor::open(txn, db); + while (cursor.get(session_id, pickled_session, MDB_NEXT)) { + auto data = nlohmann::json::parse(pickled_session).get<StoredOlmSession>(); + if (!currentNewest || currentNewest->last_message_ts < data.last_message_ts) + currentNewest = data; + } + cursor.close(); - return currentNewest ? std::optional(unpickle<SessionObject>(currentNewest->pickled_session, - pickle_secret_)) - : std::nullopt; + return currentNewest ? std::optional(unpickle<SessionObject>(currentNewest->pickled_session, + pickle_secret_)) + : std::nullopt; + } catch (...) { + return std::nullopt; + } } std::vector<std::string> |