qdrant_client/builders/
vector_params_builder.rs1use crate::grpc_macros::convert_option;
2use crate::qdrant::*;
3
4#[must_use]
5#[derive(Clone)]
6pub struct VectorParamsBuilder {
7 pub(crate) size: Option<u64>,
9 pub(crate) distance: Option<i32>,
11 pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
13 quantization_config: Option<quantization_config::Quantization>,
15 pub(crate) on_disk: Option<Option<bool>>,
17 pub(crate) datatype: Option<Option<i32>>,
19 pub(crate) multivector_config: Option<Option<MultiVectorConfig>>,
21 pub(crate) memory: Option<Option<i32>>,
23}
24
25impl VectorParamsBuilder {
26 pub fn size(self, value: u64) -> Self {
28 let mut new = self;
29 new.size = Option::Some(value);
30 new
31 }
32 pub fn distance<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
34 let mut new = self;
35 new.distance = Option::Some(value.into());
36 new
37 }
38 pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
40 let mut new = self;
41 new.hnsw_config = Option::Some(Option::Some(value.into()));
42 new
43 }
44 pub fn quantization_config<VALUE: core::convert::Into<quantization_config::Quantization>>(
46 self,
47 value: VALUE,
48 ) -> Self {
49 let mut new = self;
50 new.quantization_config = Option::Some(value.into());
51 new
52 }
53 pub fn on_disk(self, value: bool) -> Self {
57 let mut new = self;
58 new.on_disk = Option::Some(Option::Some(value));
59 new
60 }
61 pub fn datatype<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
63 let mut new = self;
64 new.datatype = Option::Some(Option::Some(value.into()));
65 new
66 }
67 pub fn multivector_config<VALUE: core::convert::Into<MultiVectorConfig>>(
69 self,
70 value: VALUE,
71 ) -> Self {
72 let mut new = self;
73 new.multivector_config = Option::Some(Option::Some(value.into()));
74 new
75 }
76 pub fn memory<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
80 let mut new = self;
81 new.memory = Option::Some(Option::Some(value.into()));
82 new
83 }
84
85 #[allow(deprecated)]
86 fn build_inner(self) -> Result<VectorParams, VectorParamsBuilderError> {
87 Ok(VectorParams {
88 size: self.size.unwrap_or_default(),
89 distance: self.distance.unwrap_or_default(),
90 hnsw_config: self.hnsw_config.unwrap_or_default(),
91 quantization_config: { convert_option(&self.quantization_config) },
92 on_disk: self.on_disk.unwrap_or_default(),
93 datatype: self.datatype.unwrap_or_default(),
94 multivector_config: self.multivector_config.unwrap_or_default(),
95 memory: self.memory.unwrap_or_default(),
96 })
97 }
98 fn create_empty() -> Self {
100 Self {
101 size: core::default::Default::default(),
102 distance: core::default::Default::default(),
103 hnsw_config: core::default::Default::default(),
104 quantization_config: core::default::Default::default(),
105 on_disk: core::default::Default::default(),
106 datatype: core::default::Default::default(),
107 multivector_config: core::default::Default::default(),
108 memory: core::default::Default::default(),
109 }
110 }
111}
112
113impl From<VectorParamsBuilder> for VectorParams {
114 fn from(value: VectorParamsBuilder) -> Self {
115 value.build_inner().unwrap_or_else(|_| {
116 panic!(
117 "Failed to convert {0} to {1}",
118 "VectorParamsBuilder", "VectorParams"
119 )
120 })
121 }
122}
123
124impl VectorParamsBuilder {
125 pub fn build(self) -> VectorParams {
127 self.build_inner().unwrap_or_else(|_| {
128 panic!(
129 "Failed to build {0} into {1}",
130 "VectorParamsBuilder", "VectorParams"
131 )
132 })
133 }
134}
135
136impl VectorParamsBuilder {
137 pub(crate) fn empty() -> Self {
138 Self::create_empty()
139 }
140}
141
142#[non_exhaustive]
143#[derive(Debug)]
144pub enum VectorParamsBuilderError {
145 UninitializedField(&'static str),
147 ValidationError(String),
149}
150
151impl std::fmt::Display for VectorParamsBuilderError {
153 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
154 match self {
155 Self::UninitializedField(field) => {
156 write!(f, "`{field}` must be initialized")
157 }
158 Self::ValidationError(error) => write!(f, "{error}"),
159 }
160 }
161}
162
163impl std::error::Error for VectorParamsBuilderError {}
165
166impl From<derive_builder::UninitializedFieldError> for VectorParamsBuilderError {
168 fn from(error: derive_builder::UninitializedFieldError) -> Self {
169 Self::UninitializedField(error.field_name())
170 }
171}
172
173impl From<String> for VectorParamsBuilderError {
175 fn from(error: String) -> Self {
176 Self::ValidationError(error)
177 }
178}