From b5560d3cb678ae2f5ea566d347aff4b1f4b16965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 16 Oct 2020 15:23:34 +0200 Subject: [PATCH] crypto: More transactions in the sqlite store. --- matrix_sdk_crypto/src/store/sqlite.rs | 32 +++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index e469948c..e3aaa303 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -754,11 +754,13 @@ impl SqliteStore { Ok(()) } - async fn save_device_helper(&self, device: ReadOnlyDevice) -> Result<()> { + async fn save_device_helper( + &self, + connection: &mut SqliteConnection, + device: ReadOnlyDevice, + ) -> Result<()> { let account_id = self.account_id().ok_or(CryptoStoreError::AccountUnset)?; - let mut connection = self.connection.lock().await; - query( "INSERT INTO devices ( account_id, user_id, device_id, @@ -1107,11 +1109,13 @@ impl SqliteStore { Ok(()) } - async fn save_user_helper(&self, user: &UserIdentities) -> Result<()> { + async fn save_user_helper( + &self, + mut connection: &mut SqliteConnection, + user: &UserIdentities, + ) -> Result<()> { let account_id = self.account_id().ok_or(CryptoStoreError::AccountUnset)?; - let mut connection = self.connection.lock().await; - query("REPLACE INTO users (account_id, user_id) VALUES (?1, ?2)") .bind(account_id) .bind(user.user_id().as_str()) @@ -1347,12 +1351,17 @@ impl CryptoStore for SqliteStore { } async fn save_devices(&self, devices: &[ReadOnlyDevice]) -> Result<()> { - // TODO turn this into a bulk transaction. + let mut connection = self.connection.lock().await; + let mut transaction = connection.begin().await?; + for device in devices { self.devices.add(device.clone()); - self.save_device_helper(device.clone()).await? + self.save_device_helper(&mut transaction, device.clone()) + .await? } + transaction.commit().await?; + Ok(()) } @@ -1391,10 +1400,15 @@ impl CryptoStore for SqliteStore { } async fn save_user_identities(&self, users: &[UserIdentities]) -> Result<()> { + let mut connection = self.connection.lock().await; + let mut transaction = connection.begin().await?; + for user in users { - self.save_user_helper(user).await?; + self.save_user_helper(&mut transaction, user).await?; } + transaction.commit().await?; + Ok(()) }