crypto: More transactions in the sqlite store.

master
Damir Jelić 2020-10-16 15:23:34 +02:00
parent fc54c63a4c
commit b5560d3cb6
1 changed files with 23 additions and 9 deletions

View File

@ -754,11 +754,13 @@ impl SqliteStore {
Ok(()) 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 account_id = self.account_id().ok_or(CryptoStoreError::AccountUnset)?;
let mut connection = self.connection.lock().await;
query( query(
"INSERT INTO devices ( "INSERT INTO devices (
account_id, user_id, device_id, account_id, user_id, device_id,
@ -1107,11 +1109,13 @@ impl SqliteStore {
Ok(()) 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 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)") query("REPLACE INTO users (account_id, user_id) VALUES (?1, ?2)")
.bind(account_id) .bind(account_id)
.bind(user.user_id().as_str()) .bind(user.user_id().as_str())
@ -1347,12 +1351,17 @@ impl CryptoStore for SqliteStore {
} }
async fn save_devices(&self, devices: &[ReadOnlyDevice]) -> Result<()> { 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 { for device in devices {
self.devices.add(device.clone()); 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(()) Ok(())
} }
@ -1391,10 +1400,15 @@ impl CryptoStore for SqliteStore {
} }
async fn save_user_identities(&self, users: &[UserIdentities]) -> Result<()> { 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 { for user in users {
self.save_user_helper(user).await?; self.save_user_helper(&mut transaction, user).await?;
} }
transaction.commit().await?;
Ok(()) Ok(())
} }