Skip to content

Commit 1849a8b

Browse files
committed
feat(sdk): Find and remove duplicated events in RoomEvents.
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.
1 parent 6b23e91 commit 1849a8b

File tree

3 files changed

+544
-165
lines changed

3 files changed

+544
-165
lines changed

Diff for: crates/matrix-sdk/src/event_cache/deduplicator.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//! Simple but efficient types to find duplicated events. See [`Deduplicator`]
1616
//! to learn more.
1717
18-
use std::{collections::BTreeSet, sync::Mutex};
18+
use std::{collections::BTreeSet, fmt, sync::Mutex};
1919

2020
use growable_bloom_filter::{GrowableBloom, GrowableBloomBuilder};
2121

22-
use super::store::{Event, RoomEvents};
22+
use super::room::events::{Event, RoomEvents};
2323

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

37+
impl fmt::Debug for Deduplicator {
38+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39+
formatter.debug_struct("Deduplicator").finish_non_exhaustive()
40+
}
41+
}
42+
3743
impl Deduplicator {
3844
const APPROXIMATED_MAXIMUM_NUMBER_OF_EVENTS: usize = 800_000;
3945
const DESIRED_FALSE_POSITIVE_RATE: f64 = 0.001;

Diff for: crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,15 @@ impl Position {
948948
pub fn index(&self) -> usize {
949949
self.1
950950
}
951+
952+
/// Decrement the index part (see [`Self::index`]), i.e. subtract 1.
953+
///
954+
/// # Panic
955+
///
956+
/// This method will panic if it will underflow, i.e. if the index is 0.
957+
pub(super) fn decrement_index(&mut self) {
958+
self.1 = self.1.checked_sub(1).expect("Cannot decrement the index because it's already 0");
959+
}
951960
}
952961

953962
/// An iterator over a [`LinkedChunk`] that traverses the chunk in backward

0 commit comments

Comments
 (0)