atagen

matrix-rust-sdk

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