Skip to main content

qdrant_client/builders/
upsert_points_builder.rs

1use crate::qdrant::*;
2
3#[derive(Clone)]
4pub struct UpsertPointsBuilder {
5    /// name of the collection
6    pub(crate) collection_name: Option<String>,
7    /// Wait until the changes have been applied?
8    pub(crate) wait: Option<Option<bool>>,
9    pub(crate) points: Option<Vec<PointStruct>>,
10    /// Write ordering guarantees
11    pub(crate) ordering: Option<Option<WriteOrdering>>,
12    /// Option for custom sharding to specify used shard keys
13    pub(crate) shard_key_selector: Option<Option<ShardKeySelector>>,
14    /// Optional filter to apply to the upsert operation. If set, only points matching the filter will be updated, others will be inserted.
15    pub(crate) update_filter: Option<Option<Filter>>,
16    /// Timeout for the request in seconds
17    pub(crate) timeout: Option<Option<u64>>,
18    /// Mode of the upsert operation: insert_only, upsert (default), update_only
19    pub(crate) update_mode: Option<Option<i32>>,
20}
21
22impl UpsertPointsBuilder {
23    /// name of the collection
24    pub fn collection_name(self, value: String) -> Self {
25        let mut new = self;
26        new.collection_name = Option::Some(value);
27        new
28    }
29    /// Wait until the changes have been applied?
30    pub fn wait(self, value: bool) -> Self {
31        let mut new = self;
32        new.wait = Option::Some(Option::Some(value));
33        new
34    }
35    pub fn points(self, value: Vec<PointStruct>) -> Self {
36        let mut new = self;
37        new.points = Option::Some(value);
38        new
39    }
40    /// Write ordering guarantees
41    pub fn ordering<VALUE: core::convert::Into<WriteOrdering>>(self, value: VALUE) -> Self {
42        let mut new = self;
43        new.ordering = Option::Some(Option::Some(value.into()));
44        new
45    }
46    /// Option for custom sharding to specify used shard keys
47    pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
48        self,
49        value: VALUE,
50    ) -> Self {
51        let mut new = self;
52        new.shard_key_selector = Option::Some(Option::Some(value.into()));
53        new
54    }
55    /// Optional filter to apply to the upsert operation. If set, only points matching the filter will be updated, others will be inserted.
56    pub fn update_filter<VALUE: core::convert::Into<Filter>>(self, value: VALUE) -> Self {
57        let mut new = self;
58        new.update_filter = Option::Some(Option::Some(value.into()));
59        new
60    }
61    /// Timeout for the request in seconds
62    pub fn timeout(self, value: u64) -> Self {
63        let mut new = self;
64        new.timeout = Option::Some(Option::Some(value));
65        new
66    }
67    /// Mode of the upsert operation: insert_only, upsert (default), update_only
68    pub fn update_mode(self, value: UpdateMode) -> Self {
69        let mut new = self;
70        new.update_mode = Option::Some(Option::Some(value.into()));
71        new
72    }
73
74    fn build_inner(self) -> Result<UpsertPoints, UpsertPointsBuilderError> {
75        Ok(UpsertPoints {
76            collection_name: match self.collection_name {
77                Some(value) => value,
78                None => {
79                    return Result::Err(core::convert::Into::into(
80                        ::derive_builder::UninitializedFieldError::from("collection_name"),
81                    ));
82                }
83            },
84            wait: self.wait.unwrap_or_default(),
85            points: match self.points {
86                Some(value) => value,
87                None => {
88                    return Result::Err(core::convert::Into::into(
89                        ::derive_builder::UninitializedFieldError::from("points"),
90                    ));
91                }
92            },
93            ordering: self.ordering.unwrap_or_default(),
94            shard_key_selector: self.shard_key_selector.unwrap_or_default(),
95            update_filter: self.update_filter.unwrap_or_default(),
96            timeout: self.timeout.unwrap_or_default(),
97            update_mode: self.update_mode.unwrap_or_default(),
98        })
99    }
100    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
101    fn create_empty() -> Self {
102        Self {
103            collection_name: core::default::Default::default(),
104            wait: core::default::Default::default(),
105            points: core::default::Default::default(),
106            ordering: core::default::Default::default(),
107            shard_key_selector: core::default::Default::default(),
108            update_filter: core::default::Default::default(),
109            timeout: core::default::Default::default(),
110            update_mode: core::default::Default::default(),
111        }
112    }
113}
114
115impl From<UpsertPointsBuilder> for UpsertPoints {
116    fn from(value: UpsertPointsBuilder) -> Self {
117        value.build_inner().unwrap_or_else(|_| {
118            panic!(
119                "Failed to convert {0} to {1}",
120                "UpsertPointsBuilder", "UpsertPoints"
121            )
122        })
123    }
124}
125
126impl UpsertPointsBuilder {
127    /// Builds the desired type. Can often be omitted.
128    pub fn build(self) -> UpsertPoints {
129        self.build_inner().unwrap_or_else(|_| {
130            panic!(
131                "Failed to build {0} into {1}",
132                "UpsertPointsBuilder", "UpsertPoints"
133            )
134        })
135    }
136}
137
138impl UpsertPointsBuilder {
139    pub(crate) fn empty() -> Self {
140        Self::create_empty()
141    }
142}
143
144#[non_exhaustive]
145#[derive(Debug)]
146pub enum UpsertPointsBuilderError {
147    /// Uninitialized field
148    UninitializedField(&'static str),
149    /// Custom validation error
150    ValidationError(String),
151}
152
153// Implementing the Display trait for better error messages
154impl std::fmt::Display for UpsertPointsBuilderError {
155    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
156        match self {
157            Self::UninitializedField(field) => {
158                write!(f, "`{field}` must be initialized")
159            }
160            Self::ValidationError(error) => write!(f, "{error}"),
161        }
162    }
163}
164
165// Implementing the Error trait
166impl std::error::Error for UpsertPointsBuilderError {}
167
168// Implementing From trait for conversion from UninitializedFieldError
169impl From<derive_builder::UninitializedFieldError> for UpsertPointsBuilderError {
170    fn from(error: derive_builder::UninitializedFieldError) -> Self {
171        Self::UninitializedField(error.field_name())
172    }
173}
174
175// Implementing From trait for conversion from String
176impl From<String> for UpsertPointsBuilderError {
177    fn from(error: String) -> Self {
178        Self::ValidationError(error)
179    }
180}