Skip to content

Commit

Permalink
feat(base): Create EventCacheStoreLock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Oct 30, 2024
1 parent 4e2bea3 commit 12f3ff3
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions crates/matrix-sdk-base/src/event_cache_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
//! into the event cache for the actual storage. By default this brings an
//! in-memory store.
use std::{str::Utf8Error, sync::Arc};
use std::{fmt, 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;
use matrix_sdk_common::store_locks::{BackingStore, CrossProcessStoreLock};
pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;

#[cfg(any(test, feature = "testing"))]
Expand Down Expand Up @@ -85,10 +85,8 @@ impl EventCacheStoreError {
/// An `EventCacheStore` specific result type.
pub type Result<T, E = EventCacheStoreError> = std::result::Result<T, E>;

/// The public API to read the event cache store: it is behind a cross-process
/// lock.
#[derive(Clone, Debug)]
pub struct LockableEventCacheStore(Arc<dyn EventCacheStore<Error = EventCacheStoreError>>);
struct LockableEventCacheStore(Arc<DynEventCacheStore>);

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
Expand All @@ -104,3 +102,32 @@ impl BackingStore for LockableEventCacheStore {
self.0.try_take_leased_lock(lease_duration_ms, key, holder).await
}
}

/// The high-level public type to represent an `EventCacheStore` lock.
pub struct EventCacheStoreLock {
cross_process_lock: CrossProcessStoreLock<LockableEventCacheStore>,
store: Arc<DynEventCacheStore>,
}

impl fmt::Debug for EventCacheStoreLock {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("EventCacheStoreLock").finish_non_exhaustive()
}
}

impl EventCacheStoreLock {
pub fn new(store: Arc<DynEventCacheStore>, key: String, holder: String) -> Self {
Self {
cross_process_lock: CrossProcessStoreLock::new(
LockableEventCacheStore(store.clone()),
key,
holder,
),
store,
}
}

pub async fn lock(&self) -> &Arc<DynEventCacheStore> {
todo!()
}
}

0 comments on commit 12f3ff3

Please sign in to comment.