feat(sled): Add a method to save tracked users in a batched way

This commit is contained in:
Damir Jelić
2022-03-31 13:13:24 +02:00
parent a6b7c98002
commit c5ddca3bc4
+30 -9
View File
@@ -42,7 +42,7 @@ use serde::{Deserialize, Serialize};
pub use sled::Error;
use sled::{
transaction::{ConflictableTransactionError, TransactionError},
Config, Db, IVec, Transactional, Tree,
Batch, Config, Db, IVec, Transactional, Tree,
};
use tracing::debug;
@@ -644,6 +644,34 @@ impl SledStore {
Ok(request)
}
/// Save a batch of tracked users.
///
/// # Arguments
///
/// * `tracked_users` - A list of tuples, the first element of the tuple is
/// the user ID, the second element is if the user should be considered to
/// be dirty.
pub async fn save_tracked_users(
&self,
tracked_users: &[(&UserId, bool)],
) -> Result<(), CryptoStoreError> {
let users: Vec<TrackedUser> = tracked_users
.iter()
.map(|(u, d)| TrackedUser { user_id: (*u).into(), dirty: *d })
.collect();
let mut batch = Batch::default();
for user in users {
batch.insert(
self.encode_key(TRACKED_USERS_TABLE, user.user_id.as_str()),
self.serialize_value(&user)?,
);
}
self.tracked_users.apply_batch(batch).map_err(|e| CryptoStoreError::Backend(anyhow!(e)))
}
}
#[async_trait]
@@ -846,14 +874,7 @@ impl CryptoStore for SledStore {
self.users_for_key_query_cache.remove(user);
}
let user = TrackedUser { user_id: user.to_owned(), dirty };
self.tracked_users
.insert(
self.encode_key(TRACKED_USERS_TABLE, user.user_id.as_str()),
self.serialize_value(&user)?,
)
.map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?;
self.save_tracked_users(&[(user, dirty)]).await?;
Ok(already_added)
}