From 9322abd36f7e8d2e52a76ad4ddd8f5895d865ddb Mon Sep 17 00:00:00 2001 From: ANIR1604 Date: Sun, 8 Dec 2024 00:03:55 +0530 Subject: [PATCH 1/4] Optimising the Db search by cache --- .../src/crypto_store/mod.rs | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs index 5f4c05532c6..f1723602752 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs @@ -46,6 +46,11 @@ use tokio::sync::Mutex; use tracing::{debug, warn}; use wasm_bindgen::JsValue; use web_sys::IdbKeyRange; +use std::collections::HashMap; +use std::sync::Arc; +use tokio::sync::RwLock; + +pub type RoomSettingsCache = Arc>>>; use self::indexeddb_serializer::MaybeEncrypted; use crate::crypto_store::{ @@ -1314,16 +1319,35 @@ impl_crypto_store! { } } - async fn get_room_settings(&self, room_id: &RoomId) -> Result> { - let key = self.serializer.encode_key(keys::ROOM_SETTINGS, room_id); - self - .inner - .transaction_on_one_with_mode(keys::ROOM_SETTINGS, IdbTransactionMode::Readonly)? - .object_store(keys::ROOM_SETTINGS)? + pub async fn get_room_settings( + room_id: &RoomId, + serializer: &Serializer, + inner: &DatabaseInner, + cache: &RoomSettingsCache, + ) -> Result, String> { + let room_id_str = room_id.to_string(); + + // Check if room settings exist in cache + if let Some(cached) = cache.read().await.get(&room_id_str).cloned() { + return Ok(cached); + } + + // Fetch from database if not in cache + let key = serializer.encode_key("ROOM_SETTINGS", room_id); + let result = inner + .transaction_on_one_with_mode("ROOM_SETTINGS", IdbTransactionMode::Readonly)? + .object_store("ROOM_SETTINGS")? .get(&key)? .await? - .map(|v| self.serializer.deserialize_value(v)) - .transpose() + .map(|v| serializer.deserialize_value(v)) + .transpose(); + + // Cache the result + if let Ok(settings) = &result { + cache.write().await.insert(room_id_str, settings.clone()); + } + + result } async fn get_custom_value(&self, key: &str) -> Result>> { From ddf9f7efcdca76404893dc4ab0e60560fc49f927 Mon Sep 17 00:00:00 2001 From: Anirban Biswas <139000437+ANIR1604@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:25:39 +0530 Subject: [PATCH 2/4] Grouped operations, simplified error handling, early returns. Signed-off-by: Anirban Biswas <139000437+ANIR1604@users.noreply.github.com> --- .../src/crypto_store/mod.rs | 52 +++++++------------ 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs index f1723602752..0fdd025ad0c 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs @@ -46,11 +46,6 @@ use tokio::sync::Mutex; use tracing::{debug, warn}; use wasm_bindgen::JsValue; use web_sys::IdbKeyRange; -use std::collections::HashMap; -use std::sync::Arc; -use tokio::sync::RwLock; - -pub type RoomSettingsCache = Arc>>>; use self::indexeddb_serializer::MaybeEncrypted; use crate::crypto_store::{ @@ -1319,36 +1314,25 @@ impl_crypto_store! { } } - pub async fn get_room_settings( - room_id: &RoomId, - serializer: &Serializer, - inner: &DatabaseInner, - cache: &RoomSettingsCache, - ) -> Result, String> { - let room_id_str = room_id.to_string(); - - // Check if room settings exist in cache - if let Some(cached) = cache.read().await.get(&room_id_str).cloned() { - return Ok(cached); - } - - // Fetch from database if not in cache - let key = serializer.encode_key("ROOM_SETTINGS", room_id); - let result = inner - .transaction_on_one_with_mode("ROOM_SETTINGS", IdbTransactionMode::Readonly)? - .object_store("ROOM_SETTINGS")? - .get(&key)? - .await? - .map(|v| serializer.deserialize_value(v)) - .transpose(); - - // Cache the result - if let Ok(settings) = &result { - cache.write().await.insert(room_id_str, settings.clone()); - } - + async fn get_room_settings(&self, room_id: &RoomId) -> Result> { + let key = self.serializer.encode_key(keys::ROOM_SETTINGS, room_id); + + // Get the transaction with the correct mode + let transaction = self + .inner + .transaction_on_one_with_mode(keys::ROOM_SETTINGS, IdbTransactionMode::Readonly)?; + + // Get the object store and the value associated with the key + let result = transaction + .object_store(keys::ROOM_SETTINGS)? + .get(&key) + .await?; + + // Deserialize and return the result, returning Option directly if there's no value result - } + .map(|v| self.serializer.deserialize_value(v)) + .transpose() +} async fn get_custom_value(&self, key: &str) -> Result>> { self From 0897f76b271083174975d3feda6670a081942ab1 Mon Sep 17 00:00:00 2001 From: Anirban Biswas <139000437+ANIR1604@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:33:29 +0530 Subject: [PATCH 3/4] Optimizing the get_room_settings Signed-off-by: Anirban Biswas <139000437+ANIR1604@users.noreply.github.com> --- crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs index 0fdd025ad0c..d94e66dc737 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs @@ -1325,11 +1325,12 @@ impl_crypto_store! { // Get the object store and the value associated with the key let result = transaction .object_store(keys::ROOM_SETTINGS)? - .get(&key) - .await?; + .get(&key)?; + + let value = result.await?; // Deserialize and return the result, returning Option directly if there's no value - result + value .map(|v| self.serializer.deserialize_value(v)) .transpose() } From 28f55d08fa603ec84a4c81e449350da07d46d010 Mon Sep 17 00:00:00 2001 From: Anirban Biswas <139000437+ANIR1604@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:14:23 +0530 Subject: [PATCH 4/4] Update mod.rs Signed-off-by: Anirban Biswas <139000437+ANIR1604@users.noreply.github.com> --- .../src/crypto_store/mod.rs | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs index d94e66dc737..a999074edb5 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs @@ -1315,25 +1315,16 @@ impl_crypto_store! { } async fn get_room_settings(&self, room_id: &RoomId) -> Result> { - let key = self.serializer.encode_key(keys::ROOM_SETTINGS, room_id); - - // Get the transaction with the correct mode - let transaction = self - .inner - .transaction_on_one_with_mode(keys::ROOM_SETTINGS, IdbTransactionMode::Readonly)?; - - // Get the object store and the value associated with the key - let result = transaction - .object_store(keys::ROOM_SETTINGS)? - .get(&key)?; - - let value = result.await?; - - // Deserialize and return the result, returning Option directly if there's no value - value - .map(|v| self.serializer.deserialize_value(v)) - .transpose() -} + let key = self.serializer.encode_key(keys::ROOM_SETTINGS, room_id); + self + .inner + .transaction_on_one_with_mode(keys::ROOM_SETTINGS, IdbTransactionMode::Readonly)? + .object_store(keys::ROOM_SETTINGS)? + .get(&key)? + .await? + .map(|v| self.serializer.deserialize_value(v)) + .transpose() + } async fn get_custom_value(&self, key: &str) -> Result>> { self