Skip to main content

qdrant_client/builders/
scalar_quantization_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct ScalarQuantizationBuilder {
6    /// Type of quantization
7    pub(crate) r#type: Option<i32>,
8    /// Number of bits to use for quantization
9    pub(crate) quantile: Option<Option<f32>>,
10    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
11    pub(crate) always_ram: Option<Option<bool>>,
12    /// Memory placement of quantized vectors.
13    pub(crate) memory: Option<Option<i32>>,
14}
15
16impl ScalarQuantizationBuilder {
17    /// Type of quantization
18    pub fn r#type(self, value: i32) -> Self {
19        let mut new = self;
20        new.r#type = Option::Some(value);
21        new
22    }
23    /// Number of bits to use for quantization
24    pub fn quantile(self, value: f32) -> Self {
25        let mut new = self;
26        new.quantile = Option::Some(Option::Some(value));
27        new
28    }
29    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
30    ///
31    /// Deprecated since 1.19.0, use [`memory`](Self::memory) instead.
32    pub fn always_ram(self, value: bool) -> Self {
33        let mut new = self;
34        new.always_ram = Option::Some(Option::Some(value));
35        new
36    }
37    /// Memory placement of quantized vectors.
38    /// Overrides the deprecated `always_ram` flag if both are set.
39    pub fn memory<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
40        let mut new = self;
41        new.memory = Option::Some(Option::Some(value.into()));
42        new
43    }
44
45    #[allow(deprecated)]
46    fn build_inner(self) -> Result<ScalarQuantization, ScalarQuantizationBuilderError> {
47        Ok(ScalarQuantization {
48            r#type: match self.r#type {
49                Some(value) => value,
50                None => {
51                    return Result::Err(core::convert::Into::into(
52                        ::derive_builder::UninitializedFieldError::from("r#type"),
53                    ));
54                }
55            },
56            quantile: self.quantile.unwrap_or_default(),
57            always_ram: self.always_ram.unwrap_or_default(),
58            memory: self.memory.unwrap_or_default(),
59        })
60    }
61    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
62    fn create_empty() -> Self {
63        Self {
64            r#type: core::default::Default::default(),
65            quantile: core::default::Default::default(),
66            always_ram: core::default::Default::default(),
67            memory: core::default::Default::default(),
68        }
69    }
70}
71
72impl From<ScalarQuantizationBuilder> for ScalarQuantization {
73    fn from(value: ScalarQuantizationBuilder) -> Self {
74        value.build_inner().unwrap_or_else(|_| {
75            panic!(
76                "Failed to convert {0} to {1}",
77                "ScalarQuantizationBuilder", "ScalarQuantization"
78            )
79        })
80    }
81}
82
83impl ScalarQuantizationBuilder {
84    /// Builds the desired type. Can often be omitted.
85    pub fn build(self) -> ScalarQuantization {
86        self.build_inner().unwrap_or_else(|_| {
87            panic!(
88                "Failed to build {0} into {1}",
89                "ScalarQuantizationBuilder", "ScalarQuantization"
90            )
91        })
92    }
93}
94
95impl ScalarQuantizationBuilder {
96    pub(crate) fn empty() -> Self {
97        Self::create_empty()
98    }
99}
100
101#[non_exhaustive]
102#[derive(Debug)]
103pub enum ScalarQuantizationBuilderError {
104    /// Uninitialized field
105    UninitializedField(&'static str),
106    /// Custom validation error
107    ValidationError(String),
108}
109
110// Implementing the Display trait for better error messages
111impl std::fmt::Display for ScalarQuantizationBuilderError {
112    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
113        match self {
114            Self::UninitializedField(field) => {
115                write!(f, "`{field}` must be initialized")
116            }
117            Self::ValidationError(error) => write!(f, "{error}"),
118        }
119    }
120}
121
122// Implementing the Error trait
123impl std::error::Error for ScalarQuantizationBuilderError {}
124
125// Implementing From trait for conversion from UninitializedFieldError
126impl From<derive_builder::UninitializedFieldError> for ScalarQuantizationBuilderError {
127    fn from(error: derive_builder::UninitializedFieldError) -> Self {
128        Self::UninitializedField(error.field_name())
129    }
130}
131
132// Implementing From trait for conversion from String
133impl From<String> for ScalarQuantizationBuilderError {
134    fn from(error: String) -> Self {
135        Self::ValidationError(error)
136    }
137}