feat(event cache): propose a debug representation for the linked chunk in RoomEvents too

This commit is contained in:
Benjamin Bouvier
2024-12-12 16:59:46 +01:00
parent f6cb8186c6
commit 34d15a4d37
2 changed files with 49 additions and 23 deletions

View File

@@ -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<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.

View File

@@ -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<String> {
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<SyncTimelineEvent, Gap>) -> 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(
|| "<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;
@@ -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(
|| "<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());