weavatrix_search_vector/quantized_io/
mod.rs1mod decoder;
2mod encoder;
3mod format;
4mod storage;
5
6use crate::error::SearchError;
7use crate::quantized::QuantizedIndex;
8use std::io::{Read, Write};
9use std::path::Path;
10
11pub(crate) use decoder::decode;
12pub(crate) use encoder::encode;
13
14pub(super) const MAGIC: &[u8; 8] = b"WVQNT003";
15pub(super) const VERSION: u32 = 1;
16pub(super) const HEADER_LEN: usize = 128;
17pub(super) const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
18pub(super) const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
19
20impl QuantizedIndex {
21 pub fn to_bytes(&self) -> Result<Vec<u8>, SearchError> {
27 encode(self)
28 }
29
30 pub fn from_bytes(bytes: &[u8]) -> Result<Self, SearchError> {
36 decode(bytes)
37 }
38
39 pub fn write_to(&self, mut writer: impl Write) -> Result<(), SearchError> {
45 writer
46 .write_all(&self.to_bytes()?)
47 .map_err(|error| SearchError::storage("write quantized snapshot", &error))
48 }
49
50 pub fn read_from(mut reader: impl Read) -> Result<Self, SearchError> {
56 let mut bytes = Vec::new();
57 reader
58 .read_to_end(&mut bytes)
59 .map_err(|error| SearchError::storage("read quantized snapshot", &error))?;
60 Self::from_bytes(&bytes)
61 }
62
63 pub fn save(&self, path: impl AsRef<Path>) -> Result<(), SearchError> {
69 crate::atomic_file::atomic_write(path.as_ref(), &self.to_bytes()?)
70 }
71
72 pub fn load(path: impl AsRef<Path>) -> Result<Self, SearchError> {
78 let bytes = std::fs::read(path.as_ref())
79 .map_err(|error| SearchError::storage("read quantized snapshot", &error))?;
80 Self::from_bytes(&bytes)
81 }
82}