Skip to content

Commit

Permalink
task(sdk): RoomEventCacheState::find_event returns the event location.
Browse files Browse the repository at this point in the history
This patch introduces `EventLocation` to know if an event has been found
in the memory (in `RoomEvents`) or in the store (in `EventCacheStore`).

This is used by the `RoomEventCacheState::find_event`.
  • Loading branch information
Hywan committed Mar 3, 2025
1 parent 51ef2dd commit 293c1a3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions crates/matrix-sdk/src/event_cache/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl RoomEventCache {
};

// Search in `AllEventsCache` for known events that are not stored.
if let Some(event) = maybe_position_and_event.map(|(_position, event)| event) {
if let Some(event) = maybe_position_and_event.map(|(_location, _position, event)| event) {
Some(event)
} else if let Some((room_id, event)) =
self.inner.all_events.read().await.events.get(event_id).cloned()
Expand Down Expand Up @@ -676,7 +676,8 @@ mod private {
EventCacheError,
},
events::RoomEvents,
sort_positions_descending, EventsPostProcessing, LoadMoreEventsBackwardsOutcome,
sort_positions_descending, EventLocation, EventsPostProcessing,
LoadMoreEventsBackwardsOutcome,
};
use crate::event_cache::RoomPaginationStatus;

Expand Down Expand Up @@ -1196,14 +1197,14 @@ mod private {
pub async fn find_event(
&self,
event_id: &EventId,
) -> Result<Option<(Position, TimelineEvent)>, EventCacheError> {
) -> Result<Option<(EventLocation, Position, TimelineEvent)>, EventCacheError> {
let room_id = self.room.as_ref();

// There are supposedly fewer events loaded in memory than in the store. Let's
// start by looking up in the `RoomEvents`.
for (position, event) in self.events().revents() {
if event.event_id().as_deref() == Some(event_id) {
return Ok(Some((position, event.clone())));
return Ok(Some((EventLocation::InMemory, position, event.clone())));
}
}

Expand All @@ -1214,7 +1215,10 @@ mod private {

let store = store.lock().await?;

Ok(store.find_event(room_id, event_id).await?)
Ok(store
.find_event(room_id, event_id)
.await?
.map(|(position, event)| (EventLocation::InStore, position, event)))
}

/// Gives a temporary mutable handle to the underlying in-memory events,
Expand Down Expand Up @@ -1347,6 +1351,11 @@ pub(super) enum EventsPostProcessing {
None,
}

pub(super) enum EventLocation {
InMemory,
InStore,
}

pub(super) use private::RoomEventCacheState;

#[cfg(test)]
Expand Down

0 comments on commit 293c1a3

Please sign in to comment.