weavatrix_search_vector/storage/
index_io.rs1use super::decoder::decode_snapshot;
2use super::writer::{write_snapshot, write_snapshot_stream};
3use super::{FORMAT_VERSION, HEADER_LEN, Header, MappedVectorIndex, SnapshotMetadata};
4use crate::error::SearchError;
5use crate::hnsw::VectorIndex;
6use std::io::{Cursor, Read, Seek, Write};
7use std::path::Path;
8
9impl VectorIndex {
10 pub fn save(&self, path: impl AsRef<Path>) -> Result<(), SearchError> {
19 write_snapshot(self, path.as_ref())
20 }
21
22 pub fn load(path: impl AsRef<Path>) -> Result<Self, SearchError> {
29 MappedVectorIndex::open(path)?.to_owned()
30 }
31
32 pub fn to_bytes(&self) -> Result<Vec<u8>, SearchError> {
38 let mut cursor = Cursor::new(Vec::new());
39 write_snapshot_stream(self, &mut cursor)?;
40 Ok(cursor.into_inner())
41 }
42
43 pub fn from_bytes(bytes: &[u8]) -> Result<Self, SearchError> {
49 decode_snapshot(bytes)
50 }
51
52 pub fn write_to<W: Write + Seek>(&self, writer: &mut W) -> Result<(), SearchError> {
60 write_snapshot_stream(self, writer)
61 }
62
63 pub fn read_from<R: Read>(reader: &mut R) -> Result<Self, SearchError> {
70 let mut bytes = Vec::new();
71 reader
72 .read_to_end(&mut bytes)
73 .map_err(|error| SearchError::storage("read snapshot stream", &error))?;
74 Self::from_bytes(&bytes)
75 }
76
77 pub fn read_metadata(path: impl AsRef<Path>) -> Result<SnapshotMetadata, SearchError> {
83 let mut file = std::fs::File::open(path)
84 .map_err(|error| SearchError::storage("open snapshot metadata", &error))?;
85 let mut header = [0_u8; HEADER_LEN];
86 file.read_exact(&mut header)
87 .map_err(|error| SearchError::storage("read snapshot metadata", &error))?;
88 SnapshotMetadata::from_bytes(&header)
89 }
90}
91
92impl SnapshotMetadata {
93 pub fn from_bytes(bytes: &[u8]) -> Result<Self, SearchError> {
99 let header = Header::parse(bytes)?;
100 Ok(Self {
101 format_version: FORMAT_VERSION,
102 vector_count: header.count,
103 serialized_bytes: header.file_len,
104 config: header.config,
105 })
106 }
107}