use crate::codec::{CompressedBlock, ExactFallbackCodec, KVecCodec};
use crate::error::Result;
pub struct ExactFallbackStore {
codec: ExactFallbackCodec,
}
impl ExactFallbackStore {
pub fn new(head_dim: usize) -> Self {
Self {
codec: ExactFallbackCodec::new(head_dim),
}
}
pub fn encode_block(&self, vector: &[f32], _seed: u64) -> Result<CompressedBlock> {
let encoded = self.codec.encode(vector, 0)?;
Ok(CompressedBlock::new(
self.codec.codec_id(),
encoded,
vector.len(),
))
}
pub fn decode_block(&self, block: &CompressedBlock) -> Result<Vec<f32>> {
self.codec.decode(&block.encoded_payload, 0)
}
pub fn verify_roundtrip(&self, original: &[f32], _seed: u64) -> Result<bool> {
let decoded = self.decode_block(&self.encode_block(original, 0)?)?;
Ok(original
.iter()
.zip(decoded.iter())
.all(|(a, b)| (a - b).abs() < 1e-6))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exact_fallback_roundtrip() {
let store = ExactFallbackStore::new(8);
let vec: Vec<f32> = vec![0.5, -0.25, 0.75, 1.0, -1.0, 0.1, 0.9, -0.5];
let block = store.encode_block(&vec, 0).unwrap();
let decoded = store.decode_block(&block).unwrap();
assert_eq!(vec, decoded);
}
#[test]
fn test_exact_fallback_dimension_mismatch() {
let store = ExactFallbackStore::new(8);
let vec: Vec<f32> = vec![0.1, 0.2, 0.3]; let result = store.encode_block(&vec, 0);
assert!(result.is_err());
}
}