qdrant_client/builders/
scalar_quantization_builder.rs

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