From 34d15a4d3792cb665f3d9d2b1809eea5ee32d14f Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 12 Dec 2024 16:59:46 +0100 Subject: [PATCH] feat(event cache): propose a debug representation for the linked chunk in `RoomEvents` too --- .../matrix-sdk/src/event_cache/room/events.rs | 18 ++++++- crates/matrix-sdk/src/event_cache/room/mod.rs | 54 +++++++++++-------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/room/events.rs b/crates/matrix-sdk/src/event_cache/room/events.rs index 24c1d9ba3..83fc22189 100644 --- a/crates/matrix-sdk/src/event_cache/room/events.rs +++ b/crates/matrix-sdk/src/event_cache/room/events.rs @@ -26,7 +26,10 @@ use matrix_sdk_common::linked_chunk::{ use ruma::OwnedEventId; use tracing::{debug, error, warn}; -use super::super::deduplicator::{Decoration, Deduplicator}; +use super::{ + super::deduplicator::{Decoration, Deduplicator}, + chunk_debug_string, +}; /// This type represents all events of a single room. #[derive(Debug)] @@ -177,7 +180,6 @@ impl RoomEvents { /// Iterate over the chunks, forward. /// /// The oldest chunk comes first. - #[cfg(test)] pub fn chunks( &self, ) -> matrix_sdk_common::linked_chunk::Iter<'_, DEFAULT_CHUNK_CAPACITY, Event, Gap> { @@ -262,6 +264,18 @@ impl RoomEvents { (deduplicated_events, duplicated_event_ids) } + + /// Return a nice debug string (a vector of lines) for the linked chunk of + /// events for this room. + pub fn debug_string(&self) -> Vec { + let mut result = Vec::new(); + for c in self.chunks() { + let content = chunk_debug_string(c.content()); + let line = format!("chunk #{}: {content}", c.identifier().index()); + result.push(line); + } + result + } } // Private implementations, implementation specific. diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index 51072771d..ddbb5ceab 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -19,6 +19,7 @@ use std::{collections::BTreeMap, fmt, sync::Arc}; use events::Gap; use matrix_sdk_base::{ deserialized_responses::{AmbiguityChange, SyncTimelineEvent}, + linked_chunk::ChunkContent, sync::{JoinedRoomUpdate, LeftRoomUpdate, Timeline}, }; use ruma::{ @@ -215,6 +216,12 @@ impl RoomEventCache { } } } + + /// Return a nice debug string (a vector of lines) for the linked chunk of + /// events for this room. + pub async fn debug_string(&self) -> Vec { + self.inner.state.read().await.events().debug_string() + } } /// The (non-cloneable) details of the `RoomEventCache`. @@ -572,6 +579,29 @@ impl RoomEventCacheInner { } } +/// Create a debug string for a [`ChunkContent`] for an event/gap pair. +fn chunk_debug_string(content: &ChunkContent) -> String { + match content { + ChunkContent::Gap(Gap { prev_token }) => { + format!("gap['{prev_token}']") + } + ChunkContent::Items(vec) => { + let items = vec + .iter() + .map(|event| { + // Limit event ids to 8 chars *after* the $. + event.event_id().map_or_else( + || "".to_owned(), + |id| id.as_str().chars().take(1 + 8).collect(), + ) + }) + .collect::>() + .join(", "); + format!("events[{items}]") + } + } +} + // Use a private module to hide `events` to this parent module. mod private { use std::sync::Arc; @@ -585,13 +615,13 @@ mod private { }, Event, Gap, }, - linked_chunk::{ChunkContent, LinkedChunk, LinkedChunkBuilder, RawChunk, Update}, + linked_chunk::{LinkedChunk, LinkedChunkBuilder, RawChunk, Update}, }; use once_cell::sync::OnceCell; use ruma::{serde::Raw, OwnedRoomId, RoomId}; use tracing::{error, trace}; - use super::events::RoomEvents; + use super::{chunk_debug_string, events::RoomEvents}; use crate::event_cache::EventCacheError; /// State for a single room's event cache. @@ -808,25 +838,7 @@ mod private { raw_chunks.sort_by_key(|c| c.identifier.index()); for c in raw_chunks { - let content = match c.content { - ChunkContent::Gap(Gap { prev_token }) => { - format!("gap['{prev_token}']") - } - ChunkContent::Items(vec) => { - let items = vec - .into_iter() - .map(|event| { - // Limit event ids to 8 chars *after* the $. - event.event_id().map_or_else( - || "".to_owned(), - |id| id.as_str().chars().take(1 + 8).collect(), - ) - }) - .collect::>() - .join(", "); - format!("events[{items}]") - } - }; + let content = chunk_debug_string(&c.content); let prev = c.previous.map_or_else(|| "".to_owned(), |prev| prev.index().to_string());