tantivy/indexer/
doc_opstamp_mapping.rs

1use crate::{DocId, Opstamp};
2
3// Doc to opstamp is used to identify which
4// document should be deleted.
5//
6// Since the docset matching the query of a delete operation
7// is not computed right when the delete operation is received,
8// we need to find a way to evaluate, for each document,
9// whether the document was added before or after
10// the delete operation. This anteriority is used by comparing
11// the docstamp of the document.
12//
13// The doc to opstamp mapping stores precisely an array
14// indexed by doc id and storing the opstamp of the document.
15//
16// This mapping is NOT necessarily increasing, because
17// we might be sorting documents according to a fast field.
18#[derive(Clone)]
19pub enum DocToOpstampMapping<'a> {
20    WithMap(&'a [Opstamp]),
21    None,
22}
23
24impl DocToOpstampMapping<'_> {
25    /// Assess whether a document should be considered deleted given that it contains
26    /// a deleted term that was deleted at the opstamp: `delete_opstamp`.
27    ///
28    /// This function returns true if the `DocToOpstamp` mapping is none or if
29    /// the `doc_opstamp` is anterior to the delete opstamp.
30    pub fn is_deleted(&self, doc_id: DocId, delete_opstamp: Opstamp) -> bool {
31        match self {
32            Self::WithMap(doc_opstamps) => {
33                let doc_opstamp = doc_opstamps[doc_id as usize];
34                doc_opstamp < delete_opstamp
35            }
36            Self::None => true,
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43
44    use super::DocToOpstampMapping;
45
46    #[test]
47    fn test_doc_to_opstamp_mapping_none() {
48        let doc_to_opstamp_mapping = DocToOpstampMapping::None;
49        assert!(doc_to_opstamp_mapping.is_deleted(1u32, 0u64));
50        assert!(doc_to_opstamp_mapping.is_deleted(1u32, 2u64));
51    }
52
53    #[test]
54    fn test_doc_to_opstamp_mapping_with_map() {
55        let doc_to_opstamp_mapping = DocToOpstampMapping::WithMap(&[5u64, 1u64, 0u64, 4u64, 3u64]);
56        assert_eq!(doc_to_opstamp_mapping.is_deleted(0u32, 2u64), false);
57        assert_eq!(doc_to_opstamp_mapping.is_deleted(1u32, 2u64), true);
58        assert_eq!(doc_to_opstamp_mapping.is_deleted(2u32, 2u64), true);
59        assert_eq!(doc_to_opstamp_mapping.is_deleted(3u32, 2u64), false);
60        assert_eq!(doc_to_opstamp_mapping.is_deleted(4u32, 2u64), false);
61    }
62}