Skip to main content

qdrant_client/builders/
create_shard_key_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct CreateShardKeyBuilder {
6    /// User-defined shard key
7    pub(crate) shard_key: Option<Option<ShardKey>>,
8    /// Number of shards to create per shard key
9    pub(crate) shards_number: Option<Option<u32>>,
10    /// Number of replicas of each shard to create
11    pub(crate) replication_factor: Option<Option<u32>>,
12    /// List of peer ids, allowed to create shards. If empty - all peers are allowed
13    pub(crate) placement: Option<Vec<u64>>,
14    /// Initial replica state for newly created shards. If empty - use Active state.
15    ///
16    /// # Warning
17    ///
18    /// Use with caution! Setting arbirray replica states here may break your Qdrant cluster.
19    pub(crate) initial_state: Option<Option<i32>>,
20}
21
22impl CreateShardKeyBuilder {
23    /// User-defined shard key
24    pub fn shard_key<VALUE: core::convert::Into<ShardKey>>(self, value: VALUE) -> Self {
25        let mut new = self;
26        new.shard_key = Option::Some(Option::Some(value.into()));
27        new
28    }
29    /// Number of shards to create per shard key
30    pub fn shards_number(self, value: u32) -> Self {
31        let mut new = self;
32        new.shards_number = Option::Some(Option::Some(value));
33        new
34    }
35    /// Number of replicas of each shard to create
36    pub fn replication_factor(self, value: u32) -> Self {
37        let mut new = self;
38        new.replication_factor = Option::Some(Option::Some(value));
39        new
40    }
41    /// List of peer ids, allowed to create shards. If empty - all peers are allowed
42    pub fn placement(self, value: Vec<u64>) -> Self {
43        let mut new = self;
44        new.placement = Option::Some(value);
45        new
46    }
47    /// Initial replica state for newly created shards.
48    ///
49    /// Uses Active state by default.
50    ///
51    /// # Warning
52    ///
53    /// Use with caution! Setting arbirray replica states here may break your Qdrant cluster.
54    #[allow(unused_mut)]
55    pub fn initial_state(self, value: ReplicaState) -> Self {
56        let mut new = self;
57        new.initial_state = Option::Some(Option::Some(value as i32));
58        new
59    }
60
61    fn build_inner(self) -> Result<CreateShardKey, std::convert::Infallible> {
62        Ok(CreateShardKey {
63            shard_key: self.shard_key.unwrap_or_default(),
64            shards_number: self.shards_number.unwrap_or_default(),
65            replication_factor: self.replication_factor.unwrap_or_default(),
66            placement: self.placement.unwrap_or_default(),
67            initial_state: self.initial_state.unwrap_or_default(),
68        })
69    }
70    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
71    fn create_empty() -> Self {
72        Self {
73            shard_key: core::default::Default::default(),
74            shards_number: core::default::Default::default(),
75            replication_factor: core::default::Default::default(),
76            placement: core::default::Default::default(),
77            initial_state: core::default::Default::default(),
78        }
79    }
80}
81
82impl From<CreateShardKeyBuilder> for CreateShardKey {
83    fn from(value: CreateShardKeyBuilder) -> Self {
84        value.build_inner().unwrap_or_else(|_| {
85            panic!(
86                "Failed to convert {0} to {1}",
87                "CreateShardKeyBuilder", "CreateShardKey"
88            )
89        })
90    }
91}
92
93impl CreateShardKeyBuilder {
94    /// Builds the desired type. Can often be omitted.
95    pub fn build(self) -> CreateShardKey {
96        self.build_inner().unwrap_or_else(|_| {
97            panic!(
98                "Failed to build {0} into {1}",
99                "CreateShardKeyBuilder", "CreateShardKey"
100            )
101        })
102    }
103}
104
105impl Default for CreateShardKeyBuilder {
106    fn default() -> Self {
107        Self::create_empty()
108    }
109}