Skip to main content

qdrant_client/builders/
turbo_quantization_builder.rs

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