Skip to main content

qdrant_client/builders/
product_quantization_builder.rs

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