Skip to content

Commit

Permalink
refactor(sdk): Rename RoomEvents::filter_duplicated_events.
Browse files Browse the repository at this point in the history
This patch renames `RoomEvents::filter_duplicated_events` to
`collect_valid_and_duplicated_events` as I believe it improves the
understanding of the code. The variables named `unique_events` are
renamed `events` as all (valid) events are returned, not only the unique
ones.
  • Loading branch information
Hywan committed Jan 8, 2025
1 parent 7ff1170 commit d649606
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions crates/matrix-sdk/src/event_cache/room/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ impl RoomEvents {
where
I: IntoIterator<Item = Event>,
{
let (unique_events, duplicated_event_ids) =
self.filter_duplicated_events(events.into_iter());
let (events, duplicated_event_ids) =
self.collect_valid_and_duplicated_events(events.into_iter());

if deduplicated_all_new_events(unique_events.len(), duplicated_event_ids.len()) {
if deduplicated_all_new_events(events.len(), duplicated_event_ids.len()) {
return false;
}

Expand All @@ -115,7 +115,7 @@ impl RoomEvents {
self.remove_events(duplicated_event_ids);

// Push new `events`.
self.chunks.push_items_back(unique_events);
self.chunks.push_items_back(events);

true
}
Expand All @@ -132,10 +132,10 @@ impl RoomEvents {
where
I: IntoIterator<Item = Event>,
{
let (unique_events, duplicated_event_ids) =
self.filter_duplicated_events(events.into_iter());
let (events, duplicated_event_ids) =
self.collect_valid_and_duplicated_events(events.into_iter());

if deduplicated_all_new_events(unique_events.len(), duplicated_event_ids.len()) {
if deduplicated_all_new_events(events.len(), duplicated_event_ids.len()) {
return Ok(false);
}

Expand All @@ -146,7 +146,7 @@ impl RoomEvents {
// argument value for each removal.
self.remove_events_and_update_insert_position(duplicated_event_ids, &mut position);

self.chunks.insert_items_at(unique_events, position)?;
self.chunks.insert_items_at(events, position)?;

Ok(true)
}
Expand All @@ -163,8 +163,8 @@ impl RoomEvents {
///
/// This method returns:
/// - a boolean indicating if we updated the linked chunk,
/// - a reference to the (first if many) newly created `Chunk` that contains
/// the `items`.
/// - the position of the (first if many) newly created `Chunk` that
/// contains the `items`.
pub fn replace_gap_at<I>(
&mut self,
events: I,
Expand All @@ -173,10 +173,10 @@ impl RoomEvents {
where
I: IntoIterator<Item = Event>,
{
let (unique_events, duplicated_event_ids) =
self.filter_duplicated_events(events.into_iter());
let (events, duplicated_event_ids) =
self.collect_valid_and_duplicated_events(events.into_iter());

if deduplicated_all_new_events(unique_events.len(), duplicated_event_ids.len()) {
if deduplicated_all_new_events(events.len(), duplicated_event_ids.len()) {
let pos = self.chunks.remove_gap_at(gap_identifier)?;
return Ok((false, pos));
}
Expand All @@ -188,13 +188,13 @@ impl RoomEvents {
// because of the removals.
self.remove_events(duplicated_event_ids);

let next_pos = if unique_events.is_empty() {
let next_pos = if events.is_empty() {
// There are no new events, so there's no need to create a new empty items
// chunk; instead, remove the gap.
self.chunks.remove_gap_at(gap_identifier)?
} else {
// Replace the gap by new events.
Some(self.chunks.replace_gap_at(unique_events, gap_identifier)?.first_position())
Some(self.chunks.replace_gap_at(events, gap_identifier)?.first_position())
};

Ok((true, next_pos))
Expand Down Expand Up @@ -257,15 +257,18 @@ impl RoomEvents {

/// Deduplicate `events` considering all events in `Self::chunks`.
///
/// The returned tuple contains (i) the unique events, and (ii) the
/// The returned tuple contains (i) all events with an ID, and (ii) the
/// duplicated events (by ID).
fn filter_duplicated_events<'a, I>(&'a mut self, events: I) -> (Vec<Event>, Vec<OwnedEventId>)
fn collect_valid_and_duplicated_events<'a, I>(
&'a mut self,
events: I,
) -> (Vec<Event>, Vec<OwnedEventId>)
where
I: Iterator<Item = Event> + 'a,
{
let mut duplicated_event_ids = Vec::new();

let deduplicated_events = self
let events = self
.deduplicator
.scan_and_learn(events, self)
.filter_map(|decorated_event| match decorated_event {
Expand All @@ -292,7 +295,7 @@ impl RoomEvents {
})
.collect();

(deduplicated_events, duplicated_event_ids)
(events, duplicated_event_ids)
}

/// Return a nice debug string (a vector of lines) for the linked chunk of
Expand Down

0 comments on commit d649606

Please sign in to comment.