Skip to main content

qdrant_client/builders/
shard_key_selector_builder.rs

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