Skip to content

Commit b4ef6ce

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add IndexedDB-backed impl of EventCacheStore::try_take_leased_lock
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent c6854a5 commit b4ef6ce

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#![allow(unused)]
1616

17+
use std::time::Duration;
18+
1719
use indexed_db_futures::IdbDatabase;
1820
use matrix_sdk_base::{
1921
event_cache::{
@@ -30,15 +32,18 @@ use matrix_sdk_base::{
3032
media::MediaRequestParameters,
3133
timer,
3234
};
33-
use ruma::{events::relation::RelationType, EventId, MxcUri, OwnedEventId, RoomId};
35+
use ruma::{
36+
events::relation::RelationType, EventId, MilliSecondsSinceUnixEpoch, MxcUri, OwnedEventId,
37+
RoomId,
38+
};
3439
use tracing::{error, instrument, trace};
3540
use web_sys::IdbTransactionMode;
3641

3742
use crate::event_cache_store::{
3843
migrations::current::keys,
39-
serializer::IndexeddbEventCacheStoreSerializer,
44+
serializer::{traits::Indexed, IndexeddbEventCacheStoreSerializer},
4045
transaction::{IndexeddbEventCacheStoreTransaction, IndexeddbEventCacheStoreTransactionError},
41-
types::{ChunkType, InBandEvent, OutOfBandEvent},
46+
types::{ChunkType, InBandEvent, Lease, OutOfBandEvent},
4247
};
4348

4449
mod builder;
@@ -132,10 +137,27 @@ impl_event_cache_store! {
132137
holder: &str,
133138
) -> Result<bool, IndexeddbEventCacheStoreError> {
134139
let _timer = timer!("method");
135-
self.memory_store
136-
.try_take_leased_lock(lease_duration_ms, key, holder)
137-
.await
138-
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
140+
141+
let now = Duration::from_millis(MilliSecondsSinceUnixEpoch::now().get().into());
142+
143+
let transaction =
144+
self.transaction(&[Lease::OBJECT_STORE], IdbTransactionMode::Readwrite)?;
145+
146+
if let Some(lease) = transaction.get_lease_by_id(key).await? {
147+
if lease.holder != holder && !lease.has_expired(now) {
148+
return Ok(false);
149+
}
150+
}
151+
152+
transaction
153+
.put_lease(&Lease {
154+
key: key.to_owned(),
155+
holder: holder.to_owned(),
156+
expiration: now + Duration::from_millis(lease_duration_ms.into()),
157+
})
158+
.await?;
159+
160+
Ok(true)
139161
}
140162

141163
#[instrument(skip(self, updates))]

crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ use crate::event_cache_store::{
3434
},
3535
types::{
3636
IndexedChunkIdKey, IndexedEventIdKey, IndexedEventPositionKey, IndexedEventRelationKey,
37-
IndexedGapIdKey, IndexedKeyRange, IndexedNextChunkIdKey,
37+
IndexedGapIdKey, IndexedKeyRange, IndexedLeaseIdKey, IndexedNextChunkIdKey,
3838
},
3939
IndexeddbEventCacheStoreSerializer,
4040
},
41-
types::{Chunk, ChunkType, Event, Gap, Position},
41+
types::{Chunk, ChunkType, Event, Gap, Lease, Position},
4242
};
4343

4444
#[derive(Debug, Error)]
@@ -410,6 +410,24 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
410410
self.transaction.object_store(T::OBJECT_STORE)?.clear()?.await.map_err(Into::into)
411411
}
412412

413+
/// Query IndexedDB for the lease that matches the given key `id`. If more
414+
/// than one lease is found, an error is returned.
415+
pub async fn get_lease_by_id(
416+
&self,
417+
id: &str,
418+
) -> Result<Option<Lease>, IndexeddbEventCacheStoreTransactionError> {
419+
self.get_item_by_key_components::<Lease, IndexedLeaseIdKey>(id).await
420+
}
421+
422+
/// Puts a lease into IndexedDB. If an event with the same key already
423+
/// exists, it will be overwritten.
424+
pub async fn put_lease(
425+
&self,
426+
lease: &Lease,
427+
) -> Result<(), IndexeddbEventCacheStoreTransactionError> {
428+
self.put_item(lease).await
429+
}
430+
413431
/// Query IndexedDB for chunks that match the given chunk identifier in the
414432
/// given room. If more than one item is found, an error is returned.
415433
pub async fn get_chunk_by_id(

crates/matrix-sdk-indexeddb/src/event_cache_store/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct Lease {
3232

3333
impl Lease {
3434
/// Determines whether the lease is expired at a given time `t`
35-
pub fn expired_at(&self, t: Duration) -> bool {
35+
pub fn has_expired(&self, t: Duration) -> bool {
3636
self.expiration < t
3737
}
3838
}

0 commit comments

Comments
 (0)