7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2024 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use ruma::EventId; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | use tantivy::{IndexWriter, TantivyDocument, TantivyError, Term}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | use crate::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | OpStamp, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | error::IndexError, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | schema::{MatrixSearchIndexSchema, RoomMessageSchema}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | pub(crate) struct SearchIndexWriter { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | inner: IndexWriter, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | last_commit_opstamp: OpStamp, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | schema: RoomMessageSchema, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | impl SearchIndexWriter { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | pub(crate) fn new(writer: IndexWriter, schema: RoomMessageSchema) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | Self { last_commit_opstamp: writer.commit_opstamp(), inner: writer, schema } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | pub(crate) fn add(&self, document: TantivyDocument) -> Result<OpStamp, IndexError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | Ok(self.inner.add_document(document)?) // TODO: This is blocking. Handle |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | // it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | pub(crate) fn remove(&self, event_id: &EventId) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | self.inner |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | .delete_term(Term::from_field_text(self.schema.deletion_key(), event_id.as_str())); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | pub(crate) fn commit(&mut self) -> Result<OpStamp, TantivyError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | self.last_commit_opstamp = self.inner.commit()?; // TODO: This is blocking. Handle it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | Ok(self.last_commit_opstamp) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | } |