qdrant_client/builders/shard_key_selector_builder.rs
1use crate::qdrant::{ShardKey, ShardKeySelector};
2
3#[derive(Clone)]
4pub struct ShardKeySelectorBuilder {
5 /// List of shard keys which should be used in the request
6 pub(crate) shard_keys: Option<Vec<ShardKey>>,
7 /// Fallback shard key to use if the primary shard keys are not available
8 pub(crate) fallback: Option<Option<ShardKey>>,
9}
10
11impl ShardKeySelectorBuilder {
12 /// Create a new ShardKeySelectorBuilder with default values.
13 ///
14 /// # Examples
15 ///
16 /// ```
17 /// use qdrant_client::qdrant::{ShardKey, ShardKeySelectorBuilder};
18 ///
19 /// let selector = ShardKeySelectorBuilder::new()
20 /// .add_shard_key(ShardKey::from("key1".to_string()))
21 /// .build();
22 /// ```
23 pub fn new() -> Self {
24 Self::create_empty()
25 }
26
27 pub fn with_shard_key(shard_key: impl Into<ShardKey>) -> Self {
28 Self::new().add_shard_key(shard_key)
29 }
30
31 /// Create a new ShardKeySelectorBuilder with the given shard keys.
32 ///
33 /// # Arguments
34 ///
35 /// * `shard_keys` - List of shard keys which should be used in the request
36 ///
37 /// # Examples
38 ///
39 /// ```
40 /// use qdrant_client::qdrant::{ShardKey, ShardKeySelectorBuilder};
41 ///
42 /// let selector = ShardKeySelectorBuilder::with_shard_keys(
43 /// vec![ShardKey::from("key1".to_string())]
44 /// ).build();
45 /// ```
46 pub fn with_shard_keys(shard_keys: impl Into<Vec<ShardKey>>) -> Self {
47 Self::new().shard_keys(shard_keys)
48 }
49
50 /// Set the shard keys which should be used in the request.
51 ///
52 /// # Arguments
53 ///
54 /// * `shard_keys` - List of shard keys which should be used in the request
55 pub fn shard_keys(self, value: impl Into<Vec<ShardKey>>) -> Self {
56 let mut new = self;
57 new.shard_keys = Option::Some(value.into());
58 new
59 }
60
61 /// Add a shard key to the list of shard keys.
62 ///
63 /// # Arguments
64 ///
65 /// * `shard_key` - Shard key to add to the list
66 pub fn add_shard_key(mut self, shard_key: impl Into<ShardKey>) -> Self {
67 match self.shard_keys {
68 Some(ref mut keys) => keys.push(shard_key.into()),
69 None => self.shard_keys = Some(vec![shard_key.into()]),
70 }
71 self
72 }
73
74 /// Set a fallback shard key to use if the primary shard keys are not available.
75 ///
76 /// # Arguments
77 ///
78 /// * `fallback` - Fallback shard key
79 pub fn fallback(self, value: impl Into<ShardKey>) -> Self {
80 let mut new = self;
81 new.fallback = Option::Some(Option::Some(value.into()));
82 new
83 }
84
85 pub fn build(self) -> ShardKeySelector {
86 ShardKeySelector {
87 shard_keys: self.shard_keys.unwrap_or_default(),
88 fallback: self.fallback.unwrap_or_default(),
89 }
90 }
91
92 /// Create an empty builder, with all fields set to `None`.
93 fn create_empty() -> Self {
94 Self {
95 shard_keys: core::default::Default::default(),
96 fallback: core::default::Default::default(),
97 }
98 }
99}
100
101impl From<ShardKeySelectorBuilder> for ShardKeySelector {
102 fn from(value: ShardKeySelectorBuilder) -> Self {
103 value.build()
104 }
105}
106
107impl Default for ShardKeySelectorBuilder {
108 fn default() -> Self {
109 Self::create_empty()
110 }
111}