Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 93 additions & 4 deletions crates/matrix-sdk-search/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

use std::{fmt, fs, path::Path, sync::Arc};

use ruma::{OwnedEventId, OwnedRoomId, RoomId, events::AnySyncMessageLikeEvent};
use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId, events::AnySyncMessageLikeEvent};
use tantivy::{
Index, IndexReader, TantivyDocument,
collector::TopDocs,
directory::{MmapDirectory, error::OpenDirectoryError},
query::QueryParser,
schema::Value,
};
use tracing::error;
use tracing::{error, warn};

use crate::{
OpStamp, TANTIVY_INDEX_MEMORY_BUDGET,
Expand Down Expand Up @@ -129,9 +129,15 @@ impl RoomIndex {
/// This which will add/remove/edit an event in the index based on the
/// event type.
pub fn handle_event(&mut self, event: AnySyncMessageLikeEvent) -> Result<(), IndexError> {
let event_id = event.event_id().to_owned();

match self.schema.handle_event(event)? {
RoomIndexOperation::Add(document) => self.writer.add_document(document)?,
};
RoomIndexOperation::Add(document) => {
if !self.contains(&event_id) {
self.writer.add_document(document)?;
}
}
}
Ok(())
}

Expand Down Expand Up @@ -184,6 +190,17 @@ impl RoomIndex {

Ok(ret)
}

fn contains(&self, event_id: &EventId) -> bool {
let search_result = self.search(format!("event_id:\"{event_id}\"").as_str(), 1);
match search_result {
Ok(results) => !results.is_empty(),
Err(err) => {
warn!("Failed to check if event has been indexed, assuming it has: {err}");
true
}
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -283,4 +300,76 @@ mod tests {

Ok(())
}

#[test]
fn test_index_contains_false() -> Result<(), Box<dyn Error>> {
let room_id = room_id!("!room_id:localhost");
let mut index =
RoomIndex::new_in_memory(room_id).expect("failed to make index in ram: {index:?}");

let event_id = event_id!("$event_id:localhost");

index.commit_and_reload()?;

assert!(!index.contains(event_id), "Index should not contain event");

Ok(())
}

#[test]
fn test_index_contains_true() -> Result<(), Box<dyn Error>> {
let room_id = room_id!("!room_id:localhost");
let mut index =
RoomIndex::new_in_memory(room_id).expect("failed to make index in ram: {index:?}");

let event_id = event_id!("$event_id:localhost");
let event = EventFactory::new()
.text_msg("This is a sentence")
.event_id(event_id)
.room(room_id)
.sender(user_id!("@user_id:localhost"))
.into_any_sync_message_like_event();

index.handle_event(event)?;

index.commit_and_reload()?;

assert!(index.contains(event_id), "Index should contain event");

Ok(())
}

#[test]
fn test_indexing_idempotency() -> Result<(), Box<dyn Error>> {
let room_id = room_id!("!room_id:localhost");
let mut index =
RoomIndex::new_in_memory(room_id).expect("failed to make index in ram: {index:?}");

let event_id = event_id!("$event_id:localhost");
let event = EventFactory::new()
.text_msg("This is a sentence")
.event_id(event_id)
.room(room_id)
.sender(user_id!("@user_id:localhost"))
.into_any_sync_message_like_event();

index.handle_event(event.clone())?;

index.commit_and_reload()?;

assert!(index.contains(event_id), "Index should contain event");

// indexing again should do nothing
index.handle_event(event)?;

index.commit_and_reload()?;

assert!(index.contains(event_id), "Index should still contain event");

let result = index.search("sentence", 10).expect("search failed with: {result:?}");

assert_eq!(result.len(), 1, "Index should have ignored second indexing");

Ok(())
}
}
Loading