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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#[cfg(test)]
use rand::{Rng, RngExt};
use std::fmt::{Debug, Formatter};
#[cfg(test)]
use strum::EnumCount;
use strum_macros::{Display, EnumCount};
/// The numeric datatype of the vector values.
#[derive(Clone, Copy, EnumCount, Display)]
#[repr(u8)]
pub enum VectorEncoding {
/**
* Encodes vector using 8 bits of precision per sample. Values provided
* with higher precision (e.g., queries provided as float) *must*
* be in the range [-128, 127]. NOTE: this can enable significant
* storage savings and faster searches, at the cost of some possible
* loss of precision.
*/
BYTE(usize),
/// Encodes vector using 32 bits of precision per sample in IEEE floating
/// point format.
FLOAT32(usize),
}
impl VectorEncoding {
/// The number of bytes required to encode a scalar in this format.
/// A vector will nominally require dimension * byteSize bytes of storage.
pub const fn byte_size(&self) -> usize {
match self {
VectorEncoding::BYTE(_) => 1,
VectorEncoding::FLOAT32(_) => 4,
}
}
pub const fn ordinal(&self) -> i32 {
match self {
VectorEncoding::BYTE(_) => 0,
VectorEncoding::FLOAT32(_) => 1,
}
}
pub const fn from_repr(repr: u8) -> Option<Self> {
match repr {
0 => Some(VectorEncoding::BYTE(1)),
1 => Some(VectorEncoding::FLOAT32(4)),
_ => None,
}
}
pub const fn values() -> &'static [VectorEncoding] {
&[VectorEncoding::BYTE(1), VectorEncoding::FLOAT32(4)]
}
#[cfg(test)]
pub fn random<R: Rng + ?Sized>(rng: &mut R) -> Self {
Self::values()[rng.random_range(0..Self::COUNT)]
}
}
impl Debug for VectorEncoding {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, formatter)
}
}
impl PartialEq for VectorEncoding {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(VectorEncoding::BYTE(_), VectorEncoding::BYTE(_))
| (VectorEncoding::FLOAT32(_), VectorEncoding::FLOAT32(_))
)
}
}
impl Eq for VectorEncoding {}
impl Default for VectorEncoding {
fn default() -> Self {
VectorEncoding::BYTE(1)
}
}