matrix_sdk/event_cache/deduplicator.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Simple but efficient types to find duplicated events. See [`Deduplicator`]
//! to learn more.
use std::{collections::BTreeSet, fmt, sync::Mutex};
use growable_bloom_filter::{GrowableBloom, GrowableBloomBuilder};
use matrix_sdk_base::{event_cache::store::EventCacheStoreLock, linked_chunk::Position};
use ruma::{OwnedEventId, OwnedRoomId};
use tracing::{debug, error};
use super::{
room::events::{Event, RoomEvents},
EventCacheError,
};
/// A `Deduplicator` helps to find duplicate events.
pub enum Deduplicator {
InMemory(BloomFilterDeduplicator),
PersistentStore(StoreDeduplicator),
}
impl Deduplicator {
/// Create an empty deduplicator instance that uses an internal Bloom
/// filter.
///
/// Such a deduplicator is stateful, with no initial known events, and it
/// will learn over time by using a Bloom filter which events are
/// duplicates or not.
///
/// When the persistent storage of the event cache is enabled by default,
/// this constructor (and the associated variant) will be removed.
pub fn new_memory_based() -> Self {
Self::InMemory(BloomFilterDeduplicator::new())
}
/// Create new store-based deduplicator that will run queries against the
/// store to find if any event is deduplicated or not.
///
/// This deduplicator is stateless.
///
/// When the persistent storage of the event cache is enabled by default,
/// this will become the default, and [`Deduplicator`] will be replaced
/// with [`StoreDeduplicator`].
pub fn new_store_based(room_id: OwnedRoomId, store: EventCacheStoreLock) -> Self {
Self::PersistentStore(StoreDeduplicator { room_id, store })
}
/// Find duplicates in the given collection of events, and return both
/// valid events (those with an event id) as well as the event ids of
/// duplicate events along with their position.
pub async fn filter_duplicate_events(
&self,
mut events: Vec<Event>,
room_events: &RoomEvents,
) -> Result<DeduplicationOutcome, EventCacheError> {
// Remove all events with no ID, or that is duplicated inside `events`, i.e.
// `events` contains duplicated events in itself, e.g. `[$e0, $e1, $e0]`, here
// `$e0` is duplicated in within `events`.
{
let mut event_ids = BTreeSet::new();
events.retain(|event| {
let Some(event_id) = event.event_id() else {
// No event ID? Bye bye.
return false;
};
// Already seen this event in `events`? Bye bye.
if event_ids.contains(&event_id) {
return false;
}
event_ids.insert(event_id);
// Let's keep this event!
true
});
}
Ok(match self {
Deduplicator::InMemory(dedup) => dedup.filter_duplicate_events(events, room_events),
Deduplicator::PersistentStore(dedup) => {
dedup.filter_duplicate_events(events, room_events).await?
}
})
}
}
/// A deduplication mechanism based on the persistent storage associated to the
/// event cache.
///
/// It will use queries to the persistent storage to figure when events are
/// duplicates or not, making it entirely stateless.
pub struct StoreDeduplicator {
/// The room this deduplicator applies to.
room_id: OwnedRoomId,
/// The actual event cache store implementation used to query events.
store: EventCacheStoreLock,
}
impl StoreDeduplicator {
async fn filter_duplicate_events(
&self,
events: Vec<Event>,
room_events: &RoomEvents,
) -> Result<DeduplicationOutcome, EventCacheError> {
let store = self.store.lock().await?;
// Let the store do its magic ✨
let duplicated_event_ids = store
.filter_duplicated_events(
&self.room_id,
events.iter().filter_map(|event| event.event_id()).collect(),
)
.await?;
// Separate duplicated events in two collections: ones that are in-memory, ones
// that are in the store.
let (in_memory_duplicated_event_ids, in_store_duplicated_event_ids) = {
// Collect all in-memory chunk identifiers.
let in_memory_chunk_identifiers =
room_events.chunks().map(|chunk| chunk.identifier()).collect::<Vec<_>>();
let mut in_memory = vec![];
let mut in_store = vec![];
for (duplicated_event_id, position) in duplicated_event_ids {
if in_memory_chunk_identifiers.contains(&position.chunk_identifier()) {
in_memory.push((duplicated_event_id, position));
} else {
in_store.push((duplicated_event_id, position));
}
}
(in_memory, in_store)
};
Ok(DeduplicationOutcome {
all_events: events,
in_memory_duplicated_event_ids,
in_store_duplicated_event_ids,
})
}
}
/// `BloomFilterDeduplicator` is an efficient type to find duplicated events,
/// using an in-memory cache.
///
/// It uses a [bloom filter] to provide a memory efficient probabilistic answer
/// to: “has event E been seen already?”. False positives are possible, while
/// false negatives are impossible. In the case of a positive reply, we fallback
/// to a linear (backward) search on all events to check whether it's a false
/// positive or not
///
/// [bloom filter]: https://en.wikipedia.org/wiki/Bloom_filter
pub struct BloomFilterDeduplicator {
bloom_filter: Mutex<GrowableBloom>,
}
impl fmt::Debug for BloomFilterDeduplicator {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("Deduplicator").finish_non_exhaustive()
}
}
impl BloomFilterDeduplicator {
// Note: don't use too high numbers here, or the amount of allocated memory will
// explode. See https://github.com/matrix-org/matrix-rust-sdk/pull/4231 for details.
const APPROXIMATED_MAXIMUM_NUMBER_OF_EVENTS: usize = 1_000;
const DESIRED_FALSE_POSITIVE_RATE: f64 = 0.01;
/// Create a new `Deduplicator` with no prior knowledge of known events.
fn new() -> Self {
let bloom_filter = GrowableBloomBuilder::new()
.estimated_insertions(Self::APPROXIMATED_MAXIMUM_NUMBER_OF_EVENTS)
.desired_error_ratio(Self::DESIRED_FALSE_POSITIVE_RATE)
.build();
Self { bloom_filter: Mutex::new(bloom_filter) }
}
/// Find duplicates in the given collection of events, and return both
/// valid events (those with an event id) as well as the event ids of
/// duplicate events along with their position.
fn filter_duplicate_events(
&self,
events: Vec<Event>,
room_events: &RoomEvents,
) -> DeduplicationOutcome {
let mut duplicated_event_ids = Vec::new();
let events = self
.scan_and_learn(events.into_iter(), room_events)
.map(|decorated_event| match decorated_event {
Decoration::Unique(event) => event,
Decoration::Duplicated((event, position)) => {
debug!(event_id = ?event.event_id(), "Found a duplicated event");
let event_id = event
.event_id()
// SAFETY: An event with no ID is not possible, as invalid events are
// already filtered out. Thus, it's safe to unwrap the
// `Option<OwnedEventId>` here.
.expect("The event has no ID");
duplicated_event_ids.push((event_id, position));
// Keep the new event!
event
}
})
.collect::<Vec<_>>();
DeduplicationOutcome {
all_events: events,
in_memory_duplicated_event_ids: duplicated_event_ids,
in_store_duplicated_event_ids: vec![],
}
}
/// Scan a collection of events and detect duplications.
///
/// This method takes a collection of events `new_events_to_scan` and
/// returns a new collection of events, where each event is decorated by
/// a [`Decoration`], so that the caller can decide what to do with
/// these events.
///
/// Each scanned event will update `Self`'s internal state.
///
/// `existing_events` represents all events of a room that already exist.
fn scan_and_learn<'a, I>(
&'a self,
new_events_to_scan: I,
existing_events: &'a RoomEvents,
) -> impl Iterator<Item = Decoration<I::Item>> + 'a
where
I: Iterator<Item = Event> + 'a,
{
new_events_to_scan.filter_map(move |event| {
let Some(event_id) = event.event_id() else {
// The event has no `event_id`. This is normally unreachable as event with no ID
// are already filtered out.
error!(?event, "Found an event with no ID");
return None;
};
Some(if self.bloom_filter.lock().unwrap().check_and_set(&event_id) {
// Oh oh, it looks like we have found a duplicate!
//
// However, bloom filters have false positives. We are NOT sure the event is NOT
// present. Even if the false positive rate is low, we need to
// iterate over all events to ensure it isn't present.
//
// We can iterate over all events to ensure `event` is not present in
// `existing_events`.
let position_of_the_duplicated_event =
existing_events.revents().find_map(|(position, other_event)| {
(other_event.event_id().as_ref() == Some(&event_id)).then_some(position)
});
if let Some(position) = position_of_the_duplicated_event {
Decoration::Duplicated((event, position))
} else {
Decoration::Unique(event)
}
} else {
// Bloom filter has no false negatives. We are sure the event is NOT present: we
// can keep it in the iterator.
Decoration::Unique(event)
})
})
}
}
/// Information about the scanned collection of events.
#[derive(Debug)]
enum Decoration<I> {
/// This event is not duplicated.
Unique(I),
/// This event is duplicated.
Duplicated((I, Position)),
}
pub(super) struct DeduplicationOutcome {
/// All events passed to the deduplicator.
///
/// All events in this collection have a valid event ID.
///
/// This collection does not contain duplicated events in itself.
pub all_events: Vec<Event>,
/// Events in [`Self::all_events`] that are duplicated and present in
/// memory. It means they have been loaded from the store if any.
///
/// Events are sorted by their position, from the newest to the oldest
/// (position is descending).
pub in_memory_duplicated_event_ids: Vec<(OwnedEventId, Position)>,
/// Events in [`Self::all_events`] that are duplicated and present in
/// the store. It means they have **NOT** been loaded from the store into
/// memory yet.
///
/// Events are sorted by their position, from the newest to the oldest
/// (position is descending).
pub in_store_duplicated_event_ids: Vec<(OwnedEventId, Position)>,
}
#[cfg(test)]
mod tests {
use assert_matches2::{assert_let, assert_matches};
use matrix_sdk_base::{deserialized_responses::TimelineEvent, linked_chunk::ChunkIdentifier};
use matrix_sdk_test::{async_test, event_factory::EventFactory};
use ruma::{owned_event_id, serde::Raw, user_id, EventId};
use super::*;
fn timeline_event(event_id: &EventId) -> TimelineEvent {
EventFactory::new()
.text_msg("")
.sender(user_id!("@mnt_io:matrix.org"))
.event_id(event_id)
.into_event()
}
#[async_test]
async fn test_filter_find_duplicates_in_the_input() {
let event_id_0 = owned_event_id!("$ev0");
let event_id_1 = owned_event_id!("$ev1");
let event_0 = timeline_event(&event_id_0);
let event_1 = timeline_event(&event_id_1);
// It doesn't matter which deduplicator we peak, the feature is ensured by the
// “frontend”, not the “backend” of the deduplicator.
let deduplicator = Deduplicator::new_memory_based();
let room_events = RoomEvents::new();
let outcome = deduplicator
.filter_duplicate_events(
vec![
event_0.clone(), // Ok
event_1, // Ok
event_0, // Duplicated
],
&room_events,
)
.await
.unwrap();
// We get 2 events, not 3, because one was duplicated.
assert_eq!(outcome.all_events.len(), 2);
assert_eq!(outcome.all_events[0].event_id(), Some(event_id_0));
assert_eq!(outcome.all_events[1].event_id(), Some(event_id_1));
}
#[async_test]
async fn test_filter_exclude_invalid_events_from_the_input() {
let event_id_0 = owned_event_id!("$ev0");
let event_id_1 = owned_event_id!("$ev1");
let event_0 = timeline_event(&event_id_0);
let event_1 = timeline_event(&event_id_1);
// An event with no ID.
let event_2 = TimelineEvent::new(Raw::from_json_string("{}".to_owned()).unwrap());
// It doesn't matter which deduplicator we peak, the feature is ensured by the
// “frontend”, not the “backend” of the deduplicator.
let deduplicator = Deduplicator::new_memory_based();
let room_events = RoomEvents::new();
let outcome = deduplicator
.filter_duplicate_events(
vec![
event_0.clone(), // Ok
event_1, // Ok
event_2, // Invalid
],
&room_events,
)
.await
.unwrap();
// We get 2 events, not 3, because one was invalid.
assert_eq!(outcome.all_events.len(), 2);
assert_eq!(outcome.all_events[0].event_id(), Some(event_id_0));
assert_eq!(outcome.all_events[1].event_id(), Some(event_id_1));
}
#[async_test]
async fn test_memory_based_duplicated_event_ids_from_in_memory_vs_in_store() {
let event_id_0 = owned_event_id!("$ev0");
let event_id_1 = owned_event_id!("$ev1");
let event_0 = timeline_event(&event_id_0);
let event_1 = timeline_event(&event_id_1);
let mut deduplicator = Deduplicator::new_memory_based();
let mut room_events = RoomEvents::new();
// `event_0` is loaded in memory.
// `event_1` is not loaded in memory, it's new.
{
let Deduplicator::InMemory(bloom_filter) = &mut deduplicator else {
panic!("test is broken, but sky is beautiful");
};
bloom_filter.bloom_filter.lock().unwrap().insert(event_id_0.clone());
room_events.push_events([event_0.clone()]);
}
let outcome = deduplicator
.filter_duplicate_events(vec![event_0, event_1], &room_events)
.await
.unwrap();
// The deduplication says 2 events are valid.
assert_eq!(outcome.all_events.len(), 2);
assert_eq!(outcome.all_events[0].event_id(), Some(event_id_0.clone()));
assert_eq!(outcome.all_events[1].event_id(), Some(event_id_1));
// From these 2 events, 1 is duplicated and has been loaded in memory.
assert_eq!(outcome.in_memory_duplicated_event_ids.len(), 1);
assert_eq!(
outcome.in_memory_duplicated_event_ids[0],
(event_id_0, Position::new(ChunkIdentifier::new(0), 0))
);
// From these 2 events, 0 are duplicated and live in the store.
//
// Note: with the Bloom filter, this value is always empty because there is no
// store.
assert!(outcome.in_store_duplicated_event_ids.is_empty());
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_store_based_duplicated_event_ids_from_in_memory_vs_in_store() {
use std::sync::Arc;
use matrix_sdk_base::{
event_cache::store::{EventCacheStore, MemoryStore},
linked_chunk::Update,
};
use ruma::room_id;
let event_id_0 = owned_event_id!("$ev0");
let event_id_1 = owned_event_id!("$ev1");
let event_id_2 = owned_event_id!("$ev2");
let event_id_3 = owned_event_id!("$ev3");
let event_id_4 = owned_event_id!("$ev4");
// `event_0` and `event_1` are in the store.
// `event_2` and `event_3` is in the store, but also in memory: it's loaded in
// memory from the store.
// `event_4` is nowhere, it's new.
let event_0 = timeline_event(&event_id_0);
let event_1 = timeline_event(&event_id_1);
let event_2 = timeline_event(&event_id_2);
let event_3 = timeline_event(&event_id_3);
let event_4 = timeline_event(&event_id_4);
let event_cache_store = Arc::new(MemoryStore::new());
let room_id = room_id!("!fondue:raclette.ch");
// Prefill the store with ev1 and ev2.
event_cache_store
.handle_linked_chunk_updates(
room_id,
vec![
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(42),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(42), 0),
items: vec![event_0.clone(), event_1.clone()],
},
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(42)),
new: ChunkIdentifier::new(0), // must match the chunk in `RoomEvents`, so 0. It simulates a lazy-load for example.
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(0), 0),
items: vec![event_2.clone(), event_3.clone()],
},
],
)
.await
.unwrap();
let event_cache_store = EventCacheStoreLock::new(event_cache_store, "hodor".to_owned());
let deduplicator = Deduplicator::new_store_based(room_id.to_owned(), event_cache_store);
let mut room_events = RoomEvents::new();
room_events.push_events([event_2.clone(), event_3.clone()]);
let outcome = deduplicator
.filter_duplicate_events(
vec![event_0, event_1, event_2, event_3, event_4],
&room_events,
)
.await
.unwrap();
// The deduplication says 5 events are valid.
assert_eq!(outcome.all_events.len(), 5);
assert_eq!(outcome.all_events[0].event_id(), Some(event_id_0.clone()));
assert_eq!(outcome.all_events[1].event_id(), Some(event_id_1.clone()));
assert_eq!(outcome.all_events[2].event_id(), Some(event_id_2.clone()));
assert_eq!(outcome.all_events[3].event_id(), Some(event_id_3.clone()));
assert_eq!(outcome.all_events[4].event_id(), Some(event_id_4.clone()));
// From these 5 events, 2 are duplicated and have been loaded in memory.
//
// Note that events are sorted by their descending position.
assert_eq!(outcome.in_memory_duplicated_event_ids.len(), 2);
assert_eq!(
outcome.in_memory_duplicated_event_ids[0],
(event_id_2, Position::new(ChunkIdentifier::new(0), 0))
);
assert_eq!(
outcome.in_memory_duplicated_event_ids[1],
(event_id_3, Position::new(ChunkIdentifier::new(0), 1))
);
// From these 4 events, 2 are duplicated and live in the store only, they have
// not been loaded in memory.
//
// Note that events are sorted by their descending position.
assert_eq!(outcome.in_store_duplicated_event_ids.len(), 2);
assert_eq!(
outcome.in_store_duplicated_event_ids[0],
(event_id_0, Position::new(ChunkIdentifier::new(42), 0))
);
assert_eq!(
outcome.in_store_duplicated_event_ids[1],
(event_id_1, Position::new(ChunkIdentifier::new(42), 1))
);
}
#[test]
fn test_bloom_filter_no_duplicate() {
let event_id_0 = owned_event_id!("$ev0");
let event_id_1 = owned_event_id!("$ev1");
let event_id_2 = owned_event_id!("$ev2");
let event_0 = timeline_event(&event_id_0);
let event_1 = timeline_event(&event_id_1);
let event_2 = timeline_event(&event_id_2);
let deduplicator = BloomFilterDeduplicator::new();
let existing_events = RoomEvents::new();
let mut events =
deduplicator.scan_and_learn([event_0, event_1, event_2].into_iter(), &existing_events);
assert_let!(Some(Decoration::Unique(event)) = events.next());
assert_eq!(event.event_id(), Some(event_id_0));
assert_let!(Some(Decoration::Unique(event)) = events.next());
assert_eq!(event.event_id(), Some(event_id_1));
assert_let!(Some(Decoration::Unique(event)) = events.next());
assert_eq!(event.event_id(), Some(event_id_2));
assert!(events.next().is_none());
}
#[test]
fn test_bloom_filter_growth() {
// This test was used as a testbed to observe, using `valgrind --tool=massive`,
// the total memory allocated by the deduplicator. We keep it checked in
// to revive this experiment in the future, if needs be.
let num_rooms = if let Ok(num_rooms) = std::env::var("ROOMS") {
num_rooms.parse().unwrap()
} else {
10
};
let num_events = if let Ok(num_events) = std::env::var("EVENTS") {
num_events.parse().unwrap()
} else {
100
};
let mut dedups = Vec::with_capacity(num_rooms);
for _ in 0..num_rooms {
let dedup = BloomFilterDeduplicator::new();
let existing_events = RoomEvents::new();
for i in 0..num_events {
let event = timeline_event(&EventId::parse(format!("$event{i}")).unwrap());
let mut it = dedup.scan_and_learn([event].into_iter(), &existing_events);
assert_matches!(it.next(), Some(Decoration::Unique(..)));
assert_matches!(it.next(), None);
}
dedups.push(dedup);
}
}
#[cfg(not(target_arch = "wasm32"))] // This uses the cross-process lock, so needs time support.
#[async_test]
async fn test_storage_deduplication() {
use std::sync::Arc;
use matrix_sdk_base::{
event_cache::store::{EventCacheStore as _, MemoryStore},
linked_chunk::{ChunkIdentifier, Position, Update},
};
use matrix_sdk_test::{ALICE, BOB};
use ruma::{event_id, room_id};
let room_id = room_id!("!galette:saucisse.bzh");
let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
let event_cache_store = Arc::new(MemoryStore::new());
let eid1 = event_id!("$1");
let eid2 = event_id!("$2");
let eid3 = event_id!("$3");
let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(eid1).into_event();
let ev2 = f.text_msg("how's it going").sender(*BOB).event_id(eid2).into_event();
let ev3 = f.text_msg("wassup").sender(*ALICE).event_id(eid3).into_event();
// An invalid event (doesn't have an event id.).
let ev4 = TimelineEvent::new(Raw::from_json_string("{}".to_owned()).unwrap());
// Prefill the store with ev1 and ev2.
event_cache_store
.handle_linked_chunk_updates(
room_id,
vec![
// Non empty items chunk.
Update::NewItemsChunk {
previous: None,
new: ChunkIdentifier::new(42),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(42), 0),
items: vec![ev1.clone()],
},
// And another items chunk, non-empty again.
Update::NewItemsChunk {
previous: Some(ChunkIdentifier::new(42)),
new: ChunkIdentifier::new(43),
next: None,
},
Update::PushItems {
at: Position::new(ChunkIdentifier::new(43), 0),
items: vec![ev2.clone()],
},
],
)
.await
.unwrap();
// Wrap the store into its lock.
let event_cache_store = EventCacheStoreLock::new(event_cache_store, "hodor".to_owned());
let deduplicator = Deduplicator::new_store_based(room_id.to_owned(), event_cache_store);
let room_events = RoomEvents::new();
let DeduplicationOutcome {
all_events: events,
in_memory_duplicated_event_ids,
in_store_duplicated_event_ids,
} = deduplicator
.filter_duplicate_events(vec![ev1, ev2, ev3, ev4], &room_events)
.await
.unwrap();
assert_eq!(events.len(), 3);
assert_eq!(events[0].event_id().as_deref(), Some(eid1));
assert_eq!(events[1].event_id().as_deref(), Some(eid2));
assert_eq!(events[2].event_id().as_deref(), Some(eid3));
assert!(in_memory_duplicated_event_ids.is_empty());
assert_eq!(in_store_duplicated_event_ids.len(), 2);
assert_eq!(
in_store_duplicated_event_ids[0],
(eid1.to_owned(), Position::new(ChunkIdentifier::new(42), 0))
);
assert_eq!(
in_store_duplicated_event_ids[1],
(eid2.to_owned(), Position::new(ChunkIdentifier::new(43), 0))
);
}
}