Skip to content

Commit 74972d8

Browse files
mgoldenbergHywan
authored andcommitted
feat(indexeddb): add IndexedDB-backed impl for EventCacheStore::load_all_chunks_metadata
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
1 parent 03a76fb commit 74972d8

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,13 @@ macro_rules! event_cache_store_integration_tests {
728728
event_cache_store.test_rebuild_empty_linked_chunk().await;
729729
}
730730

731+
#[async_test]
732+
async fn test_load_all_chunks_metadata() {
733+
let event_cache_store =
734+
get_event_cache_store().await.unwrap().into_event_cache_store();
735+
event_cache_store.test_load_all_chunks_metadata().await;
736+
}
737+
731738
#[async_test]
732739
async fn test_clear_all_linked_chunks() {
733740
let event_cache_store =

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,27 @@ impl_event_cache_store! {
287287
&self,
288288
linked_chunk_id: LinkedChunkId<'_>,
289289
) -> Result<Vec<ChunkMetadata>, IndexeddbEventCacheStoreError> {
290-
self.memory_store
291-
.load_all_chunks_metadata(linked_chunk_id)
292-
.await
293-
.map_err(IndexeddbEventCacheStoreError::MemoryStore)
290+
let linked_chunk_id = linked_chunk_id.to_owned();
291+
let room_id = linked_chunk_id.room_id();
292+
293+
let transaction = self.transaction(
294+
&[keys::LINKED_CHUNKS, keys::EVENTS, keys::GAPS],
295+
IdbTransactionMode::Readwrite,
296+
)?;
297+
298+
let mut raw_chunks = Vec::new();
299+
let chunks = transaction.get_chunks_in_room(room_id).await?;
300+
for chunk in chunks {
301+
let chunk_id = ChunkIdentifier::new(chunk.identifier);
302+
let num_items = transaction.get_events_count_by_chunk(room_id, &chunk_id).await?;
303+
raw_chunks.push(ChunkMetadata {
304+
num_items,
305+
previous: chunk.previous.map(ChunkIdentifier::new),
306+
identifier: ChunkIdentifier::new(chunk.identifier),
307+
next: chunk.next.map(ChunkIdentifier::new),
308+
});
309+
}
310+
Ok(raw_chunks)
294311
}
295312

296313
async fn load_last_chunk(

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,17 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
580580
self.get_items_by_key_components::<Event, IndexedEventPositionKey>(room_id, range).await
581581
}
582582

583+
/// Query IndexedDB for number of events in the given position range in the
584+
/// given room.
585+
pub async fn get_events_count_by_position(
586+
&self,
587+
room_id: &RoomId,
588+
range: impl Into<IndexedKeyRange<&Position>>,
589+
) -> Result<usize, IndexeddbEventCacheStoreTransactionError> {
590+
self.get_items_count_by_key_components::<Event, IndexedEventPositionKey>(room_id, range)
591+
.await
592+
}
593+
583594
/// Query IndexedDB for events in the given chunk in the given room.
584595
pub async fn get_events_by_chunk(
585596
&self,
@@ -594,6 +605,21 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
594605
self.get_events_by_position(room_id, range).await
595606
}
596607

608+
/// Query IndexedDB for number of events in the given chunk in the given
609+
/// room.
610+
pub async fn get_events_count_by_chunk(
611+
&self,
612+
room_id: &RoomId,
613+
chunk_id: &ChunkIdentifier,
614+
) -> Result<usize, IndexeddbEventCacheStoreTransactionError> {
615+
let mut lower = IndexedEventPositionKey::lower_key_components();
616+
lower.chunk_identifier = chunk_id.index();
617+
let mut upper = IndexedEventPositionKey::upper_key_components();
618+
upper.chunk_identifier = chunk_id.index();
619+
let range = IndexedKeyRange::Bound(&lower, &upper);
620+
self.get_events_count_by_position(room_id, range).await
621+
}
622+
597623
/// Puts an event in the given room. If an event with the same key already
598624
/// exists, it will be overwritten.
599625
pub async fn put_event(

0 commit comments

Comments
 (0)