Skip to main content

weavatrix_search_vector/quantized_io/
mod.rs

1mod 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    /// Serializes the complete compact index into a checksummed buffer.
22    ///
23    /// # Errors
24    ///
25    /// Returns a capacity or allocation error.
26    pub fn to_bytes(&self) -> Result<Vec<u8>, SearchError> {
27        encode(self)
28    }
29
30    /// Restores and fully validates a compact index from a buffer.
31    ///
32    /// # Errors
33    ///
34    /// Returns a typed format, integrity, configuration, or allocation error.
35    pub fn from_bytes(bytes: &[u8]) -> Result<Self, SearchError> {
36        decode(bytes)
37    }
38
39    /// Writes the checksummed compact representation to a stream.
40    ///
41    /// # Errors
42    ///
43    /// Returns a serialization or stream error.
44    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    /// Reads and validates a compact representation from a stream.
51    ///
52    /// # Errors
53    ///
54    /// Returns a stream, format, integrity, or allocation error.
55    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    /// Atomically saves a checksummed compact snapshot.
64    ///
65    /// # Errors
66    ///
67    /// Returns a serialization or filesystem error.
68    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    /// Loads and validates a compact snapshot.
73    ///
74    /// # Errors
75    ///
76    /// Returns a filesystem, format, integrity, or allocation error.
77    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}