nodedb_vector/
index_config.rs1use crate::hnsw::HnswParams;
6
7#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
9#[non_exhaustive]
10pub enum IndexType {
11 #[default]
13 Hnsw,
14 HnswPq,
16 IvfPq,
18}
19
20impl IndexType {
21 pub fn parse(s: &str) -> Option<Self> {
22 match s.to_lowercase().as_str() {
23 "hnsw" | "" => Some(Self::Hnsw),
24 "hnsw_pq" => Some(Self::HnswPq),
25 "ivf_pq" => Some(Self::IvfPq),
26 _ => None,
27 }
28 }
29}
30
31#[derive(Debug, Clone)]
33pub struct IndexConfig {
34 pub hnsw: HnswParams,
36 pub index_type: IndexType,
38 pub pq_m: usize,
40 pub ivf_cells: usize,
42 pub ivf_nprobe: usize,
44}
45
46pub const DEFAULT_PQ_M: usize = 8;
48pub const DEFAULT_IVF_CELLS: usize = 256;
50pub const DEFAULT_IVF_NPROBE: usize = 16;
52
53impl Default for IndexConfig {
54 fn default() -> Self {
55 Self {
56 hnsw: HnswParams::default(),
57 index_type: IndexType::Hnsw,
58 pq_m: DEFAULT_PQ_M,
59 ivf_cells: DEFAULT_IVF_CELLS,
60 ivf_nprobe: DEFAULT_IVF_NPROBE,
61 }
62 }
63}
64
65impl IndexConfig {
66 pub fn to_ivf_params(&self) -> crate::ivf::IvfPqParams {
68 crate::ivf::IvfPqParams {
69 n_cells: self.ivf_cells,
70 pq_m: self.pq_m,
71 pq_k: 256,
72 nprobe: self.ivf_nprobe,
73 metric: self.hnsw.metric,
74 }
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn parse_index_type() {
84 assert_eq!(IndexType::parse("hnsw"), Some(IndexType::Hnsw));
85 assert_eq!(IndexType::parse(""), Some(IndexType::Hnsw));
86 assert_eq!(IndexType::parse("hnsw_pq"), Some(IndexType::HnswPq));
87 assert_eq!(IndexType::parse("ivf_pq"), Some(IndexType::IvfPq));
88 assert_eq!(IndexType::parse("ivfpq"), None);
89 assert_eq!(IndexType::parse("unknown"), None);
90 }
91
92 #[test]
93 fn default_is_hnsw() {
94 let cfg = IndexConfig::default();
95 assert_eq!(cfg.index_type, IndexType::Hnsw);
96 }
97}