Skip to main content

sift_core/index/trigram/
mod.rs

1pub mod builder;
2pub mod file_table;
3pub mod key;
4mod lifecycle;
5mod search;
6pub mod storage;
7
8use std::path::{Path, PathBuf};
9
10use crate::index::{CorpusKind, FileId};
11
12use self::file_table::FileFingerprint;
13pub use key::Trigram;
14
15/// Errors specific to opening or persisting a trigram index.
16#[derive(Debug, thiserror::Error)]
17pub enum TrigramIndexError {
18    #[error("index component missing: {0}")]
19    MissingComponent(PathBuf),
20
21    #[error("IO error: {0}")]
22    Io(#[from] std::io::Error),
23}
24
25/// Opened trigram index with memory-mapped posting lists.
26///
27/// A trigram index is an inverted index mapping every 3-byte sequence found
28/// in the corpus to the set of files that contain it. At query time, required
29/// literals are extracted from the regex pattern, decomposed into trigrams,
30/// and intersected against the posting lists to produce a narrow candidate set.
31///
32/// This is the first shipped index type in Sift's composable index architecture.
33/// It sits alongside future index types (AST, dependency graph, vector) as a
34/// peer in the `Index` enum.
35#[derive(Debug)]
36pub struct TrigramIndex {
37    root: PathBuf,
38    pub(crate) fingerprints: Vec<FileFingerprint>,
39    trigram_sets: storage::trigram_sets::TrigramSets,
40    lexicon: storage::lexicon::Lexicon,
41    postings: storage::postings::Postings,
42    corpus_kind: CorpusKind,
43}
44
45impl TrigramIndex {
46    #[must_use]
47    pub fn file_path(&self, id: FileId) -> Option<&Path> {
48        self.fingerprints.get(id.get()).map(|fp| fp.path.as_path())
49    }
50
51    #[must_use]
52    pub fn file_abs_path(&self, id: FileId) -> Option<PathBuf> {
53        self.fingerprints
54            .get(id.get())
55            .map(|fp| self.root.join(&fp.path))
56    }
57
58    #[must_use]
59    pub fn root(&self) -> &Path {
60        &self.root
61    }
62
63    #[must_use]
64    pub const fn corpus_kind(&self) -> CorpusKind {
65        self.corpus_kind
66    }
67}