From 12f3ff3486f1b953f1ec340b05f1fa47545e45df Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 30 Oct 2024 16:53:45 +0100 Subject: [PATCH] feat(base): Create `EventCacheStoreLock`. --- .../src/event_cache_store/mod.rs | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/crates/matrix-sdk-base/src/event_cache_store/mod.rs b/crates/matrix-sdk-base/src/event_cache_store/mod.rs index bfcf185441..d2a851bcfe 100644 --- a/crates/matrix-sdk-base/src/event_cache_store/mod.rs +++ b/crates/matrix-sdk-base/src/event_cache_store/mod.rs @@ -19,7 +19,7 @@ //! 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] @@ -27,7 +27,7 @@ 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"))] @@ -85,10 +85,8 @@ impl EventCacheStoreError { /// An `EventCacheStore` specific result type. pub type Result = std::result::Result; -/// The public API to read the event cache store: it is behind a cross-process -/// lock. #[derive(Clone, Debug)] -pub struct LockableEventCacheStore(Arc>); +struct LockableEventCacheStore(Arc); #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] @@ -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, + store: Arc, +} + +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, key: String, holder: String) -> Self { + Self { + cross_process_lock: CrossProcessStoreLock::new( + LockableEventCacheStore(store.clone()), + key, + holder, + ), + store, + } + } + + pub async fn lock(&self) -> &Arc { + todo!() + } +}