Skip to main content

qdrant_edge/edge/builders/
edge_config.rs

1//! Fluent builder for [`EdgeConfig`].
2//!
3//! Builder fields mirror [`EdgeConfig`] explicitly (no `inner: EdgeConfig`)
4//! so adding a field to [`EdgeConfig`] forces a compile error here — both
5//! in the struct definition and in the exhaustive `build` step.
6
7use std::collections::HashMap;
8
9use crate::segment::types::{HnswConfig, QuantizationConfig, VectorNameBuf};
10use crate::wal::WalOptions;
11
12use crate::edge::config::optimizers::EdgeOptimizersConfig;
13use crate::edge::config::shard::EdgeConfig;
14use crate::edge::config::vectors::{EdgeSparseVectorParams, EdgeVectorParams};
15
16/// Fluent builder for [`EdgeConfig`].
17///
18/// All fields are optional; at minimum supply at least one dense or sparse
19/// vector via [`Self::vector`] / [`Self::sparse_vector`]. Fields left unset
20/// stay unspecified (`None`) in the built config: loading an existing shard
21/// keeps their persisted values, otherwise defaults apply.
22#[derive(Debug, Default)]
23pub struct EdgeConfigBuilder {
24    on_disk_payload: Option<bool>,
25    vectors: HashMap<VectorNameBuf, EdgeVectorParams>,
26    sparse_vectors: HashMap<VectorNameBuf, EdgeSparseVectorParams>,
27    hnsw_config: Option<HnswConfig>,
28    quantization_config: Option<QuantizationConfig>,
29    optimizers: Option<EdgeOptimizersConfig>,
30    wal_options: Option<WalOptions>,
31    max_search_threads: Option<usize>,
32    search_pool_core: Option<usize>,
33}
34
35impl EdgeConfigBuilder {
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    /// Insert a single dense vector by name. Overwrites any prior entry with
41    /// the same name.
42    pub fn vector(mut self, name: impl Into<VectorNameBuf>, params: EdgeVectorParams) -> Self {
43        self.vectors.insert(name.into(), params);
44        self
45    }
46
47    /// Replace the full dense vectors map.
48    pub fn vectors(mut self, vectors: HashMap<VectorNameBuf, EdgeVectorParams>) -> Self {
49        self.vectors = vectors;
50        self
51    }
52
53    /// Insert a single sparse vector by name. Overwrites any prior entry with
54    /// the same name.
55    pub fn sparse_vector(
56        mut self,
57        name: impl Into<VectorNameBuf>,
58        params: EdgeSparseVectorParams,
59    ) -> Self {
60        self.sparse_vectors.insert(name.into(), params);
61        self
62    }
63
64    /// Replace the full sparse vectors map.
65    pub fn sparse_vectors(
66        mut self,
67        sparse_vectors: HashMap<VectorNameBuf, EdgeSparseVectorParams>,
68    ) -> Self {
69        self.sparse_vectors = sparse_vectors;
70        self
71    }
72
73    pub fn on_disk_payload(mut self, on_disk_payload: bool) -> Self {
74        self.on_disk_payload = Some(on_disk_payload);
75        self
76    }
77
78    pub fn hnsw_config(mut self, hnsw_config: HnswConfig) -> Self {
79        self.hnsw_config = Some(hnsw_config);
80        self
81    }
82
83    pub fn quantization_config(mut self, quantization_config: QuantizationConfig) -> Self {
84        self.quantization_config = Some(quantization_config);
85        self
86    }
87
88    pub fn optimizers(mut self, optimizers: EdgeOptimizersConfig) -> Self {
89        self.optimizers = Some(optimizers);
90        self
91    }
92
93    pub fn wal_options(mut self, wal_options: WalOptions) -> Self {
94        self.wal_options = Some(wal_options);
95        self
96    }
97
98    /// Number of threads in the shard's search thread pool. `0` derives the count from the number
99    /// of CPUs (matching the core search runtime). See [`EdgeConfig::max_search_threads`].
100    pub fn max_search_threads(mut self, max_search_threads: usize) -> Self {
101        self.max_search_threads = Some(max_search_threads);
102        self
103    }
104
105    /// Pin every thread of the shard's search pool to the given CPU core (best-effort). See
106    /// [`EdgeConfig::search_pool_core`].
107    pub fn search_pool_core(mut self, core: usize) -> Self {
108        self.search_pool_core = Some(core);
109        self
110    }
111
112    pub fn build(self) -> EdgeConfig {
113        // Exhaustively destructure Self and construct EdgeConfig: adding a
114        // field to either type forces a compile error here.
115        let Self {
116            on_disk_payload,
117            vectors,
118            sparse_vectors,
119            hnsw_config,
120            quantization_config,
121            optimizers,
122            wal_options,
123            max_search_threads,
124            search_pool_core,
125        } = self;
126        EdgeConfig {
127            on_disk_payload,
128            vectors,
129            sparse_vectors,
130            hnsw_config,
131            quantization_config,
132            optimizers,
133            wal_options,
134            max_search_threads,
135            search_pool_core,
136        }
137    }
138}