Skip to content

Commit

Permalink
feat(sdk): Find and remove duplicated events in RoomEvents.
Browse files Browse the repository at this point in the history
This patch uses the new `Deduplicator` type, along with
`LinkeChunk::remove_item_at` to remove duplicated events. When a new
event is received, the older one is removed.
  • Loading branch information
Hywan committed Oct 30, 2024
1 parent 7d64ea1 commit fe79826
Show file tree
Hide file tree
Showing 3 changed files with 544 additions and 165 deletions.
10 changes: 8 additions & 2 deletions crates/matrix-sdk/src/event_cache/deduplicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
//! Simple but efficient types to find duplicated events. See [`Deduplicator`]
//! to learn more.

use std::{collections::BTreeSet, sync::Mutex};
use std::{collections::BTreeSet, fmt, sync::Mutex};

use growable_bloom_filter::{GrowableBloom, GrowableBloomBuilder};

use super::store::{Event, RoomEvents};
use super::room::events::{Event, RoomEvents};

/// `Deduplicator` is an efficient type to find duplicated events.
///
Expand All @@ -34,6 +34,12 @@ pub struct Deduplicator {
bloom_filter: Mutex<GrowableBloom>,
}

impl fmt::Debug for Deduplicator {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("Deduplicator").finish_non_exhaustive()
}
}

impl Deduplicator {
const APPROXIMATED_MAXIMUM_NUMBER_OF_EVENTS: usize = 800_000;
const DESIRED_FALSE_POSITIVE_RATE: f64 = 0.001;
Expand Down
9 changes: 9 additions & 0 deletions crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,15 @@ impl Position {
pub fn index(&self) -> usize {
self.1
}

/// Decrement the index part (see [`Self::index`]), i.e. subtract 1.
///
/// # Panic
///
/// This method will panic if it will underflow, i.e. if the index is 0.
pub(super) fn decrement_index(&mut self) {
self.1 = self.1.checked_sub(1).expect("Cannot decrement the index because it's already 0");
}
}

/// An iterator over a [`LinkedChunk`] that traverses the chunk in backward
Expand Down
Loading

0 comments on commit fe79826

Please sign in to comment.