Skip to content

Commit

Permalink
feat(base): Create LockableEventCacheStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Nov 5, 2024
1 parent 46f22fb commit a8f244a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/matrix-sdk-base/src/event_cache_store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ impl MemoryStore {
impl EventCacheStore for MemoryStore {
type Error = EventCacheStoreError;

async fn try_take_leased_lock(
&self,
lease_duration_ms: u32,
key: &str,
holder: &str,
) -> Result<bool, Self::Error> {
todo!()
}

async fn add_media_content(&self, request: &MediaRequest, data: Vec<u8>) -> Result<()> {
// Avoid duplication. Let's try to remove it first.
self.remove_media_content(request).await?;
Expand Down
23 changes: 22 additions & 1 deletion crates/matrix-sdk-base/src/event_cache_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
//! into the event cache for the actual storage. By default this brings an
//! in-memory store.
use std::str::Utf8Error;
use std::{str::Utf8Error, sync::Arc};

#[cfg(any(test, feature = "testing"))]
#[macro_use]
pub mod integration_tests;
mod memory_store;
mod traits;

use matrix_sdk_common::store_locks::BackingStore;
pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;

#[cfg(any(test, feature = "testing"))]
Expand Down Expand Up @@ -83,3 +84,23 @@ impl EventCacheStoreError {

/// An `EventCacheStore` specific result type.
pub type Result<T, E = EventCacheStoreError> = std::result::Result<T, E>;

/// A type that wraps the [`EventCacheStore`] but implements [`BackingStore`] to
/// make it usable inside the cross process lock.
#[derive(Clone, Debug)]
struct LockableEventCacheStore(Arc<DynEventCacheStore>);

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl BackingStore for LockableEventCacheStore {
type LockError = EventCacheStoreError;

async fn try_lock(
&self,
lease_duration_ms: u32,
key: &str,
holder: &str,
) -> std::result::Result<bool, Self::LockError> {
self.0.try_take_leased_lock(lease_duration_ms, key, holder).await
}
}
17 changes: 17 additions & 0 deletions crates/matrix-sdk-base/src/event_cache_store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ pub trait EventCacheStore: AsyncTraitDeps {
/// The error type used by this event cache store.
type Error: fmt::Debug + Into<EventCacheStoreError>;

/// Try to take a lock using the given store.
async fn try_take_leased_lock(
&self,
lease_duration_ms: u32,
key: &str,
holder: &str,
) -> Result<bool, Self::Error>;

/// Add a media file's content in the media store.
///
/// # Arguments
Expand Down Expand Up @@ -105,6 +113,15 @@ impl<T: fmt::Debug> fmt::Debug for EraseEventCacheStoreError<T> {
impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
type Error = EventCacheStoreError;

async fn try_take_leased_lock(
&self,
lease_duration_ms: u32,
key: &str,
holder: &str,
) -> Result<bool, Self::Error> {
self.0.try_take_leased_lock(lease_duration_ms, key, holder).await.map_err(Into::into)
}

async fn add_media_content(
&self,
request: &MediaRequest,
Expand Down
9 changes: 9 additions & 0 deletions crates/matrix-sdk-sqlite/src/event_cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ async fn run_migrations(conn: &SqliteAsyncConn, version: u8) -> Result<()> {
impl EventCacheStore for SqliteEventCacheStore {
type Error = Error;

async fn try_take_leased_lock(
&self,
lease_duration_ms: u32,
key: &str,
holder: &str,
) -> Result<bool, Self::Error> {
todo!()
}

async fn add_media_content(&self, request: &MediaRequest, content: Vec<u8>) -> Result<()> {
let uri = self.encode_key(keys::MEDIA, request.source.unique_key());
let format = self.encode_key(keys::MEDIA, request.format.unique_key());
Expand Down

0 comments on commit a8f244a

Please sign in to comment.