atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git

Blame: crates/matrix-sdk-search/src/writer.rs

7ff8adafe9e4atagen2026-10-22 12:30 UTC1
// Copyright 2024 The Matrix.org Foundation C.I.C.
7ff8adafe9e4atagen2026-10-22 12:30 UTC2
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC3
// Licensed under the Apache License, Version 2.0 (the "License");
7ff8adafe9e4atagen2026-10-22 12:30 UTC4
// you may not use this file except in compliance with the License.
7ff8adafe9e4atagen2026-10-22 12:30 UTC5
// You may obtain a copy of the License at
7ff8adafe9e4atagen2026-10-22 12:30 UTC6
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC7
//     http://www.apache.org/licenses/LICENSE-2.0
7ff8adafe9e4atagen2026-10-22 12:30 UTC8
//
7ff8adafe9e4atagen2026-10-22 12:30 UTC9
// Unless required by applicable law or agreed to in writing, software
7ff8adafe9e4atagen2026-10-22 12:30 UTC10
// distributed under the License is distributed on an "AS IS" BASIS,
7ff8adafe9e4atagen2026-10-22 12:30 UTC11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7ff8adafe9e4atagen2026-10-22 12:30 UTC12
// See the License for the specific language governing permissions and
7ff8adafe9e4atagen2026-10-22 12:30 UTC13
// limitations under the License.
7ff8adafe9e4atagen2026-10-22 12:30 UTC14
7ff8adafe9e4atagen2026-10-22 12:30 UTC15
use ruma::EventId;
7ff8adafe9e4atagen2026-10-22 12:30 UTC16
use tantivy::{IndexWriter, TantivyDocument, TantivyError, Term};
7ff8adafe9e4atagen2026-10-22 12:30 UTC17
7ff8adafe9e4atagen2026-10-22 12:30 UTC18
use crate::{
7ff8adafe9e4atagen2026-10-22 12:30 UTC19
    OpStamp,
7ff8adafe9e4atagen2026-10-22 12:30 UTC20
    error::IndexError,
7ff8adafe9e4atagen2026-10-22 12:30 UTC21
    schema::{MatrixSearchIndexSchema, RoomMessageSchema},
7ff8adafe9e4atagen2026-10-22 12:30 UTC22
};
7ff8adafe9e4atagen2026-10-22 12:30 UTC23
7ff8adafe9e4atagen2026-10-22 12:30 UTC24
pub(crate) struct SearchIndexWriter {
7ff8adafe9e4atagen2026-10-22 12:30 UTC25
    inner: IndexWriter,
7ff8adafe9e4atagen2026-10-22 12:30 UTC26
    last_commit_opstamp: OpStamp,
7ff8adafe9e4atagen2026-10-22 12:30 UTC27
    schema: RoomMessageSchema,
7ff8adafe9e4atagen2026-10-22 12:30 UTC28
}
7ff8adafe9e4atagen2026-10-22 12:30 UTC29
7ff8adafe9e4atagen2026-10-22 12:30 UTC30
impl SearchIndexWriter {
7ff8adafe9e4atagen2026-10-22 12:30 UTC31
    pub(crate) fn new(writer: IndexWriter, schema: RoomMessageSchema) -> Self {
7ff8adafe9e4atagen2026-10-22 12:30 UTC32
        Self { last_commit_opstamp: writer.commit_opstamp(), inner: writer, schema }
7ff8adafe9e4atagen2026-10-22 12:30 UTC33
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC34
7ff8adafe9e4atagen2026-10-22 12:30 UTC35
    pub(crate) fn add(&self, document: TantivyDocument) -> Result<OpStamp, IndexError> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC36
        Ok(self.inner.add_document(document)?) // TODO: This is blocking. Handle
7ff8adafe9e4atagen2026-10-22 12:30 UTC37
        // it.
7ff8adafe9e4atagen2026-10-22 12:30 UTC38
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC39
7ff8adafe9e4atagen2026-10-22 12:30 UTC40
    pub(crate) fn remove(&self, event_id: &EventId) {
7ff8adafe9e4atagen2026-10-22 12:30 UTC41
        self.inner
7ff8adafe9e4atagen2026-10-22 12:30 UTC42
            .delete_term(Term::from_field_text(self.schema.deletion_key(), event_id.as_str()));
7ff8adafe9e4atagen2026-10-22 12:30 UTC43
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC44
7ff8adafe9e4atagen2026-10-22 12:30 UTC45
    pub(crate) fn commit(&mut self) -> Result<OpStamp, TantivyError> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC46
        self.last_commit_opstamp = self.inner.commit()?; // TODO: This is blocking. Handle it.
7ff8adafe9e4atagen2026-10-22 12:30 UTC47
        Ok(self.last_commit_opstamp)
7ff8adafe9e4atagen2026-10-22 12:30 UTC48
    }
7ff8adafe9e4atagen2026-10-22 12:30 UTC49
}