c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1 | use std::{collections::HashMap, path::PathBuf, sync::Arc}; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 2 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 3 | use sha2::{Digest, Sha256}; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 4 | use tantivy::{ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 5 | DocAddress, DocId, DocSet, Index, IndexSettings, ReloadPolicy, Score, Searcher, SegmentReader, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 6 | TantivyDocument, Term, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 7 | collector::{Collector, SegmentCollector}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 8 | directory::{Directory, OwnedBytes, RamDirectory}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 9 | fastfield::{AliveBitSet, write_alive_bitset}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 10 | indexer::{SingleSegmentIndexWriter, merge_filtered_segments}, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 11 | query::{BooleanQuery, Occur, Query, TermSetQuery}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 12 | schema::{ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 13 | FAST, Field, IndexRecordOption, STORED, STRING, Schema, TextFieldIndexing, TextOptions, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 14 | Value, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 15 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 16 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 17 | use tantivy_common::BitSet; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 18 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 19 | use super::{ |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 20 | MessageIndex, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 21 | text::{TOKENIZER, analyzer, build_query}, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 22 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 23 | use crate::{Error, ErrorKind}; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 24 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 25 | const WRITER_BUDGET: usize = 4_000_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 26 | /// segments of a size class merge once there are this many of them |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 27 | const MERGE_FACTOR: usize = 6; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 28 | /// decompressed docstore blocks a searcher keeps per segment, tantivy's default is 100 |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 29 | const DOCSTORE_BLOCKS: usize = 2; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 30 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 31 | #[derive(Clone, Debug, Eq, PartialEq)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 32 | pub(crate) struct Doc { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 33 | pub(crate) key: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 34 | pub(crate) room: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 35 | pub(crate) sender: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 36 | /// the original's, or the edit's while the original hasn't turned up |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 37 | pub(crate) ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 38 | /// the event whose text is indexed, the original or its latest edit |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 39 | pub(crate) content: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 40 | pub(crate) content_ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 41 | /// only kept for provisional docs, read back empty otherwise |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 42 | pub(crate) text: String, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 43 | /// an edit seen before its original, under `provisional_key`. it's kept out of results, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 44 | /// since its timestamp moves to the original's once that turns up |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 45 | pub(crate) provisional: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 46 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 47 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 48 | /// one per target and edit sender, so an edit from anyone else can't crowd out the author's |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 49 | pub(crate) fn provisional_key(target: &str, sender: &str) -> String { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 50 | format!("{target}\n{sender}") |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 51 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 52 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 53 | #[derive(Clone, Debug, Eq, PartialEq)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 54 | pub(crate) struct Hit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 55 | pub(crate) key: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 56 | pub(crate) room: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 57 | pub(crate) sender: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 58 | pub(crate) content: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 59 | pub(crate) ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 60 | pub(crate) tie: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 61 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 62 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 63 | struct SegmentRecord { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 64 | id: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 65 | del_gen: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 66 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 67 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 68 | #[derive(Default)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 69 | pub(crate) struct Changes { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 70 | pub(crate) puts: Vec<(String, Vec<u8>)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 71 | pub(crate) deletes: Vec<String>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 72 | /// the index as it stands once these land, which is what has to be saved with them |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 73 | pub(crate) manifest: serde_json::Value, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 74 | files: Vec<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 75 | dels: Vec<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 76 | removed: Vec<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 77 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 78 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 79 | #[derive(Clone, Copy)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 80 | struct Fields { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 81 | key: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 82 | room: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 83 | sender: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 84 | content: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 85 | text: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 86 | ts: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 87 | tie: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 88 | content_ts: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 89 | pending: Field, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 90 | target: Field, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 91 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 92 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 93 | struct Segment { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 94 | id: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 95 | raw: RamDirectory, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 96 | index: Index, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 97 | searcher: Searcher, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 98 | alive: Arc<BitSet>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 99 | max_doc: u32, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 100 | del_gen: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 101 | persisted: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 102 | deletes_dirty: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 103 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 104 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 105 | impl Segment { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 106 | fn live(&self) -> usize { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 107 | self.alive.len() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 108 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 109 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 110 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 111 | pub(crate) struct KeywordIndex { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 112 | schema: Schema, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 113 | fields: Fields, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 114 | settings: IndexSettings, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 115 | segments: Vec<Segment>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 116 | buffer: HashMap<String, Doc>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 117 | next_id: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 118 | persistent: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 119 | /// merged away since the last commit, with the delete generation they were persisted at |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 120 | removed: Vec<(u64, bool, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 121 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 122 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 123 | fn schema() -> (Schema, Fields) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 124 | let mut builder = Schema::builder(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 125 | let text_options = TextOptions::default().set_indexing_options( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 126 | TextFieldIndexing::default() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 127 | .set_tokenizer(TOKENIZER) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 128 | .set_index_option(IndexRecordOption::WithFreqsAndPositions), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 129 | ); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 130 | let fields = Fields { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 131 | key: builder.add_text_field("key", STRING | STORED), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 132 | room: builder.add_text_field("room", STRING | STORED), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 133 | sender: builder.add_text_field("sender", STRING | STORED), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 134 | content: builder.add_text_field("content", STRING | STORED), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 135 | text: builder.add_text_field("text", text_options), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 136 | ts: builder.add_u64_field("ts", FAST | STORED), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 137 | tie: builder.add_u64_field("tie", FAST), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 138 | content_ts: builder.add_u64_field("content_ts", STORED), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 139 | pending: builder.add_text_field("pending", STORED), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 140 | target: builder.add_text_field("target", STRING), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 141 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 142 | (builder.build(), fields) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 143 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 144 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 145 | fn tie(key: &str) -> u64 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 146 | let digest = Sha256::digest(key.as_bytes()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 147 | u64::from_le_bytes(digest[..8].try_into().expect("a sha256 digest is 32 bytes")) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 148 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 149 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 150 | fn settings() -> IndexSettings { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 151 | // the dedicated compressor is a thread, and wasm32-unknown-unknown has none |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 152 | IndexSettings { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 153 | docstore_compress_dedicated_thread: false, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 154 | ..IndexSettings::default() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 155 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 156 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 157 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 158 | fn tantivy_error( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 159 | context: &'static str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 160 | error: impl std::error::Error + Send + Sync + 'static, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 161 | ) -> Error { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 162 | Error::with_source(ErrorKind::Store, context, error) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 163 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 164 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 165 | fn open_segment(id: u64, raw: RamDirectory, max_doc_hint: Option<u32>) -> Result<Segment, Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 166 | let index = Index::open(raw.clone()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 167 | .map_err(|error| tantivy_error("opening a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 168 | index.tokenizers().register(TOKENIZER, analyzer()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 169 | let reader = index |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 170 | .reader_builder() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 171 | .reload_policy(ReloadPolicy::Manual) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 172 | .doc_store_cache_num_blocks(DOCSTORE_BLOCKS) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 173 | .try_into() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 174 | .map_err(|error| tantivy_error("reading a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 175 | let searcher = reader.searcher(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 176 | let max_doc = searcher |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 177 | .segment_readers() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 178 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 179 | .map(SegmentReader::max_doc) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 180 | .sum(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 181 | if searcher.segment_readers().len() > 1 || max_doc_hint.is_some_and(|hint| hint != max_doc) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 182 | return Err(Error::new( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 183 | ErrorKind::Store, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 184 | "a search segment has an unexpected shape", |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 185 | )); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 186 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 187 | Ok(Segment { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 188 | id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 189 | raw, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 190 | index, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 191 | searcher, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 192 | alive: Arc::new(BitSet::with_max_value_and_full(max_doc)), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 193 | max_doc, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 194 | del_gen: 0, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 195 | persisted: false, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 196 | deletes_dirty: false, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 197 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 198 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 199 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 200 | fn pack(files: &[(String, OwnedBytes)]) -> Vec<u8> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 201 | let mut packed = Vec::with_capacity( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 202 | files |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 203 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 204 | .map(|(name, bytes)| name.len() + bytes.len() + 12) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 205 | .sum::<usize>() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 206 | + 68, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 207 | ); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 208 | packed.extend_from_slice(&(files.len() as u32).to_le_bytes()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 209 | for (name, bytes) in files { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 210 | packed.extend_from_slice(&(name.len() as u32).to_le_bytes()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 211 | packed.extend_from_slice(name.as_bytes()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 212 | packed.extend_from_slice(&(bytes.len() as u64).to_le_bytes()); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 213 | packed.extend_from_slice(bytes.as_slice()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 214 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 215 | packed |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 216 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 217 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 218 | fn unpack(mut bytes: &[u8]) -> Option<Vec<(String, &[u8])>> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 219 | fn take<'a>(bytes: &mut &'a [u8], len: usize) -> Option<&'a [u8]> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 220 | if bytes.len() < len { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 221 | return None; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 222 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 223 | let (head, tail) = bytes.split_at(len); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 224 | *bytes = tail; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 225 | Some(head) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 226 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 227 | let count = u32::from_le_bytes(take(&mut bytes, 4)?.try_into().ok()?); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 228 | let mut files = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 229 | for _ in 0..count { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 230 | let name_len = u32::from_le_bytes(take(&mut bytes, 4)?.try_into().ok()?) as usize; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 231 | let name = std::str::from_utf8(take(&mut bytes, name_len)?) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 232 | .ok()? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 233 | .to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 234 | let len = |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 235 | usize::try_from(u64::from_le_bytes(take(&mut bytes, 8)?.try_into().ok()?)).ok()?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 236 | files.push((name, take(&mut bytes, len)?)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 237 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 238 | bytes.is_empty().then_some(files) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 239 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 240 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 241 | pub(crate) fn segment_key(id: u64) -> String { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 242 | format!("seg/{id}") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 243 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 244 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 245 | pub(crate) fn deletes_key(id: u64, generation: u64) -> String { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 246 | format!("del/{id}/{generation}") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 247 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 248 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 249 | fn manifest(records: impl Iterator<Item = SegmentRecord>, next_id: u64) -> serde_json::Value { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 250 | let segments: Vec<_> = records |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 251 | .map(|record| serde_json::json!([record.id, record.del_gen])) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 252 | .collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 253 | serde_json::json!({ "segments": segments, "next": next_id }) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 254 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 255 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 256 | impl KeywordIndex { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 257 | pub(crate) fn new(persistent: bool) -> Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 258 | let (schema, fields) = schema(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 259 | Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 260 | schema, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 261 | fields, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 262 | settings: settings(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 263 | segments: Vec::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 264 | buffer: HashMap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 265 | next_id: 1, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 266 | persistent, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 267 | removed: Vec::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 268 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 269 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 270 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 271 | /// `blob` hands over what an earlier commit put, once each, and any of it missing or |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 272 | /// unreadable fails the load |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 273 | pub(crate) fn load( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 274 | manifest: &serde_json::Value, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 275 | mut blob: impl FnMut(&str) -> Option<Vec<u8>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 276 | ) -> Result<Self, Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 277 | let invalid = || Error::new(ErrorKind::Store, "the search manifest is unreadable"); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 278 | let records = manifest["segments"] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 279 | .as_array() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 280 | .ok_or_else(invalid)? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 281 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 282 | .map(|record| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 283 | Some(SegmentRecord { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 284 | id: record.get(0)?.as_u64()?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 285 | del_gen: record.get(1)?.as_u64()?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 286 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 287 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 288 | .collect::<Option<Vec<_>>>() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 289 | .ok_or_else(invalid)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 290 | let next_id = manifest["next"].as_u64().ok_or_else(invalid)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 291 | let mut index = Self::new(true); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 292 | let missing = || { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 293 | Error::new( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 294 | ErrorKind::Store, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 295 | "a search segment is missing or unreadable", |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 296 | ) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 297 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 298 | for record in &records { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 299 | let packed = blob(&segment_key(record.id)).ok_or_else(missing)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 300 | let raw = RamDirectory::create(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 301 | for (name, bytes) in unpack(&packed).ok_or_else(missing)? { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 302 | raw.atomic_write(&PathBuf::from(name), bytes) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 303 | .map_err(|error| tantivy_error("loading a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 304 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 305 | drop(packed); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 306 | let mut segment = open_segment(record.id, raw, None)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 307 | if segment.index.schema() != index.schema { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 308 | return Err(Error::new( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 309 | ErrorKind::Store, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 310 | "a search segment has another schema", |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 311 | )); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 312 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 313 | if record.del_gen > 0 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 314 | let bytes = blob(&deletes_key(record.id, record.del_gen)).ok_or_else(missing)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 315 | let alive = AliveBitSet::open(OwnedBytes::new(bytes)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 316 | let mut bits = BitSet::with_max_value(segment.max_doc); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 317 | for doc in alive.iter_alive().filter(|doc| *doc < segment.max_doc) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 318 | bits.insert(doc); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 319 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 320 | segment.alive = Arc::new(bits); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 321 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 322 | segment.del_gen = record.del_gen; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 323 | segment.persisted = true; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 324 | index.segments.push(segment); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 325 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 326 | index.next_id = next_id.max( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 327 | records |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 328 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 329 | .map(|record| record.id + 1) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 330 | .max() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 331 | .unwrap_or(1), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 332 | ); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 333 | Ok(index) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 334 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 335 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 336 | /// the blob names what is persisted points at |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 337 | #[cfg(test)] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 338 | pub(crate) fn referenced(&self) -> Vec<String> { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 339 | let mut keys = Vec::new(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 340 | for segment in self.segments.iter().filter(|segment| segment.persisted) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 341 | keys.push(segment_key(segment.id)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 342 | if segment.del_gen > 0 { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 343 | keys.push(deletes_key(segment.id, segment.del_gen)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 344 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 345 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 346 | keys |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 347 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 348 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 349 | fn document(&self, doc: &Doc) -> TantivyDocument { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 350 | let fields = self.fields; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 351 | let mut document = TantivyDocument::default(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 352 | document.add_text(fields.key, &doc.key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 353 | document.add_text(fields.room, &doc.room); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 354 | document.add_text(fields.sender, &doc.sender); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 355 | document.add_text(fields.content, &doc.content); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 356 | document.add_u64(fields.ts, doc.ts); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 357 | document.add_u64(fields.tie, tie(&doc.key)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 358 | document.add_u64(fields.content_ts, doc.content_ts); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 359 | if doc.provisional { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 360 | document.add_text(fields.pending, &doc.text); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 361 | let target = doc |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 362 | .key |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 363 | .split_once('\n') |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 364 | .map_or(doc.key.as_str(), |(target, _)| target); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 365 | document.add_text(fields.target, target); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 366 | } else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 367 | document.add_text(fields.text, &doc.text); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 368 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 369 | document |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 370 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 371 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 372 | fn stored(&self, document: &TantivyDocument) -> Option<Doc> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 373 | let fields = self.fields; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 374 | let text = |field| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 375 | document |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 376 | .get_first(field) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 377 | .and_then(|value| value.as_str()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 378 | .map(str::to_owned) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 379 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 380 | let number = |field| document.get_first(field).and_then(|value| value.as_u64()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 381 | let pending = text(fields.pending); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 382 | Some(Doc { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 383 | key: text(fields.key)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 384 | room: text(fields.room)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 385 | sender: text(fields.sender)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 386 | ts: number(fields.ts)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 387 | content: text(fields.content)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 388 | content_ts: number(fields.content_ts)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 389 | provisional: pending.is_some(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 390 | text: pending.unwrap_or_default(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 391 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 392 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 393 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 394 | /// the live docs in `segment` whose `field` holds `value` |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 395 | fn find(&self, segment: &Segment, field: Field, value: &str) -> Vec<DocId> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 396 | let term = Term::from_field_text(field, value); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 397 | let mut found = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 398 | for reader in segment.searcher.segment_readers() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 399 | let Ok(inverted) = reader.inverted_index(field) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 400 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 401 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 402 | let Ok(Some(mut postings)) = inverted.read_postings(&term, IndexRecordOption::Basic) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 403 | else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 404 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 405 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 406 | let mut doc = postings.doc(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 407 | while doc != tantivy::TERMINATED { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 408 | if segment.alive.contains(doc) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 409 | found.push(doc); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 410 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 411 | doc = postings.advance(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 412 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 413 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 414 | found |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 415 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 416 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 417 | fn lookup(&self, field: Field, value: &str) -> Option<Doc> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 418 | self.segments.iter().rev().find_map(|segment| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 419 | let doc = *self.find(segment, field, value).first()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 420 | let document: TantivyDocument = segment.searcher.doc(DocAddress::new(0, doc)).ok()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 421 | self.stored(&document) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 422 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 423 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 424 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 425 | /// tombstones every live doc in a segment whose `field` holds `value` |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 426 | fn remove_where(&mut self, field: Field, value: &str) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 427 | let mut removed = false; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 428 | for position in 0..self.segments.len() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 429 | let docs = self.find(&self.segments[position], field, value); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 430 | if docs.is_empty() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 431 | continue; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 432 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 433 | let segment = &mut self.segments[position]; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 434 | let alive = Arc::make_mut(&mut segment.alive); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 435 | for doc in docs { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 436 | alive.remove(doc); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 437 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 438 | segment.deletes_dirty = true; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 439 | removed = true; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 440 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 441 | removed |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 442 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 443 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 444 | fn flush_buffer(&mut self) -> Result<(), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 445 | if self.buffer.is_empty() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 446 | return Ok(()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 447 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 448 | let raw = RamDirectory::create(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 449 | let index = Index::create(raw.clone(), self.schema.clone(), self.settings.clone()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 450 | .map_err(|error| tantivy_error("creating a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 451 | index.tokenizers().register(TOKENIZER, analyzer()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 452 | let mut writer = SingleSegmentIndexWriter::new(index, WRITER_BUDGET) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 453 | .map_err(|error| tantivy_error("writing a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 454 | let mut docs: Vec<_> = self.buffer.values().collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 455 | docs.sort_by_key(|doc| doc.ts); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 456 | for doc in docs { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 457 | writer |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 458 | .add_document(self.document(doc)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 459 | .map_err(|error| tantivy_error("writing a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 460 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 461 | writer |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 462 | .finalize() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 463 | .map_err(|error| tantivy_error("finishing a search segment", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 464 | let segment = open_segment(self.next_id, raw, Some(self.buffer.len() as u32))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 465 | self.next_id += 1; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 466 | self.segments.push(segment); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 467 | self.buffer.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 468 | Ok(()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 469 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 470 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 471 | fn class(live: usize) -> u32 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 472 | live.max(1).ilog(MERGE_FACTOR) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 473 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 474 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 475 | fn forget(&mut self, segment: &Segment) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 476 | if self.persistent { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 477 | self.removed |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 478 | .push((segment.id, segment.persisted, segment.del_gen)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 479 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 480 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 481 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 482 | /// merges a size class once it fills up, and rewrites a segment that's mostly deleted |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 483 | fn compact(&mut self) -> Result<(), Error> { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 484 | let (empty, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut self.segments) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 485 | .into_iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 486 | .partition(|segment| segment.live() == 0); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 487 | self.segments = kept; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 488 | for segment in &empty { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 489 | self.forget(segment); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 490 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 491 | loop { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 492 | let mut classes: HashMap<u32, Vec<usize>> = HashMap::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 493 | for (position, segment) in self.segments.iter().enumerate() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 494 | classes |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 495 | .entry(Self::class(segment.live())) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 496 | .or_default() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 497 | .push(position); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 498 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 499 | let full = classes |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 500 | .into_values() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 501 | .find(|positions| positions.len() >= MERGE_FACTOR); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 502 | let sparse = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 503 | .segments |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 504 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 505 | .position(|segment| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 506 | segment.max_doc >= 1_000 && segment.live() * 2 < segment.max_doc as usize |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 507 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 508 | .map(|position| vec![position]); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 509 | let Some(positions) = full.or(sparse) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 510 | return Ok(()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 511 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 512 | self.merge(&positions)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 513 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 514 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 515 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 516 | fn merge(&mut self, positions: &[usize]) -> Result<(), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 517 | let mut inputs = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 518 | let mut alive = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 519 | for position in positions { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 520 | let segment = &self.segments[*position]; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 521 | let searchable = segment |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 522 | .index |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 523 | .searchable_segments() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 524 | .map_err(|error| tantivy_error("merging search segments", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 525 | let mut bytes = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 526 | write_alive_bitset(&segment.alive, &mut bytes) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 527 | .map_err(|error| tantivy_error("merging search segments", error))?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 528 | let bytes = OwnedBytes::new(bytes); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 529 | for tantivy_segment in searchable { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 530 | inputs.push(tantivy_segment); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 531 | alive.push(Some(AliveBitSet::open(bytes.clone()))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 532 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 533 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 534 | let raw = RamDirectory::create(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 535 | merge_filtered_segments(&inputs, self.settings.clone(), alive, raw.clone()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 536 | .map_err(|error| tantivy_error("merging search segments", error))?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 537 | drop(inputs); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 538 | let merged = open_segment(self.next_id, raw, None)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 539 | self.next_id += 1; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 540 | let (merging, kept): (Vec<_>, Vec<_>) = std::mem::take(&mut self.segments) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 541 | .into_iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 542 | .enumerate() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 543 | .partition(|(position, _)| positions.contains(position)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 544 | self.segments = kept.into_iter().map(|(_, segment)| segment).collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 545 | for (_, segment) in &merging { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 546 | self.forget(segment); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 547 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 548 | self.segments.push(merged); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 549 | Ok(()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 550 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 551 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 552 | fn segment_files(segment: &Segment) -> Result<Vec<u8>, Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 553 | let mut names: Vec<PathBuf> = segment |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 554 | .index |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 555 | .searchable_segment_metas() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 556 | .map_err(|error| tantivy_error("reading a search segment", error))? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 557 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 558 | .flat_map(|meta| meta.list_files()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 559 | .collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 560 | names.push(PathBuf::from("meta.json")); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 561 | let mut files = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 562 | for name in names { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 563 | if !segment.raw.exists(&name).unwrap_or(false) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 564 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 565 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 566 | let bytes = segment |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 567 | .raw |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 568 | .open_read(&name) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 569 | .map_err(|error| tantivy_error("reading a search segment", error))? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 570 | .read_bytes() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 571 | .map_err(|error| tantivy_error("reading a search segment", error))?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 572 | files.push((name.to_string_lossy().into_owned(), bytes)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 573 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 574 | Ok(pack(&files)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 575 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 576 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 577 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 578 | impl MessageIndex for KeywordIndex { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 579 | fn get(&self, key: &str) -> Option<Doc> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 580 | if let Some(doc) = self.buffer.get(key) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 581 | return Some(doc.clone()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 582 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 583 | self.lookup(self.fields.key, key) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 584 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 585 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 586 | fn by_content(&self, content: &str) -> Option<Doc> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 587 | if let Some(doc) = self.buffer.values().find(|doc| doc.content == content) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 588 | return Some(doc.clone()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 589 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 590 | self.lookup(self.fields.content, content) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 591 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 592 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 593 | fn put(&mut self, doc: Doc) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 594 | self.remove(&doc.key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 595 | self.buffer.insert(doc.key.clone(), doc); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 596 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 597 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 598 | fn remove(&mut self, key: &str) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 599 | let buffered = self.buffer.remove(key).is_some(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 600 | let key_field = self.fields.key; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 601 | self.remove_where(key_field, key) || buffered |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 602 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 603 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 604 | fn remove_provisional(&mut self, target: &str) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 605 | let prefix = format!("{target}\n"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 606 | let before = self.buffer.len(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 607 | self.buffer |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 608 | .retain(|key, doc| !(doc.provisional && key.starts_with(&prefix))); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 609 | let buffered = self.buffer.len() != before; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 610 | let target_field = self.fields.target; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 611 | self.remove_where(target_field, target) || buffered |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 612 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 613 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 614 | fn buffered(&self) -> usize { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 615 | self.buffer.len() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 616 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 617 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 618 | fn search( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 619 | &mut self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 620 | query: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 621 | rooms: Option<&[String]>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 622 | before: Option<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 623 | limit: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 624 | ) -> Result<(Vec<Hit>, Vec<String>), Error> { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 625 | let (text_query, terms) = build_query(self.fields.text, query)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 626 | self.flush_buffer()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 627 | let query: Box<dyn Query> = match rooms { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 628 | None => text_query, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 629 | Some(rooms) => Box::new(BooleanQuery::new(vec![ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 630 | (Occur::Must, text_query), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 631 | ( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 632 | Occur::Must, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 633 | Box::new(TermSetQuery::new( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 634 | rooms |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 635 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 636 | .map(|room| Term::from_field_text(self.fields.room, room)), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 637 | )), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 638 | ), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 639 | ])), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 640 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 641 | let mut found = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 642 | for segment in &self.segments { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 643 | let collector = Newest { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 644 | alive: segment.alive.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 645 | before, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 646 | limit, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 647 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 648 | let newest = segment |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 649 | .searcher |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 650 | .search(&*query, &collector) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 651 | .map_err(|error| tantivy_error("searching messages", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 652 | for (ts, tie, doc) in newest { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 653 | found.push((ts, tie, segment.id, doc)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 654 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 655 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 656 | found.sort_by(|a, b| (b.0, b.1).cmp(&(a.0, a.1))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 657 | found.truncate(limit); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 658 | let mut hits = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 659 | for (ts, tie, id, doc) in found { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 660 | let segment = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 661 | .segments |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 662 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 663 | .find(|segment| segment.id == id) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 664 | .expect("the segment was just searched"); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 665 | let document: TantivyDocument = segment |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 666 | .searcher |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 667 | .doc(doc) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 668 | .map_err(|error| tantivy_error("reading a search hit", error))?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 669 | let Some(stored) = self.stored(&document) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 670 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 671 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 672 | hits.push(Hit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 673 | key: stored.key, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 674 | room: stored.room, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 675 | sender: stored.sender, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 676 | content: stored.content, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 677 | ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 678 | tie, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 679 | }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 680 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 681 | Ok((hits, terms)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 682 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 683 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 684 | fn maintain(&mut self) -> Result<(), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 685 | self.flush_buffer()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 686 | self.compact() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 687 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 688 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 689 | fn changes(&mut self) -> Result<Changes, Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 690 | self.maintain()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 691 | let mut changes = Changes::default(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 692 | for (id, persisted, del_gen) in &self.removed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 693 | if *persisted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 694 | changes.deletes.push(segment_key(*id)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 695 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 696 | if *del_gen > 0 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 697 | changes.deletes.push(deletes_key(*id, *del_gen)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 698 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 699 | changes.removed.push(*id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 700 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 701 | let mut records = Vec::with_capacity(self.segments.len()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 702 | for segment in &self.segments { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 703 | if !segment.persisted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 704 | changes |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 705 | .puts |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 706 | .push((segment_key(segment.id), Self::segment_files(segment)?)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 707 | changes.files.push(segment.id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 708 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 709 | let mut del_gen = segment.del_gen; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 710 | if segment.deletes_dirty && segment.live() < segment.max_doc as usize { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 711 | del_gen = segment.del_gen + 1; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 712 | let mut bytes = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 713 | write_alive_bitset(&segment.alive, &mut bytes) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 714 | .map_err(|error| tantivy_error("saving search deletes", error))?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 715 | changes.puts.push((deletes_key(segment.id, del_gen), bytes)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 716 | if segment.del_gen > 0 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 717 | changes |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 718 | .deletes |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 719 | .push(deletes_key(segment.id, segment.del_gen)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 720 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 721 | changes.dels.push((segment.id, del_gen)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 722 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 723 | records.push(SegmentRecord { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 724 | id: segment.id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 725 | del_gen, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 726 | }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 727 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 728 | changes.manifest = manifest(records.into_iter(), self.next_id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 729 | Ok(changes) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 730 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 731 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 732 | fn committed(&mut self, changes: &Changes) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 733 | self.removed |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 734 | .retain(|(id, ..)| !changes.removed.contains(id)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 735 | for segment in &mut self.segments { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 736 | if changes.files.contains(&segment.id) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 737 | segment.persisted = true; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 738 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 739 | if let Some((_, generation)) = changes.dels.iter().find(|(id, _)| *id == segment.id) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 740 | segment.del_gen = *generation; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 741 | segment.deletes_dirty = false; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 742 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 743 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 744 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 745 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 746 | fn detach(&mut self) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 747 | self.persistent = false; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 748 | self.removed.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 749 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 750 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 751 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 752 | /// the `limit` newest live docs older than `before`, by timestamp and then key hash |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 753 | struct Newest { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 754 | alive: Arc<BitSet>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 755 | before: Option<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 756 | limit: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 757 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 758 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 759 | struct NewestSegment { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 760 | alive: Arc<BitSet>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 761 | before: Option<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 762 | limit: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 763 | ts: tantivy::columnar::Column<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 764 | tie: tantivy::columnar::Column<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 765 | segment: u32, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 766 | top: std::collections::BinaryHeap<std::cmp::Reverse<(u64, u64, DocAddress)>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 767 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 768 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 769 | impl Collector for Newest { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 770 | type Fruit = Vec<(u64, u64, DocAddress)>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 771 | type Child = NewestSegment; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 772 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 773 | fn for_segment(&self, segment: u32, reader: &SegmentReader) -> tantivy::Result<NewestSegment> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 774 | Ok(NewestSegment { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 775 | alive: self.alive.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 776 | before: self.before, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 777 | limit: self.limit, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 778 | ts: reader.fast_fields().u64("ts")?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 779 | tie: reader.fast_fields().u64("tie")?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 780 | segment, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 781 | top: std::collections::BinaryHeap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 782 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 783 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 784 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 785 | fn requires_scoring(&self) -> bool { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 786 | false |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 787 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 788 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 789 | fn merge_fruits(&self, fruits: Vec<Self::Fruit>) -> tantivy::Result<Self::Fruit> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 790 | let mut all: Vec<_> = fruits.into_iter().flatten().collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 791 | all.sort_by(|a, b| (b.0, b.1).cmp(&(a.0, a.1))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 792 | all.truncate(self.limit); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 793 | Ok(all) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 794 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 795 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 796 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 797 | impl SegmentCollector for NewestSegment { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 798 | type Fruit = Vec<(u64, u64, DocAddress)>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 799 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 800 | fn collect(&mut self, doc: DocId, _: Score) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 801 | if !self.alive.contains(doc) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 802 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 803 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 804 | let (Some(ts), Some(tie)) = (self.ts.first(doc), self.tie.first(doc)) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 805 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 806 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 807 | if self.before.is_some_and(|before| (ts, tie) >= before) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 808 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 809 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 810 | let entry = std::cmp::Reverse((ts, tie, DocAddress::new(self.segment, doc))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 811 | if self.top.len() < self.limit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 812 | self.top.push(entry); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 813 | } else if self.top.peek().is_some_and(|smallest| entry < *smallest) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 814 | self.top.pop(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 815 | self.top.push(entry); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 816 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 817 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 818 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 819 | fn harvest(self) -> Self::Fruit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 820 | let mut top: Vec<_> = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 821 | .top |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 822 | .into_iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 823 | .map(|std::cmp::Reverse(entry)| entry) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 824 | .collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 825 | top.sort_by(|a, b| (b.0, b.1).cmp(&(a.0, a.1))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 826 | top |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 827 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 828 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 829 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 830 | #[cfg(test)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 831 | #[path = "index_tests.rs"] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 832 | mod tests; |