forked from quickwit-oss/tantivy
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathheader.rs
More file actions
108 lines (91 loc) · 3.8 KB
/
Copy pathheader.rs
File metadata and controls
108 lines (91 loc) · 3.8 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Format version for the per-segment vector files (`.vec` and `.centroids`).
//!
//! A fixed 4-byte header (a `u32` version) is prepended to every file, ahead of
//! the [`CompositeFile`](crate::directory::CompositeFile) body. The version is
//! the wire-layout *generation* — bump it when the framing changes incompatibly.
//!
//! For `.vec`, the version is orthogonal to the
//! [`IdMap`](super::flat::id_map) variant, which selects the storage *mode*
//! (flat vs IVF) within a generation. For `.centroids`, it versions the IVF
//! routing composite (centroids, cluster offsets, optional graph).
use std::io::{self, Read, Write};
use common::{BinarySerializable, HasLen};
use crate::directory::FileSlice;
/// Length of the version header in bytes (a single `u32`).
pub(crate) const HEADER_LEN: usize = 4;
/// On-disk format version of a vector segment file (`.vec` or `.centroids`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum VectorFileVersion {
V1 = 1,
}
/// Version stamped into newly written vector files.
pub(crate) const CURRENT: VectorFileVersion = VectorFileVersion::V1;
impl BinarySerializable for VectorFileVersion {
fn serialize<W: Write + ?Sized>(&self, writer: &mut W) -> io::Result<()> {
(*self as u32).serialize(writer)
}
fn deserialize<R: Read>(reader: &mut R) -> io::Result<Self> {
match u32::deserialize(reader)? {
1 => Ok(VectorFileVersion::V1),
other => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("unsupported vector file format version: {other}"),
)),
}
}
}
/// Write the current version header. Call before wrapping the writer in a
/// [`CompositeWrite`](crate::directory::CompositeWrite); the composite's
/// offsets are self-relative, so the header does not perturb them.
pub(crate) fn write_header<W: Write + ?Sized>(writer: &mut W) -> io::Result<()> {
CURRENT.serialize(writer)
}
/// Parse the version header and return it alongside the composite body (the
/// file slice past the header). Errors if the version is unknown or newer than
/// [`CURRENT`].
pub(crate) fn read_header(file: &FileSlice) -> io::Result<(VectorFileVersion, FileSlice)> {
if file.len() < HEADER_LEN {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"vector file is smaller than its header",
));
}
let header_bytes = file.slice_to(HEADER_LEN).read_bytes()?;
let version = VectorFileVersion::deserialize(&mut header_bytes.as_slice())?;
Ok((version, file.slice_from(HEADER_LEN)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_header_round_trip() {
let mut buf = Vec::new();
write_header(&mut buf).unwrap();
assert_eq!(buf.len(), HEADER_LEN);
assert_eq!(buf, vec![1, 0, 0, 0]);
let (version, body) = read_header(&FileSlice::from(buf)).unwrap();
assert_eq!(version, VectorFileVersion::V1);
assert_eq!(body.len(), 0);
}
#[test]
fn test_header_preserves_body() {
let mut buf = Vec::new();
write_header(&mut buf).unwrap();
buf.extend_from_slice(b"composite-bytes");
let (version, body) = read_header(&FileSlice::from(buf)).unwrap();
assert_eq!(version, VectorFileVersion::V1);
assert_eq!(body.read_bytes().unwrap().as_slice(), b"composite-bytes");
}
#[test]
fn test_future_version_rejected() {
let buf = 2u32.to_le_bytes().to_vec();
let err = read_header(&FileSlice::from(buf)).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidData);
}
#[test]
fn test_truncated_header_rejected() {
let buf = vec![1u8, 0];
let err = read_header(&FileSlice::from(buf)).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::UnexpectedEof);
}
}