From 74972d8db7bb0f7275ac1371b9fe48becb22ca37 Mon Sep 17 00:00:00 2001 From: Michael Goldenberg Date: Mon, 14 Jul 2025 23:02:29 -0400 Subject: [PATCH] feat(indexeddb): add IndexedDB-backed impl for EventCacheStore::load_all_chunks_metadata Signed-off-by: Michael Goldenberg --- .../event_cache_store/integration_tests.rs | 7 +++++ .../src/event_cache_store/mod.rs | 25 +++++++++++++++--- .../src/event_cache_store/transaction.rs | 26 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/integration_tests.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/integration_tests.rs index 3d6e511b6..fd278cb73 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/integration_tests.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/integration_tests.rs @@ -728,6 +728,13 @@ macro_rules! event_cache_store_integration_tests { event_cache_store.test_rebuild_empty_linked_chunk().await; } + #[async_test] + async fn test_load_all_chunks_metadata() { + let event_cache_store = + get_event_cache_store().await.unwrap().into_event_cache_store(); + event_cache_store.test_load_all_chunks_metadata().await; + } + #[async_test] async fn test_clear_all_linked_chunks() { let event_cache_store = diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs index 5139d55c0..ae0b83418 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs @@ -287,10 +287,27 @@ impl_event_cache_store! { &self, linked_chunk_id: LinkedChunkId<'_>, ) -> Result, IndexeddbEventCacheStoreError> { - self.memory_store - .load_all_chunks_metadata(linked_chunk_id) - .await - .map_err(IndexeddbEventCacheStoreError::MemoryStore) + let linked_chunk_id = linked_chunk_id.to_owned(); + let room_id = linked_chunk_id.room_id(); + + let transaction = self.transaction( + &[keys::LINKED_CHUNKS, keys::EVENTS, keys::GAPS], + IdbTransactionMode::Readwrite, + )?; + + let mut raw_chunks = Vec::new(); + let chunks = transaction.get_chunks_in_room(room_id).await?; + for chunk in chunks { + let chunk_id = ChunkIdentifier::new(chunk.identifier); + let num_items = transaction.get_events_count_by_chunk(room_id, &chunk_id).await?; + raw_chunks.push(ChunkMetadata { + num_items, + previous: chunk.previous.map(ChunkIdentifier::new), + identifier: ChunkIdentifier::new(chunk.identifier), + next: chunk.next.map(ChunkIdentifier::new), + }); + } + Ok(raw_chunks) } async fn load_last_chunk( diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs index 45615d0ae..15435d4a5 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs @@ -580,6 +580,17 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> { self.get_items_by_key_components::(room_id, range).await } + /// Query IndexedDB for number of events in the given position range in the + /// given room. + pub async fn get_events_count_by_position( + &self, + room_id: &RoomId, + range: impl Into>, + ) -> Result { + self.get_items_count_by_key_components::(room_id, range) + .await + } + /// Query IndexedDB for events in the given chunk in the given room. pub async fn get_events_by_chunk( &self, @@ -594,6 +605,21 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> { self.get_events_by_position(room_id, range).await } + /// Query IndexedDB for number of events in the given chunk in the given + /// room. + pub async fn get_events_count_by_chunk( + &self, + room_id: &RoomId, + chunk_id: &ChunkIdentifier, + ) -> Result { + let mut lower = IndexedEventPositionKey::lower_key_components(); + lower.chunk_identifier = chunk_id.index(); + let mut upper = IndexedEventPositionKey::upper_key_components(); + upper.chunk_identifier = chunk_id.index(); + let range = IndexedKeyRange::Bound(&lower, &upper); + self.get_events_count_by_position(room_id, range).await + } + /// Puts an event in the given room. If an event with the same key already /// exists, it will be overwritten. pub async fn put_event(