qdrant_client/builders/
optimizers_config_diff_builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::qdrant::*;

pub struct OptimizersConfigDiffBuilder {
    ///
    /// The minimal fraction of deleted vectors in a segment, required to perform segment optimization
    pub(crate) deleted_threshold: Option<Option<f64>>,
    ///
    /// The minimal number of vectors in a segment, required to perform segment optimization
    pub(crate) vacuum_min_vector_number: Option<Option<u64>>,
    ///
    /// Target amount of segments the optimizer will try to keep.
    /// Real amount of segments may vary depending on multiple parameters:
    ///
    /// - Amount of stored points.
    /// - Current write RPS.
    ///
    /// It is recommended to select the default number of segments as a factor of the number of search threads,
    /// so that each segment would be handled evenly by one of the threads.
    pub(crate) default_segment_number: Option<Option<u64>>,
    ///
    /// Do not create segments larger this size (in kilobytes).
    /// Large segments might require disproportionately long indexation times,
    /// therefore it makes sense to limit the size of segments.
    ///
    /// If indexing speed is more important - make this parameter lower.
    /// If search speed is more important - make this parameter higher.
    /// Note: 1Kb = 1 vector of size 256
    /// If not set, will be automatically selected considering the number of available CPUs.
    pub(crate) max_segment_size: Option<Option<u64>>,
    ///
    /// Maximum size (in kilobytes) of vectors to store in-memory per segment.
    /// Segments larger than this threshold will be stored as read-only memmaped file.
    ///
    /// Memmap storage is disabled by default, to enable it, set this threshold to a reasonable value.
    ///
    /// To disable memmap storage, set this to `0`.
    ///
    /// Note: 1Kb = 1 vector of size 256
    pub(crate) memmap_threshold: Option<Option<u64>>,
    ///
    /// Maximum size (in kilobytes) of vectors allowed for plain index, exceeding this threshold will enable vector indexing
    ///
    /// Default value is 20,000, based on <<https://github.com/google-research/google-research/blob/master/scann/docs/algorithms.md>.>
    ///
    /// To disable vector indexing, set to `0`.
    ///
    /// Note: 1kB = 1 vector of size 256.
    pub(crate) indexing_threshold: Option<Option<u64>>,
    ///
    /// Interval between forced flushes.
    pub(crate) flush_interval_sec: Option<Option<u64>>,
    ///
    /// Max number of threads (jobs) for running optimizations per shard.
    /// Each optimization job will also use `max_indexing_threads` threads by itself for index building.
    ///
    /// - If `auto` - have no limit and choose dynamically to saturate CPU.
    /// - If `disabled` or `0` - no optimization threads, optimizations will be disabled.
    pub(crate) max_optimization_threads: Option<Option<MaxOptimizationThreads>>,
}

impl OptimizersConfigDiffBuilder {
    ///
    /// The minimal fraction of deleted vectors in a segment, required to perform segment optimization
    #[allow(unused_mut)]
    pub fn deleted_threshold(self, value: f64) -> Self {
        let mut new = self;
        new.deleted_threshold = Option::Some(Option::Some(value));
        new
    }
    ///
    /// The minimal number of vectors in a segment, required to perform segment optimization
    #[allow(unused_mut)]
    pub fn vacuum_min_vector_number(self, value: u64) -> Self {
        let mut new = self;
        new.vacuum_min_vector_number = Option::Some(Option::Some(value));
        new
    }
    ///
    /// Target amount of segments the optimizer will try to keep.
    /// Real amount of segments may vary depending on multiple parameters:
    ///
    /// - Amount of stored points.
    /// - Current write RPS.
    ///
    /// It is recommended to select the default number of segments as a factor of the number of search threads,
    /// so that each segment would be handled evenly by one of the threads.
    #[allow(unused_mut)]
    pub fn default_segment_number(self, value: u64) -> Self {
        let mut new = self;
        new.default_segment_number = Option::Some(Option::Some(value));
        new
    }
    ///
    /// Do not create segments larger this size (in kilobytes).
    /// Large segments might require disproportionately long indexation times,
    /// therefore it makes sense to limit the size of segments.
    ///
    /// If indexing speed is more important - make this parameter lower.
    /// If search speed is more important - make this parameter higher.
    /// Note: 1Kb = 1 vector of size 256
    /// If not set, will be automatically selected considering the number of available CPUs.
    #[allow(unused_mut)]
    pub fn max_segment_size(self, value: u64) -> Self {
        let mut new = self;
        new.max_segment_size = Option::Some(Option::Some(value));
        new
    }
    ///
    /// Maximum size (in kilobytes) of vectors to store in-memory per segment.
    /// Segments larger than this threshold will be stored as read-only memmaped file.
    ///
    /// Memmap storage is disabled by default, to enable it, set this threshold to a reasonable value.
    ///
    /// To disable memmap storage, set this to `0`.
    ///
    /// Note: 1Kb = 1 vector of size 256
    #[allow(unused_mut)]
    pub fn memmap_threshold(self, value: u64) -> Self {
        let mut new = self;
        new.memmap_threshold = Option::Some(Option::Some(value));
        new
    }
    ///
    /// Maximum size (in kilobytes) of vectors allowed for plain index, exceeding this threshold will enable vector indexing
    ///
    /// Default value is 20,000, based on <<https://github.com/google-research/google-research/blob/master/scann/docs/algorithms.md>.>
    ///
    /// To disable vector indexing, set to `0`.
    ///
    /// Note: 1kB = 1 vector of size 256.
    #[allow(unused_mut)]
    pub fn indexing_threshold(self, value: u64) -> Self {
        let mut new = self;
        new.indexing_threshold = Option::Some(Option::Some(value));
        new
    }
    ///
    /// Interval between forced flushes.
    #[allow(unused_mut)]
    pub fn flush_interval_sec(self, value: u64) -> Self {
        let mut new = self;
        new.flush_interval_sec = Option::Some(Option::Some(value));
        new
    }
    ///
    /// Max number of threads (jobs) for running optimizations per shard.
    /// Each optimization job will also use `max_indexing_threads` threads by itself for index building.
    ///
    /// - If `auto` - have no limit and choose dynamically to saturate CPU.
    /// - If `disabled` or `0` - no optimization threads, optimizations will be disabled.
    ///
    /// ```no_run
    ///# use qdrant_client::{Qdrant, QdrantError};
    /// use qdrant_client::qdrant::{OptimizersConfigDiffBuilder, UpdateCollectionBuilder, MaxOptimizationThreadsBuilder};
    ///
    ///# async fn create_collection(client: &Qdrant)
    ///# -> Result<(), QdrantError> {
    /// let optimizers_config = OptimizersConfigDiffBuilder::default()
    ///     // Use exactly 8 threads
    ///     .max_optimization_threads(8)
    ///     // Or automatically choose
    ///     .max_optimization_threads(MaxOptimizationThreadsBuilder::auto())
    ///     // Or disable
    ///     .max_optimization_threads(MaxOptimizationThreadsBuilder::disabled());
    ///
    /// client
    ///     .update_collection(
    ///         UpdateCollectionBuilder::new("my_collection").optimizers_config(optimizers_config),
    ///     )
    ///     .await?;
    ///# Ok(())
    ///# }
    /// ```
    #[allow(unused_mut)]
    pub fn max_optimization_threads<VALUE: Into<MaxOptimizationThreads>>(
        self,
        value: VALUE,
    ) -> Self {
        let mut new = self;
        new.max_optimization_threads = Option::Some(Option::Some(value.into()));
        new
    }

    fn build_inner(self) -> Result<OptimizersConfigDiff, std::convert::Infallible> {
        Ok(OptimizersConfigDiff {
            deleted_threshold: self.deleted_threshold.unwrap_or_default(),
            vacuum_min_vector_number: self.vacuum_min_vector_number.unwrap_or_default(),
            default_segment_number: self.default_segment_number.unwrap_or_default(),
            max_segment_size: self.max_segment_size.unwrap_or_default(),
            memmap_threshold: self.memmap_threshold.unwrap_or_default(),
            indexing_threshold: self.indexing_threshold.unwrap_or_default(),
            flush_interval_sec: self.flush_interval_sec.unwrap_or_default(),
            max_optimization_threads: self.max_optimization_threads.unwrap_or_default(),
            // Deprecated: replaced with max_optimization_threads
            deprecated_max_optimization_threads: None,
        })
    }
    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
    fn create_empty() -> Self {
        Self {
            deleted_threshold: core::default::Default::default(),
            vacuum_min_vector_number: core::default::Default::default(),
            default_segment_number: core::default::Default::default(),
            max_segment_size: core::default::Default::default(),
            memmap_threshold: core::default::Default::default(),
            indexing_threshold: core::default::Default::default(),
            flush_interval_sec: core::default::Default::default(),
            max_optimization_threads: core::default::Default::default(),
        }
    }
}

impl From<OptimizersConfigDiffBuilder> for OptimizersConfigDiff {
    fn from(value: OptimizersConfigDiffBuilder) -> Self {
        value.build_inner().unwrap_or_else(|_| {
            panic!(
                "Failed to convert {0} to {1}",
                "OptimizersConfigDiffBuilder", "OptimizersConfigDiff"
            )
        })
    }
}

impl OptimizersConfigDiffBuilder {
    /// Builds the desired type. Can often be omitted.
    pub fn build(self) -> OptimizersConfigDiff {
        self.build_inner().unwrap_or_else(|_| {
            panic!(
                "Failed to build {0} into {1}",
                "OptimizersConfigDiffBuilder", "OptimizersConfigDiff"
            )
        })
    }
}

impl Default for OptimizersConfigDiffBuilder {
    fn default() -> Self {
        Self::create_empty()
    }
}