qdrant_client/builders/
update_collection_builder.rs

1use crate::grpc_conversions::metadata::MetadataWrapper;
2use crate::grpc_macros::convert_option;
3use crate::qdrant::*;
4
5#[derive(Clone)]
6pub struct UpdateCollectionBuilder {
7    /// Name of the collection
8    pub(crate) collection_name: Option<String>,
9    /// New configuration parameters for the collection. This operation is blocking, it will only proceed once all current optimizations are complete
10    pub(crate) optimizers_config: Option<Option<OptimizersConfigDiff>>,
11    /// Wait timeout for operation commit in seconds if blocking, if not specified - default value will be supplied
12    pub(crate) timeout: Option<Option<u64>>,
13    /// New configuration parameters for the collection
14    pub(crate) params: Option<Option<CollectionParamsDiff>>,
15    /// New HNSW parameters for the collection index
16    pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
17    /// New vector parameters
18    pub(crate) vectors_config: Option<Option<VectorsConfigDiff>>,
19    /// Quantization configuration of vector
20    quantization_config: Option<quantization_config_diff::Quantization>,
21    /// New sparse vector parameters
22    pub(crate) sparse_vectors_config: Option<Option<SparseVectorConfig>>,
23    /// New strict mode configuration
24    pub(crate) strict_mode_config: Option<Option<StrictModeConfig>>,
25    /// Arbitrary JSON-like metadata for the collection, will be merged with already stored metadata
26    pub(crate) metadata: Option<std::collections::HashMap<String, Value>>,
27}
28
29impl UpdateCollectionBuilder {
30    /// Name of the collection
31    pub fn collection_name(self, value: String) -> Self {
32        let mut new = self;
33        new.collection_name = Option::Some(value);
34        new
35    }
36    /// New configuration parameters for the collection. This operation is blocking, it will only proceed once all current optimizations are complete
37    pub fn optimizers_config<VALUE: core::convert::Into<OptimizersConfigDiff>>(
38        self,
39        value: VALUE,
40    ) -> Self {
41        let mut new = self;
42        new.optimizers_config = Option::Some(Option::Some(value.into()));
43        new
44    }
45    /// Wait timeout for operation commit in seconds if blocking, if not specified - default value will be supplied
46    pub fn timeout(self, value: u64) -> Self {
47        let mut new = self;
48        new.timeout = Option::Some(Option::Some(value));
49        new
50    }
51    /// New configuration parameters for the collection
52    pub fn params<VALUE: core::convert::Into<CollectionParamsDiff>>(self, value: VALUE) -> Self {
53        let mut new = self;
54        new.params = Option::Some(Option::Some(value.into()));
55        new
56    }
57    /// New HNSW parameters for the collection index
58    pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
59        let mut new = self;
60        new.hnsw_config = Option::Some(Option::Some(value.into()));
61        new
62    }
63    /// New vector parameters
64    pub fn vectors_config<VALUE: core::convert::Into<VectorsConfigDiff>>(
65        self,
66        value: VALUE,
67    ) -> Self {
68        let mut new = self;
69        new.vectors_config = Option::Some(Option::Some(value.into()));
70        new
71    }
72    /// Quantization configuration of vector
73    pub fn quantization_config<
74        VALUE: core::convert::Into<quantization_config_diff::Quantization>,
75    >(
76        self,
77        value: VALUE,
78    ) -> Self {
79        let mut new = self;
80        new.quantization_config = Option::Some(value.into());
81        new
82    }
83    /// New sparse vector parameters
84    pub fn sparse_vectors_config<VALUE: core::convert::Into<SparseVectorConfig>>(
85        self,
86        value: VALUE,
87    ) -> Self {
88        let mut new = self;
89        new.sparse_vectors_config = Option::Some(Option::Some(value.into()));
90        new
91    }
92    /// New strict mode configuration
93    pub fn strict_mode_config<VALUE: core::convert::Into<StrictModeConfig>>(
94        self,
95        value: VALUE,
96    ) -> Self {
97        let mut new = self;
98        new.strict_mode_config = Option::Some(Option::Some(value.into()));
99        new
100    }
101    /// Arbitrary JSON-like metadata for the collection, will be merged with already stored metadata
102    pub fn metadata(self, value: impl Into<MetadataWrapper>) -> Self {
103        let mut new = self;
104        new.metadata = Option::Some(value.into().0);
105        new
106    }
107
108    fn build_inner(self) -> Result<UpdateCollection, UpdateCollectionBuilderError> {
109        Ok(UpdateCollection {
110            collection_name: match self.collection_name {
111                Some(value) => value,
112                None => {
113                    return Result::Err(core::convert::Into::into(
114                        ::derive_builder::UninitializedFieldError::from("collection_name"),
115                    ));
116                }
117            },
118            optimizers_config: self.optimizers_config.unwrap_or_default(),
119            timeout: self.timeout.unwrap_or_default(),
120            params: self.params.unwrap_or_default(),
121            hnsw_config: self.hnsw_config.unwrap_or_default(),
122            vectors_config: self.vectors_config.unwrap_or_default(),
123            quantization_config: { convert_option(&self.quantization_config) },
124            sparse_vectors_config: self.sparse_vectors_config.unwrap_or_default(),
125            strict_mode_config: self.strict_mode_config.unwrap_or_default(),
126            metadata: self.metadata.unwrap_or_default(),
127        })
128    }
129    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
130    fn create_empty() -> Self {
131        Self {
132            collection_name: core::default::Default::default(),
133            optimizers_config: core::default::Default::default(),
134            timeout: core::default::Default::default(),
135            params: core::default::Default::default(),
136            hnsw_config: core::default::Default::default(),
137            vectors_config: core::default::Default::default(),
138            quantization_config: core::default::Default::default(),
139            sparse_vectors_config: core::default::Default::default(),
140            strict_mode_config: core::default::Default::default(),
141            metadata: core::default::Default::default(),
142        }
143    }
144}
145
146impl From<UpdateCollectionBuilder> for UpdateCollection {
147    fn from(value: UpdateCollectionBuilder) -> Self {
148        value.build_inner().unwrap_or_else(|_| {
149            panic!(
150                "Failed to convert {0} to {1}",
151                "UpdateCollectionBuilder", "UpdateCollection"
152            )
153        })
154    }
155}
156
157impl UpdateCollectionBuilder {
158    /// Builds the desired type. Can often be omitted.
159    pub fn build(self) -> UpdateCollection {
160        self.build_inner().unwrap_or_else(|_| {
161            panic!(
162                "Failed to build {0} into {1}",
163                "UpdateCollectionBuilder", "UpdateCollection"
164            )
165        })
166    }
167}
168
169impl UpdateCollectionBuilder {
170    pub(crate) fn empty() -> Self {
171        Self::create_empty()
172    }
173}
174
175#[non_exhaustive]
176#[derive(Debug)]
177pub enum UpdateCollectionBuilderError {
178    /// Uninitialized field
179    UninitializedField(&'static str),
180    /// Custom validation error
181    ValidationError(String),
182}
183
184// Implementing the Display trait for better error messages
185impl std::fmt::Display for UpdateCollectionBuilderError {
186    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
187        match self {
188            Self::UninitializedField(field) => {
189                write!(f, "`{field}` must be initialized")
190            }
191            Self::ValidationError(error) => write!(f, "{error}"),
192        }
193    }
194}
195
196// Implementing the Error trait
197impl std::error::Error for UpdateCollectionBuilderError {}
198
199// Implementing From trait for conversion from UninitializedFieldError
200impl From<derive_builder::UninitializedFieldError> for UpdateCollectionBuilderError {
201    fn from(error: derive_builder::UninitializedFieldError) -> Self {
202        Self::UninitializedField(error.field_name())
203    }
204}
205
206// Implementing From trait for conversion from String
207impl From<String> for UpdateCollectionBuilderError {
208    fn from(error: String) -> Self {
209        Self::ValidationError(error)
210    }
211}