Skip to content

Commit

Permalink
feat(event cache): propose a debug representation for the linked chun…
Browse files Browse the repository at this point in the history
…k in `RoomEvents` too
  • Loading branch information
bnjbvr committed Dec 12, 2024
1 parent fc70598 commit 734bcc6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
18 changes: 16 additions & 2 deletions crates/matrix-sdk/src/event_cache/room/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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<String> {
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.
Expand Down
54 changes: 33 additions & 21 deletions crates/matrix-sdk/src/event_cache/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<String> {
self.inner.state.read().await.events().debug_string()
}
}

/// The (non-cloneable) details of the `RoomEventCache`.
Expand Down Expand Up @@ -572,6 +579,29 @@ impl RoomEventCacheInner {
}
}

/// Create a debug string for a [`ChunkContent`] for an event/gap pair.
fn chunk_debug_string(content: &ChunkContent<SyncTimelineEvent, Gap>) -> String {
match 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(
|| "<no event id>".to_owned(),
|id| id.as_str().chars().take(1 + 8).collect(),
)
})
.collect::<Vec<_>>()
.join(", ");
format!("events[{items}]")
}
}
}

// Use a private module to hide `events` to this parent module.
mod private {
use std::sync::Arc;
Expand All @@ -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.
Expand Down Expand Up @@ -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(
|| "<no event id>".to_owned(),
|id| id.as_str().chars().take(1 + 8).collect(),
)
})
.collect::<Vec<_>>()
.join(", ");
format!("events[{items}]")
}
};
let content = chunk_debug_string(&c.content);

let prev =
c.previous.map_or_else(|| "<none>".to_owned(), |prev| prev.index().to_string());
Expand Down

0 comments on commit 734bcc6

Please sign in to comment.