Skip to main content

feox_ann/
model.rs

1#[derive(Debug, Clone)]
2pub struct AnnConfig {
3    pub dimensions: usize,
4    pub max_neighbors: usize,
5    pub max_base_neighbors: usize,
6    pub ef_construction: usize,
7    pub ef_search: usize,
8    pub max_level: usize,
9}
10
11impl AnnConfig {
12    pub fn for_dimensions(dimensions: usize) -> Self {
13        Self {
14            dimensions,
15            max_neighbors: 24,
16            max_base_neighbors: 24,
17            ef_construction: 160,
18            ef_search: 64,
19            max_level: 16,
20        }
21    }
22}
23
24#[derive(Clone, Copy)]
25pub struct AnnQuery<'a> {
26    pub vector: &'a [f32],
27    pub top_k: usize,
28    pub ef_search: Option<usize>,
29    pub filter: Option<&'a dyn AnnFilter>,
30}
31
32#[derive(Debug, Clone, PartialEq)]
33pub struct AnnCandidate {
34    pub id: String,
35    pub score: f32,
36}
37
38pub trait AnnFilter {
39    fn accept(&self, id: &str) -> bool;
40}
41
42impl<F> AnnFilter for F
43where
44    F: Fn(&str) -> bool,
45{
46    fn accept(&self, id: &str) -> bool {
47        self(id)
48    }
49}
50
51#[derive(Debug, thiserror::Error)]
52pub enum AnnError {
53    #[error("invalid ANN config: {0}")]
54    InvalidConfig(String),
55    #[error("invalid ANN vector: {0}")]
56    InvalidVector(String),
57    #[error("io error: {0}")]
58    Io(#[from] std::io::Error),
59    #[error("corrupted index snapshot: {0}")]
60    Corrupted(String),
61}