qdrant_client/builders/
product_quantization_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
use crate::qdrant::*;

pub struct ProductQuantizationBuilder {
    /// Compression ratio
    pub(crate) compression: Option<i32>,
    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
    pub(crate) always_ram: Option<Option<bool>>,
}

impl ProductQuantizationBuilder {
    /// Compression ratio
    #[allow(unused_mut)]
    pub fn compression(self, value: i32) -> Self {
        let mut new = self;
        new.compression = Option::Some(value);
        new
    }
    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
    #[allow(unused_mut)]
    pub fn always_ram(self, value: bool) -> Self {
        let mut new = self;
        new.always_ram = Option::Some(Option::Some(value));
        new
    }

    fn build_inner(self) -> Result<ProductQuantization, ProductQuantizationBuilderError> {
        Ok(ProductQuantization {
            compression: match self.compression {
                Some(value) => value,
                None => {
                    return Result::Err(core::convert::Into::into(
                        ::derive_builder::UninitializedFieldError::from("compression"),
                    ));
                }
            },
            always_ram: self.always_ram.unwrap_or_default(),
        })
    }
    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
    fn create_empty() -> Self {
        Self {
            compression: core::default::Default::default(),
            always_ram: core::default::Default::default(),
        }
    }
}

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

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

impl ProductQuantizationBuilder {
    pub(crate) fn empty() -> Self {
        Self::create_empty()
    }
}

/// Error type for ProductQuantizationBuilder
#[non_exhaustive]
#[derive(Debug)]
pub enum ProductQuantizationBuilderError {
    /// Uninitialized field
    UninitializedField(&'static str),
    /// Custom validation error
    ValidationError(String),
}

// Implementing the Display trait for better error messages
impl std::fmt::Display for ProductQuantizationBuilderError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::UninitializedField(field) => {
                write!(f, "`{}` must be initialized", field)
            }
            Self::ValidationError(error) => write!(f, "{}", error),
        }
    }
}

// Implementing the Error trait
impl std::error::Error for ProductQuantizationBuilderError {}

// Implementing From trait for conversion from UninitializedFieldError
impl From<derive_builder::UninitializedFieldError> for ProductQuantizationBuilderError {
    fn from(error: derive_builder::UninitializedFieldError) -> Self {
        Self::UninitializedField(error.field_name())
    }
}

// Implementing From trait for conversion from String
impl From<String> for ProductQuantizationBuilderError {
    fn from(error: String) -> Self {
        Self::ValidationError(error)
    }
}