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