Skip to main content

cipherstash_config/column/index/
mod.rs

1mod array_index_mode;
2mod index_type;
3
4use crate::{operator::Operator, ColumnType};
5pub use array_index_mode::ArrayIndexMode;
6pub use index_type::IndexType;
7use serde::{Deserialize, Serialize};
8
9pub const K_DEFAULT: usize = 6;
10pub const M_DEFAULT: usize = 2048;
11
12#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
13#[serde(tag = "kind", rename_all = "kebab-case")]
14pub enum Tokenizer {
15    EdgeNgram { min_gram: usize, max_gram: usize },
16    Ngram { token_length: usize },
17    Standard,
18}
19
20#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
21#[serde(tag = "kind", rename_all = "kebab-case")]
22pub enum TokenFilter {
23    Upcase,
24    Downcase,
25    Stemmer,
26    Stop,
27}
28
29#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
30pub struct Index {
31    pub version: usize,
32
33    #[serde(flatten)]
34    pub index_type: IndexType,
35}
36
37impl Index {
38    pub fn new(index_type: IndexType) -> Self {
39        Self {
40            version: 1,
41            index_type,
42        }
43    }
44
45    pub fn new_match() -> Self {
46        Self::new(IndexType::Match {
47            token_filters: vec![TokenFilter::Downcase],
48            tokenizer: Tokenizer::Ngram { token_length: 3 },
49            m: M_DEFAULT,
50            k: K_DEFAULT,
51            include_original: true,
52        })
53    }
54
55    pub fn new_ore() -> Self {
56        Self::new(IndexType::Ore)
57    }
58
59    pub fn new_unique() -> Self {
60        Self::new(IndexType::Unique {
61            token_filters: vec![],
62        })
63    }
64
65    pub fn supports(&self, op: &Operator, cast_type: &ColumnType) -> bool {
66        self.index_type.supports(op, cast_type)
67    }
68
69    pub fn is_orderable(&self) -> bool {
70        self.index_type.is_orderable()
71    }
72
73    /// String identifier for the index type.
74    /// Useful when naming columns
75    pub fn as_str(&self) -> &str {
76        self.index_type.as_str()
77    }
78
79    pub fn is_match(&self) -> bool {
80        matches!(&self.index_type, IndexType::Match { .. })
81    }
82
83    pub fn is_ore(&self) -> bool {
84        matches!(&self.index_type, IndexType::Ore)
85    }
86
87    pub fn is_unique(&self) -> bool {
88        matches!(&self.index_type, IndexType::Unique { .. })
89    }
90
91    pub fn is_ste_vec(&self) -> bool {
92        matches!(&self.index_type, IndexType::SteVec { .. })
93    }
94}