rsearch-index 0.3.0

Index engine for rSearch: ES-style mappings on Tantivy, immutable split files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Tombstone application. A tombstone says "hide every version of `_id`
//! whose `_seq` is below `before_seq`". Resolving that against an
//! immutable split yields a fixed set of (segment, doc) addresses, so the
//! set is computed once per split and extended incrementally as newer
//! tombstones arrive — Lucene's live-docs bitset, kept in cache form
//! because splits are shared read-only objects.

use std::collections::HashMap;
use std::sync::Arc;

use tantivy::index::SegmentId;
use tantivy::query::{ConstScorer, EnableScoring, Explanation, Query, Scorer, Weight};
use tantivy::schema::{IndexRecordOption, Term};
use tantivy::{DocId, DocSet, Score, SegmentReader, TERMINATED};

use crate::error::IndexResult;
use crate::mapping::SEQ_FIELD;
use crate::reader::SplitReader;

/// One tombstone, as stored in the metastore.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tombstone {
    /// Monotonic tombstone ordinal; application state is tracked by it.
    pub seq: i64,
    /// The `_id` whose older versions are hidden.
    pub doc_id: String,
    /// Versions with `_seq` strictly below this are hidden.
    pub before_seq: i64,
}

/// The documents of one split hidden by every tombstone applied so far.
#[derive(Debug)]
pub struct ExclusionSet {
    /// Highest tombstone `seq` folded into this set.
    pub applied_through: i64,
    /// Per segment ordinal: sorted, de-duplicated excluded doc ids.
    per_segment: Vec<Arc<[DocId]>>,
    /// Segment id → ordinal (a `SegmentReader` knows its id, not its
    /// ordinal, when a `Weight` builds a scorer for it).
    ordinals: HashMap<SegmentId, usize>,
    total: usize,
}

impl ExclusionSet {
    /// An empty set for a split whose segments are `segment_ids`, in
    /// ordinal order.
    pub fn empty(segment_ids: Vec<SegmentId>) -> Self {
        Self {
            applied_through: 0,
            per_segment: vec![Arc::from(Vec::new()); segment_ids.len()],
            ordinals: segment_ids
                .into_iter()
                .enumerate()
                .map(|(ord, id)| (id, ord))
                .collect(),
            total: 0,
        }
    }

    /// Excluded docs of the segment with the given id (empty if none).
    fn for_segment(&self, id: SegmentId) -> Arc<[DocId]> {
        self.ordinals
            .get(&id)
            .and_then(|ord| self.per_segment.get(*ord))
            .cloned()
            .unwrap_or_else(|| Arc::from(Vec::new()))
    }

    /// Number of excluded documents across all segments.
    pub fn len(&self) -> usize {
        self.total
    }

    /// True when nothing is excluded.
    pub fn is_empty(&self) -> bool {
        self.total == 0
    }

    /// Whether (segment, doc) is excluded.
    pub fn contains(&self, segment_ord: u32, doc: DocId) -> bool {
        self.per_segment
            .get(segment_ord as usize)
            .map(|docs| docs.binary_search(&doc).is_ok())
            .unwrap_or(false)
    }

    /// Fold newly excluded docs (unsorted, may repeat) into a new set.
    fn extended(&self, mut additions: Vec<Vec<DocId>>, applied_through: i64) -> Self {
        let mut per_segment = Vec::with_capacity(self.per_segment.len());
        let mut total = 0;
        for (ord, existing) in self.per_segment.iter().enumerate() {
            let added = additions
                .get_mut(ord)
                .map(std::mem::take)
                .unwrap_or_default();
            let docs: Arc<[DocId]> = if added.is_empty() {
                existing.clone()
            } else {
                let mut merged: Vec<DocId> = existing.iter().copied().chain(added).collect();
                merged.sort_unstable();
                merged.dedup();
                Arc::from(merged)
            };
            total += docs.len();
            per_segment.push(docs);
        }
        Self {
            applied_through,
            per_segment,
            ordinals: self.ordinals.clone(),
            total,
        }
    }
}

impl SplitReader {
    /// Fold `tombstones` (ascending by `seq`) into this split's cached
    /// exclusion set and return the result. Tombstones at or below the
    /// already-applied seq are skipped, so callers pass their whole cached
    /// list each time and only the tail costs anything. Call from a
    /// blocking context (term lookups may read from storage).
    pub fn apply_tombstones(&self, tombstones: &[Tombstone]) -> IndexResult<Arc<ExclusionSet>> {
        // Serialize appliers: concurrent queries on one split wait for the
        // first rather than each repeating the term lookups. A poisoned
        // lock (a panic mid-apply) just yields the last good set.
        let mut slot = self
            .exclusions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let current = slot.clone();
        let start = tombstones.partition_point(|t| t.seq <= current.applied_through);
        let pending = &tombstones[start..];
        let Some(last) = pending.last() else {
            return Ok(current);
        };
        let applied_through = last.seq;
        let id_field = match self.mapped_schema().id {
            Some(field) => field,
            None => {
                // Legacy split: no ids, so tombstones can never apply.
                let next = Arc::new(current.extended(Vec::new(), applied_through));
                *slot = next.clone();
                return Ok(next);
            }
        };
        // Nothing in the split is older than a tombstone's bound when the
        // split's lowest _seq is already at or past it.
        let seq_min = self.meta.split.seq_min;
        let searcher = self.searcher()?;
        let segments = searcher.segment_readers();
        let mut additions: Vec<Vec<DocId>> = vec![Vec::new(); segments.len()];
        for (ord, segment) in segments.iter().enumerate() {
            let inverted = segment.inverted_index(id_field)?;
            let seq_column = segment.fast_fields().i64(SEQ_FIELD)?;
            for tombstone in pending {
                if seq_min.is_some_and(|min| tombstone.before_seq <= min) {
                    continue;
                }
                let term = Term::from_field_text(id_field, &tombstone.doc_id);
                let Some(mut postings) = inverted.read_postings(&term, IndexRecordOption::Basic)?
                else {
                    continue;
                };
                let mut doc = postings.doc();
                while doc != TERMINATED {
                    if seq_column
                        .first(doc)
                        .is_some_and(|seq| seq < tombstone.before_seq)
                    {
                        additions[ord].push(doc);
                    }
                    doc = postings.advance();
                }
            }
        }
        let next = Arc::new(current.extended(additions, applied_through));
        *slot = next.clone();
        Ok(next)
    }

    /// Start the exclusion bookkeeping at `applied_through`: tombstones at
    /// or below it are known to hide nothing in this split (it was built —
    /// by compaction or a merge — with them applied), so they are never
    /// looked up. Only moves forward, and only while nothing has been
    /// applied yet (the cached set would otherwise be incomplete).
    pub fn seed_applied_through(&self, applied_through: i64) {
        let mut slot = self
            .exclusions
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if slot.applied_through == 0 && slot.is_empty() && applied_through > 0 {
            *slot = Arc::new(ExclusionSet {
                applied_through,
                per_segment: slot.per_segment.clone(),
                ordinals: slot.ordinals.clone(),
                total: 0,
            });
        }
    }
}

/// Matches exactly the documents in an [`ExclusionSet`] — used as a
/// `must_not` clause so counts, hits, and aggregations all skip them.
#[derive(Debug, Clone)]
pub struct ExcludeDocsQuery {
    set: Arc<ExclusionSet>,
}

impl ExcludeDocsQuery {
    /// A query over the given set.
    pub fn new(set: Arc<ExclusionSet>) -> Self {
        Self { set }
    }
}

impl Query for ExcludeDocsQuery {
    fn weight(&self, _enable_scoring: EnableScoring<'_>) -> tantivy::Result<Box<dyn Weight>> {
        Ok(Box::new(ExcludeDocsWeight {
            set: self.set.clone(),
        }))
    }
}

struct ExcludeDocsWeight {
    set: Arc<ExclusionSet>,
}

impl Weight for ExcludeDocsWeight {
    fn scorer(&self, reader: &SegmentReader, boost: Score) -> tantivy::Result<Box<dyn Scorer>> {
        let docs = self.set.for_segment(reader.segment_id());
        Ok(Box::new(ConstScorer::new(SortedDocSet::new(docs), boost)))
    }

    fn explain(&self, reader: &SegmentReader, doc: DocId) -> tantivy::Result<Explanation> {
        let docs = self.set.for_segment(reader.segment_id());
        if docs.binary_search(&doc).is_ok() {
            Ok(Explanation::new("tombstoned document", 1.0))
        } else {
            Err(tantivy::TantivyError::InvalidArgument(
                "document is not excluded".to_string(),
            ))
        }
    }
}

/// A `DocSet` over a sorted, de-duplicated slice of doc ids.
struct SortedDocSet {
    docs: Arc<[DocId]>,
    cursor: usize,
}

impl SortedDocSet {
    fn new(docs: Arc<[DocId]>) -> Self {
        Self { docs, cursor: 0 }
    }
}

impl DocSet for SortedDocSet {
    fn advance(&mut self) -> DocId {
        self.cursor = self.cursor.saturating_add(1).min(self.docs.len());
        self.doc()
    }

    fn seek(&mut self, target: DocId) -> DocId {
        // Docs are sorted: jump to the first id >= target.
        let rest = &self.docs[self.cursor.min(self.docs.len())..];
        self.cursor += rest.partition_point(|&d| d < target);
        self.doc()
    }

    fn doc(&self) -> DocId {
        self.docs.get(self.cursor).copied().unwrap_or(TERMINATED)
    }

    fn size_hint(&self) -> u32 {
        self.docs.len().saturating_sub(self.cursor) as u32
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::builder::SplitBuilder;
    use crate::cache::SplitCache;
    use crate::document::DocIdentity;
    use crate::mapping::{IndexMapping, MappedSchema};
    use rsearch_storage::{FsStorage, Storage};
    use tantivy::collector::Count;
    use tantivy::query::{AllQuery, BooleanQuery, Occur, TermQuery};

    /// alpha@10, beta@20, alpha@30, gamma@40 (two segments worth of adds
    /// are irrelevant — one commit, one segment).
    async fn open_split() -> (Arc<SplitReader>, tempfile::TempDir, tempfile::TempDir) {
        let store_dir = tempfile::tempdir().unwrap();
        let cache_dir = tempfile::tempdir().unwrap();
        let scratch = tempfile::tempdir().unwrap();
        let storage: Arc<dyn Storage> = Arc::new(FsStorage::new(store_dir.path()));
        let schema = MappedSchema::build(IndexMapping::default());
        let mut builder = SplitBuilder::new("docs", schema, scratch.path(), 20 << 20).unwrap();
        for (id, seq) in [("alpha", 10), ("beta", 20), ("alpha", 30), ("gamma", 40)] {
            builder
                .add_document(
                    serde_json::json!({"id": id}),
                    None,
                    &DocIdentity::new(id, seq),
                    tantivy::DateTime::from_timestamp_millis(1_753_300_000_000),
                )
                .unwrap();
        }
        let packaged = builder.finish().unwrap();
        assert_eq!(packaged.meta.seq_min, Some(10));
        assert_eq!(packaged.meta.seq_max, Some(40));
        let key = format!("splits/{}.split", packaged.meta.split_id);
        storage.put_file(&key, &packaged.file_path).await.unwrap();
        let cache = Arc::new(SplitCache::new(cache_dir.path(), 1 << 30).unwrap());
        let reader = Arc::new(SplitReader::open(storage, &key, cache).await.unwrap());
        (reader, store_dir, cache_dir)
    }

    fn count(reader: &SplitReader, set: &Arc<ExclusionSet>, query: Box<dyn Query>) -> usize {
        let searcher = reader.searcher().unwrap();
        let wrapped = BooleanQuery::new(vec![
            (Occur::Must, query),
            (Occur::MustNot, Box::new(ExcludeDocsQuery::new(set.clone()))),
        ]);
        searcher.search(&wrapped, &Count).unwrap()
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn tombstones_hide_older_versions_incrementally() {
        let (reader, _s, _c) = open_split().await;
        let r = reader.clone();
        tokio::task::spawn_blocking(move || {
            let id_field = r.mapped_schema().id.unwrap();
            let alpha = || -> Box<dyn Query> {
                Box::new(TermQuery::new(
                    Term::from_field_text(id_field, "alpha"),
                    IndexRecordOption::Basic,
                ))
            };

            // Nothing applied: everything visible.
            let empty = r.apply_tombstones(&[]).unwrap();
            assert!(empty.is_empty());
            assert_eq!(count(&r, &empty, Box::new(AllQuery)), 4);

            // "alpha replaced at seq 30": hides alpha@10 only.
            let set = r
                .apply_tombstones(&[Tombstone {
                    seq: 1,
                    doc_id: "alpha".into(),
                    before_seq: 30,
                }])
                .unwrap();
            assert_eq!(set.len(), 1);
            assert_eq!(set.applied_through, 1);
            assert_eq!(count(&r, &set, Box::new(AllQuery)), 3);
            assert_eq!(count(&r, &set, alpha()), 1);

            // Re-applying the same list is a no-op (already past seq 1).
            let again = r
                .apply_tombstones(&[Tombstone {
                    seq: 1,
                    doc_id: "alpha".into(),
                    before_seq: 30,
                }])
                .unwrap();
            assert!(Arc::ptr_eq(&set, &again));

            // Delete beta (before everything) and alpha entirely: the
            // cached set extends by the tail only.
            let set = r
                .apply_tombstones(&[
                    Tombstone {
                        seq: 1,
                        doc_id: "alpha".into(),
                        before_seq: 30,
                    },
                    Tombstone {
                        seq: 2,
                        doc_id: "beta".into(),
                        before_seq: 1_000,
                    },
                    Tombstone {
                        seq: 3,
                        doc_id: "alpha".into(),
                        before_seq: 1_000,
                    },
                ])
                .unwrap();
            assert_eq!(set.len(), 3);
            assert_eq!(set.applied_through, 3);
            assert_eq!(count(&r, &set, Box::new(AllQuery)), 1);
            assert_eq!(count(&r, &set, alpha()), 0);
            // A tombstone at or below the split's lowest _seq is skipped
            // without any lookup; unknown ids are harmless.
            let set = r
                .apply_tombstones(&[
                    Tombstone {
                        seq: 4,
                        doc_id: "gamma".into(),
                        before_seq: 10,
                    },
                    Tombstone {
                        seq: 5,
                        doc_id: "nobody".into(),
                        before_seq: 1_000,
                    },
                ])
                .unwrap();
            assert_eq!(set.len(), 3);
            assert_eq!(set.applied_through, 5);
            assert!(set.contains(0, 0));
            assert!(!set.contains(0, 3));
        })
        .await
        .unwrap();
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn seeded_reader_skips_already_applied_tombstones() {
        let (reader, _s, _c) = open_split().await;
        tokio::task::spawn_blocking(move || {
            // The split was (notionally) rebuilt with tombstones through
            // seq 5 applied: those are never looked up, later ones are.
            reader.seed_applied_through(5);
            let set = reader
                .apply_tombstones(&[
                    Tombstone {
                        seq: 3,
                        doc_id: "alpha".into(),
                        before_seq: 1_000,
                    },
                    Tombstone {
                        seq: 7,
                        doc_id: "beta".into(),
                        before_seq: 1_000,
                    },
                ])
                .unwrap();
            assert_eq!(set.applied_through, 7);
            assert_eq!(set.len(), 1, "only beta (seq 7) was applied");
            // Seeding after application is a no-op.
            reader.seed_applied_through(100);
            assert_eq!(reader.apply_tombstones(&[]).unwrap().applied_through, 7);
        })
        .await
        .unwrap();
    }
}