Skip to content

Commit 93c6eb3

Browse files
committed
feat(base): Create LockableEventCacheStore.
1 parent 46f22fb commit 93c6eb3

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

crates/matrix-sdk-base/src/event_cache_store/memory_store.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ impl MemoryStore {
5151
impl EventCacheStore for MemoryStore {
5252
type Error = EventCacheStoreError;
5353

54+
async fn try_take_leased_lock(
55+
&self,
56+
lease_duration_ms: u32,
57+
key: &str,
58+
holder: &str,
59+
) -> Result<bool, Self::Error> {
60+
todo!()
61+
}
62+
5463
async fn add_media_content(&self, request: &MediaRequest, data: Vec<u8>) -> Result<()> {
5564
// Avoid duplication. Let's try to remove it first.
5665
self.remove_media_content(request).await?;

crates/matrix-sdk-base/src/event_cache_store/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
//! into the event cache for the actual storage. By default this brings an
2020
//! in-memory store.
2121
22-
use std::str::Utf8Error;
22+
use std::{str::Utf8Error, sync::Arc};
2323

2424
#[cfg(any(test, feature = "testing"))]
2525
#[macro_use]
2626
pub mod integration_tests;
2727
mod memory_store;
2828
mod traits;
2929

30+
use matrix_sdk_common::store_locks::BackingStore;
3031
pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;
3132

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

8485
/// An `EventCacheStore` specific result type.
8586
pub type Result<T, E = EventCacheStoreError> = std::result::Result<T, E>;
87+
88+
/// The public API to read the event cache store: it is behind a cross-process
89+
/// lock.
90+
#[derive(Clone, Debug)]
91+
pub struct LockableEventCacheStore(Arc<dyn EventCacheStore<Error = EventCacheStoreError>>);
92+
93+
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
94+
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
95+
impl BackingStore for LockableEventCacheStore {
96+
type LockError = EventCacheStoreError;
97+
98+
async fn try_lock(
99+
&self,
100+
lease_duration_ms: u32,
101+
key: &str,
102+
holder: &str,
103+
) -> std::result::Result<bool, Self::LockError> {
104+
self.0.try_take_leased_lock(lease_duration_ms, key, holder).await
105+
}
106+
}

crates/matrix-sdk-base/src/event_cache_store/traits.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ pub trait EventCacheStore: AsyncTraitDeps {
2929
/// The error type used by this event cache store.
3030
type Error: fmt::Debug + Into<EventCacheStoreError>;
3131

32+
/// Try to take a lock using the given store.
33+
async fn try_take_leased_lock(
34+
&self,
35+
lease_duration_ms: u32,
36+
key: &str,
37+
holder: &str,
38+
) -> Result<bool, Self::Error>;
39+
3240
/// Add a media file's content in the media store.
3341
///
3442
/// # Arguments
@@ -105,6 +113,15 @@ impl<T: fmt::Debug> fmt::Debug for EraseEventCacheStoreError<T> {
105113
impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
106114
type Error = EventCacheStoreError;
107115

116+
async fn try_take_leased_lock(
117+
&self,
118+
lease_duration_ms: u32,
119+
key: &str,
120+
holder: &str,
121+
) -> Result<bool, Self::Error> {
122+
self.0.try_take_leased_lock(lease_duration_ms, key, holder).await.map_err(Into::into)
123+
}
124+
108125
async fn add_media_content(
109126
&self,
110127
request: &MediaRequest,

crates/matrix-sdk-sqlite/src/event_cache_store.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ async fn run_migrations(conn: &SqliteAsyncConn, version: u8) -> Result<()> {
140140
impl EventCacheStore for SqliteEventCacheStore {
141141
type Error = Error;
142142

143+
async fn try_take_leased_lock(
144+
&self,
145+
lease_duration_ms: u32,
146+
key: &str,
147+
holder: &str,
148+
) -> Result<bool, Self::Error> {
149+
todo!()
150+
}
151+
143152
async fn add_media_content(&self, request: &MediaRequest, content: Vec<u8>) -> Result<()> {
144153
let uri = self.encode_key(keys::MEDIA, request.source.unique_key());
145154
let format = self.encode_key(keys::MEDIA, request.format.unique_key());

0 commit comments

Comments
 (0)