Skip to main content

hermes_core/segment/
vector_data.rs

1//! Vector index data structures shared between builder and reader
2
3use serde::{Deserialize, Serialize};
4
5/// Flat vector data for brute-force search (accumulating state)
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct FlatVectorData {
8    pub dim: usize,
9    pub vectors: Vec<Vec<f32>>,
10    /// Document IDs with ordinals: (doc_id, ordinal) pairs
11    /// Ordinal tracks which vector in a multi-valued field
12    pub doc_ids: Vec<(u32, u16)>,
13}
14
15/// IVF-RaBitQ index data with embedded centroids and codebook
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct IVFRaBitQIndexData {
18    pub index: crate::structures::IVFRaBitQIndex,
19    pub centroids: crate::structures::CoarseCentroids,
20    pub codebook: crate::structures::RaBitQCodebook,
21}
22
23impl IVFRaBitQIndexData {
24    pub fn to_bytes(&self) -> std::io::Result<Vec<u8>> {
25        serde_json::to_vec(self)
26            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
27    }
28
29    pub fn from_bytes(data: &[u8]) -> std::io::Result<Self> {
30        serde_json::from_slice(data)
31            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
32    }
33}
34
35/// ScaNN index data with embedded centroids and codebook
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ScaNNIndexData {
38    pub index: crate::structures::IVFPQIndex,
39    pub centroids: crate::structures::CoarseCentroids,
40    pub codebook: crate::structures::PQCodebook,
41}
42
43impl ScaNNIndexData {
44    pub fn to_bytes(&self) -> std::io::Result<Vec<u8>> {
45        serde_json::to_vec(self)
46            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
47    }
48
49    pub fn from_bytes(data: &[u8]) -> std::io::Result<Self> {
50        serde_json::from_slice(data)
51            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
52    }
53}