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#[derive(Debug)]
26pub struct TrigramIndex {
27    root: PathBuf,
28    pub(crate) fingerprints: Vec<FileFingerprint>,
29    trigram_sets: storage::trigram_sets::TrigramSets,
30    lexicon: storage::lexicon::Lexicon,
31    postings: storage::postings::Postings,
32    corpus_kind: CorpusKind,
33}
34
35impl TrigramIndex {
36    #[must_use]
37    pub fn file_path(&self, id: FileId) -> Option<&Path> {
38        self.fingerprints.get(id.get()).map(|fp| fp.path.as_path())
39    }
40
41    #[must_use]
42    pub fn file_abs_path(&self, id: FileId) -> Option<PathBuf> {
43        self.fingerprints
44            .get(id.get())
45            .map(|fp| self.root.join(&fp.path))
46    }
47
48    #[must_use]
49    pub fn root(&self) -> &Path {
50        &self.root
51    }
52
53    #[must_use]
54    pub const fn corpus_kind(&self) -> CorpusKind {
55        self.corpus_kind
56    }
57}