Skip to main content

qdrant_client/
manual_builder.rs

1//! For service types which are in a sub-module in qdrant.rs we can't use dervie_builder if we
2//! want to preserve consistency with the other builders. This is because the generated builders types are
3//! private but for custom type conversions, build() function and constructor with required values we
4//! need access to those private fields. For this reason we introduce a few manually created builder here.
5
6use std::path::PathBuf;
7
8use derive_builder::Builder;
9
10/// Builder for service types within qdrant::points_update_operation.
11/// This sub-module is necessary since we do a `use manual_builder::*` in the qdrant.rs file to have
12/// all builder coming from qdrant.rs file in the user API. However there are some builder types here
13/// whichs name is already part of the qdrant.rs module. This submodule prevents ambiguity for those builder.
14pub mod points_update_operation {
15    use std::collections::HashMap;
16
17    use crate::grpc_macros::builder_type_conversions;
18    use crate::qdrant::points_update_operation::{
19        ClearPayload, DeletePayload, DeletePoints, DeleteVectors, OverwritePayload,
20        PointStructList, SetPayload, UpdateVectors,
21    };
22    use crate::qdrant::{
23        points_selector, PointStruct, PointVectors, PointsSelector, ShardKeySelector, Value,
24        VectorsSelector,
25    };
26
27    #[derive(Clone)]
28    pub struct PointStructListBuilder {
29        points: Vec<PointStruct>,
30        shard_key_selector: Option<ShardKeySelector>,
31    }
32
33    impl PointStructListBuilder {
34        pub fn new(points: impl Into<Vec<PointStruct>>) -> Self {
35            Self {
36                points: points.into(),
37                shard_key_selector: None,
38            }
39        }
40
41        /// Option for custom sharding to specify used shard keys
42        pub fn shard_key_selector(
43            &mut self,
44            shard_key_selector: impl Into<ShardKeySelector>,
45        ) -> &mut Self {
46            self.shard_key_selector = Some(shard_key_selector.into());
47            self
48        }
49
50        fn build_inner(&self) -> Result<PointStructList, ()> {
51            let builder = self.clone();
52            Ok(PointStructList {
53                points: builder.points,
54                shard_key_selector: builder.shard_key_selector,
55                update_filter: None,
56                update_mode: None,
57            })
58        }
59    }
60
61    builder_type_conversions!(PointStructList, PointStructListBuilder);
62
63    #[derive(Clone)]
64    pub struct SetPayloadBuilder {
65        payload: HashMap<String, Value>,
66        points_selector: Option<PointsSelector>,
67        shard_key_selector: Option<ShardKeySelector>,
68        key: Option<String>,
69    }
70
71    impl SetPayloadBuilder {
72        pub fn new(payload: impl Into<HashMap<String, Value>>) -> Self {
73            Self {
74                payload: payload.into(),
75                points_selector: None,
76                shard_key_selector: None,
77                key: None,
78            }
79        }
80
81        /// Affected points
82        pub fn points_selector(
83            &mut self,
84            points_selector: impl Into<points_selector::PointsSelectorOneOf>,
85        ) -> &mut Self {
86            self.points_selector = Some(PointsSelector {
87                points_selector_one_of: Some(points_selector.into()),
88            });
89            self
90        }
91
92        /// Option for custom sharding to specify used shard keys
93        pub fn shard_key_selector(
94            &mut self,
95            shard_key_selector: impl Into<ShardKeySelector>,
96        ) -> &mut Self {
97            self.shard_key_selector = Some(shard_key_selector.into());
98            self
99        }
100
101        /// Option for indicate property of payload
102        pub fn key(&mut self, key: impl Into<String>) -> &mut Self {
103            self.key = Some(key.into());
104            self
105        }
106
107        fn build_inner(&self) -> Result<SetPayload, ()> {
108            let builder = self.clone();
109            Ok(SetPayload {
110                payload: builder.payload,
111                points_selector: builder.points_selector,
112                shard_key_selector: builder.shard_key_selector,
113                key: builder.key,
114            })
115        }
116    }
117
118    builder_type_conversions!(SetPayload, SetPayloadBuilder);
119
120    #[derive(Clone)]
121    pub struct OverwritePayloadBuilder {
122        payload: HashMap<String, Value>,
123        points_selector: Option<PointsSelector>,
124        shard_key_selector: Option<ShardKeySelector>,
125        key: Option<String>,
126    }
127
128    impl OverwritePayloadBuilder {
129        pub fn new(payload: impl Into<HashMap<String, Value>>) -> Self {
130            Self {
131                payload: payload.into(),
132                points_selector: None,
133                shard_key_selector: None,
134                key: None,
135            }
136        }
137
138        /// Affected points
139        pub fn points_selector(
140            &mut self,
141            points_selector: impl Into<points_selector::PointsSelectorOneOf>,
142        ) -> &mut Self {
143            self.points_selector = Some(PointsSelector {
144                points_selector_one_of: Some(points_selector.into()),
145            });
146            self
147        }
148
149        /// Option for custom sharding to specify used shard keys
150        pub fn shard_key_selector(
151            &mut self,
152            shard_key_selector: impl Into<ShardKeySelector>,
153        ) -> &mut Self {
154            self.shard_key_selector = Some(shard_key_selector.into());
155            self
156        }
157
158        /// Option for indicate property of payload
159        pub fn key(&mut self, key: impl Into<String>) -> &mut Self {
160            self.key = Some(key.into());
161            self
162        }
163
164        fn build_inner(&self) -> Result<OverwritePayload, ()> {
165            let builder = self.clone();
166            Ok(OverwritePayload {
167                payload: builder.payload,
168                points_selector: builder.points_selector,
169                shard_key_selector: builder.shard_key_selector,
170                key: builder.key,
171            })
172        }
173    }
174
175    builder_type_conversions!(OverwritePayload, OverwritePayloadBuilder);
176
177    #[derive(Clone)]
178    pub struct DeletePayloadBuilder {
179        keys: Vec<String>,
180        points_selector: Option<PointsSelector>,
181        shard_key_selector: Option<ShardKeySelector>,
182    }
183
184    impl DeletePayloadBuilder {
185        pub fn new(keys: impl Into<Vec<String>>) -> Self {
186            Self {
187                keys: keys.into(),
188                points_selector: None,
189                shard_key_selector: None,
190            }
191        }
192
193        /// Affected points
194        pub fn points_selector(
195            &mut self,
196            points_selector: impl Into<points_selector::PointsSelectorOneOf>,
197        ) -> &mut Self {
198            self.points_selector = Some(PointsSelector {
199                points_selector_one_of: Some(points_selector.into()),
200            });
201            self
202        }
203
204        /// Option for custom sharding to specify used shard keys
205        pub fn shard_key_selector(
206            &mut self,
207            shard_key_selector: impl Into<ShardKeySelector>,
208        ) -> &mut Self {
209            self.shard_key_selector = Some(shard_key_selector.into());
210            self
211        }
212
213        fn build_inner(&self) -> Result<DeletePayload, ()> {
214            let builder = self.clone();
215            Ok(DeletePayload {
216                keys: builder.keys,
217                points_selector: builder.points_selector,
218                shard_key_selector: builder.shard_key_selector,
219            })
220        }
221    }
222
223    builder_type_conversions!(DeletePayload, DeletePayloadBuilder);
224
225    #[derive(Clone)]
226    pub struct UpdateVectorsBuilder {
227        points: Vec<PointVectors>,
228        shard_key_selector: Option<ShardKeySelector>,
229    }
230
231    impl UpdateVectorsBuilder {
232        pub fn new(points: impl Into<Vec<PointVectors>>) -> Self {
233            Self {
234                points: points.into(),
235                shard_key_selector: None,
236            }
237        }
238
239        /// Option for custom sharding to specify used shard keys
240        pub fn shard_key_selector(
241            &mut self,
242            shard_key_selector: impl Into<ShardKeySelector>,
243        ) -> &mut Self {
244            self.shard_key_selector = Some(shard_key_selector.into());
245            self
246        }
247
248        fn build_inner(&self) -> Result<UpdateVectors, ()> {
249            let builder = self.clone();
250            Ok(UpdateVectors {
251                points: builder.points,
252                shard_key_selector: builder.shard_key_selector,
253                update_filter: None,
254            })
255        }
256    }
257
258    builder_type_conversions!(UpdateVectors, UpdateVectorsBuilder);
259
260    #[derive(Clone, Default)]
261    pub struct DeleteVectorsBuilder {
262        points_selector: Option<PointsSelector>,
263        vectors: Option<VectorsSelector>,
264        shard_key_selector: Option<ShardKeySelector>,
265    }
266
267    impl DeleteVectorsBuilder {
268        pub fn new() -> Self {
269            Self {
270                points_selector: None,
271                vectors: None,
272                shard_key_selector: None,
273            }
274        }
275
276        /// Affected points
277        pub fn points_selector(
278            &mut self,
279            points_selector: impl Into<points_selector::PointsSelectorOneOf>,
280        ) -> &mut Self {
281            self.points_selector = Some(PointsSelector {
282                points_selector_one_of: Some(points_selector.into()),
283            });
284            self
285        }
286
287        /// List of vector names to delete
288        pub fn vectors(&mut self, vectors: impl Into<VectorsSelector>) -> &mut Self {
289            self.vectors = Some(vectors.into());
290            self
291        }
292
293        /// Option for custom sharding to specify used shard keys
294        pub fn shard_key_selector(
295            &mut self,
296            shard_key_selector: impl Into<ShardKeySelector>,
297        ) -> &mut Self {
298            self.shard_key_selector = Some(shard_key_selector.into());
299            self
300        }
301
302        fn build_inner(&self) -> Result<DeleteVectors, ()> {
303            let builder = self.clone();
304            Ok(DeleteVectors {
305                points_selector: builder.points_selector,
306                vectors: builder.vectors,
307                shard_key_selector: builder.shard_key_selector,
308            })
309        }
310    }
311
312    builder_type_conversions!(DeleteVectors, DeleteVectorsBuilder);
313
314    #[derive(Clone, Default)]
315    pub struct DeletePointsBuilder {
316        points: Option<PointsSelector>,
317        shard_key_selector: Option<ShardKeySelector>,
318    }
319
320    impl DeletePointsBuilder {
321        pub fn new() -> Self {
322            Self {
323                points: None,
324                shard_key_selector: None,
325            }
326        }
327
328        /// Affected points
329        pub fn points(
330            &mut self,
331            points: impl Into<points_selector::PointsSelectorOneOf>,
332        ) -> &mut Self {
333            self.points = Some(PointsSelector {
334                points_selector_one_of: Some(points.into()),
335            });
336            self
337        }
338
339        /// Option for custom sharding to specify used shard keys
340        pub fn shard_key_selector(
341            &mut self,
342            shard_key_selector: impl Into<ShardKeySelector>,
343        ) -> &mut Self {
344            self.shard_key_selector = Some(shard_key_selector.into());
345            self
346        }
347
348        fn build_inner(&self) -> Result<DeletePoints, ()> {
349            let builder = self.clone();
350            Ok(DeletePoints {
351                points: builder.points,
352                shard_key_selector: builder.shard_key_selector,
353            })
354        }
355    }
356
357    builder_type_conversions!(DeletePoints, DeletePointsBuilder);
358
359    #[derive(Clone, Default)]
360    pub struct ClearPayloadBuilder {
361        points: Option<PointsSelector>,
362        shard_key_selector: Option<ShardKeySelector>,
363    }
364
365    impl ClearPayloadBuilder {
366        pub fn new() -> Self {
367            Self {
368                points: None,
369                shard_key_selector: None,
370            }
371        }
372
373        /// Affected points
374        pub fn points(
375            &mut self,
376            points: impl Into<points_selector::PointsSelectorOneOf>,
377        ) -> &mut Self {
378            self.points = Some(PointsSelector {
379                points_selector_one_of: Some(points.into()),
380            });
381            self
382        }
383
384        /// Option for custom sharding to specify used shard keys
385        pub fn shard_key_selector(
386            &mut self,
387            shard_key_selector: impl Into<ShardKeySelector>,
388        ) -> &mut Self {
389            self.shard_key_selector = Some(shard_key_selector.into());
390            self
391        }
392
393        fn build_inner(&self) -> Result<ClearPayload, ()> {
394            let builder = self.clone();
395            Ok(ClearPayload {
396                points: builder.points,
397                shard_key_selector: builder.shard_key_selector,
398            })
399        }
400    }
401
402    builder_type_conversions!(ClearPayload, ClearPayloadBuilder);
403}
404
405#[derive(Builder)]
406#[builder(
407    build_fn(private, name = "build_inner"),
408    pattern = "owned",
409    custom_constructor
410)]
411pub struct SnapshotDownload {
412    pub out_path: PathBuf,
413    pub collection_name: String,
414    #[builder(default, setter(strip_option, into))]
415    pub snapshot_name: Option<String>,
416    #[builder(default, setter(strip_option, into))]
417    pub rest_api_uri: Option<String>,
418}
419
420impl SnapshotDownloadBuilder {
421    pub fn new(out_path: impl Into<PathBuf>, collection_name: impl Into<String>) -> Self {
422        let mut builder = Self::create_empty();
423        builder.out_path = Some(out_path.into());
424        builder.collection_name = Some(collection_name.into());
425        builder
426    }
427
428    pub fn build(self) -> SnapshotDownload {
429        self.build_inner().unwrap()
430    }
431}
432
433impl From<SnapshotDownloadBuilder> for SnapshotDownload {
434    fn from(value: SnapshotDownloadBuilder) -> Self {
435        value.build()
436    }
437}