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 and fall back to [`EdgeConfig::default`] values
19/// at [`Self::build`] time; at minimum supply at least one dense or sparse
20/// vector via [`Self::vector`] / [`Self::sparse_vector`].
21#[derive(Debug, Default)]
22pub struct EdgeConfigBuilder {
23    on_disk_payload: Option<bool>,
24    vectors: HashMap<VectorNameBuf, EdgeVectorParams>,
25    sparse_vectors: HashMap<VectorNameBuf, EdgeSparseVectorParams>,
26    hnsw_config: Option<HnswConfig>,
27    quantization_config: Option<QuantizationConfig>,
28    optimizers: Option<EdgeOptimizersConfig>,
29    wal_options: Option<WalOptions>,
30}
31
32impl EdgeConfigBuilder {
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    /// Insert a single dense vector by name. Overwrites any prior entry with
38    /// the same name.
39    pub fn vector(mut self, name: impl Into<VectorNameBuf>, params: EdgeVectorParams) -> Self {
40        self.vectors.insert(name.into(), params);
41        self
42    }
43
44    /// Replace the full dense vectors map.
45    pub fn vectors(mut self, vectors: HashMap<VectorNameBuf, EdgeVectorParams>) -> Self {
46        self.vectors = vectors;
47        self
48    }
49
50    /// Insert a single sparse vector by name. Overwrites any prior entry with
51    /// the same name.
52    pub fn sparse_vector(
53        mut self,
54        name: impl Into<VectorNameBuf>,
55        params: EdgeSparseVectorParams,
56    ) -> Self {
57        self.sparse_vectors.insert(name.into(), params);
58        self
59    }
60
61    /// Replace the full sparse vectors map.
62    pub fn sparse_vectors(
63        mut self,
64        sparse_vectors: HashMap<VectorNameBuf, EdgeSparseVectorParams>,
65    ) -> Self {
66        self.sparse_vectors = sparse_vectors;
67        self
68    }
69
70    pub fn on_disk_payload(mut self, on_disk_payload: bool) -> Self {
71        self.on_disk_payload = Some(on_disk_payload);
72        self
73    }
74
75    pub fn hnsw_config(mut self, hnsw_config: HnswConfig) -> Self {
76        self.hnsw_config = Some(hnsw_config);
77        self
78    }
79
80    pub fn quantization_config(mut self, quantization_config: QuantizationConfig) -> Self {
81        self.quantization_config = Some(quantization_config);
82        self
83    }
84
85    pub fn optimizers(mut self, optimizers: EdgeOptimizersConfig) -> Self {
86        self.optimizers = Some(optimizers);
87        self
88    }
89
90    pub fn wal_options(mut self, wal_options: WalOptions) -> Self {
91        self.wal_options = Some(wal_options);
92        self
93    }
94
95    pub fn build(self) -> EdgeConfig {
96        // Exhaustively destructure Self and construct EdgeConfig: adding a
97        // field to either type forces a compile error here.
98        let Self {
99            on_disk_payload,
100            vectors,
101            sparse_vectors,
102            hnsw_config,
103            quantization_config,
104            optimizers,
105            wal_options,
106        } = self;
107        let defaults = EdgeConfig::default();
108        EdgeConfig {
109            on_disk_payload: on_disk_payload.unwrap_or(defaults.on_disk_payload),
110            vectors,
111            sparse_vectors,
112            hnsw_config: hnsw_config.unwrap_or(defaults.hnsw_config),
113            quantization_config: quantization_config.or(defaults.quantization_config),
114            optimizers: optimizers.unwrap_or(defaults.optimizers),
115            wal_options: wal_options.or(defaults.wal_options),
116        }
117    }
118}