From 9102a9c84124d0b6bd2fbd4910df38cb33ff6dc3 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 18 Mar 2024 14:30:58 +0100 Subject: [PATCH] feat(sdk): `RoomEventCachecacherInner::oldest_backpagination_token` uses `RoomEvents`. --- crates/matrix-sdk/src/event_cache/mod.rs | 22 ++++++++++++---------- crates/matrix-sdk/src/event_cache/store.rs | 7 +++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index d1162c1e6..23e44baaf 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -62,7 +62,7 @@ use ruma::{ use tokio::{ sync::{ broadcast::{error::RecvError, Receiver, Sender}, - Mutex, Notify, RwLock, + Mutex, Notify, RwLock, RwLockReadGuard, }, time::timeout, }; @@ -307,7 +307,7 @@ impl EventCacheInner { async fn handle_room_updates(&self, updates: RoomUpdates) -> Result<()> { // First, take the lock that indicates we're processing updates, to avoid // handling multiple updates concurrently. - let lock = self.multiple_room_updates_lock.lock().await; + let _lock = self.multiple_room_updates_lock.lock().await; // Left rooms. for (room_id, left_room_update) in updates.leave { @@ -704,16 +704,19 @@ impl RoomEventCacheInner { max_wait: Option, ) -> Result> { // Optimistically try to return the backpagination token immediately. - todo!(); - /* - if let Some(token) = - self.store.lock().await.oldest_backpagination_token(self.room.room_id()).await? - { + fn get_oldest(room_events: RwLockReadGuard) -> Option { + room_events.chunks().find_map(|chunk| match chunk.content() { + ChunkContent::Gap(gap) => Some(gap.prev_token.clone()), + ChunkContent::Items(..) => None, + }) + } + + if let Some(token) = get_oldest(self.events.read().await) { return Ok(Some(token)); } let Some(max_wait) = max_wait else { - // We had no token and no time to wait, so... no tokens. + // We had no token and no time to wait, so… no tokens. return Ok(None); }; @@ -721,8 +724,7 @@ impl RoomEventCacheInner { // Timeouts are fine, per this function's contract. let _ = timeout(max_wait, self.pagination_token_notifier.notified()).await; - self.store.lock().await.oldest_backpagination_token(self.room.room_id()).await - */ + Ok(get_oldest(self.events.read().await)) } } diff --git a/crates/matrix-sdk/src/event_cache/store.rs b/crates/matrix-sdk/src/event_cache/store.rs index 998b2b655..c0fe3b08e 100644 --- a/crates/matrix-sdk/src/event_cache/store.rs +++ b/crates/matrix-sdk/src/event_cache/store.rs @@ -324,6 +324,13 @@ impl RoomEvents { self.chunks.rchunks() } + /// Iterate over the chunks, forward. + /// + /// The oldest chunk comes first. + pub fn chunks(&self) -> LinkedChunkIter<'_, SyncTimelineEvent, Gap, DEFAULT_CHUNK_CAPACITY> { + self.chunks.chunks() + } + /// Iterate over the chunks, starting from `identifier`, backward. pub fn rchunks_from( &self,