navid/veins / e362a182625e66b06bd48dcda2992ca49b89e9b8638e55b72b043f1e7ea705b1 / veins-rust/src/store/index.rs
index.rs
← veins-rust/src/store · 3461 bytes · raw · download
use crate::encoding::Reader;
use crate::error::{Error, Result};
use crate::object::id::{hash_bytes, Id};
#[derive(Clone)]
pub struct IndexEntry {
pub id: Id,
pub offset: u64,
}
#[derive(Clone)]
pub struct Index {
pub entries: Vec<IndexEntry>,
pub pack_trailer: [u8; 32],
}
impl Index {
pub fn find(&self, id: Id) -> Option<usize> {
self.entries
.binary_search_by(|entry| entry.id.0.cmp(&id.0))
.ok()
}
}
pub fn decode(data: &[u8], expected_pack_trailer: Option<[u8; 32]>) -> Result<Index> {
const BASE: usize = 1032 + 64;
if data.len() < BASE {
return Err(Error::new("index is truncated"));
}
if &data[..4] != b"VIX1" {
return Err(Error::new("invalid index magic"));
}
let mut reader = Reader::new(data);
reader.raw(4)?;
let version = reader.u32le()?;
if version != 1 {
return Err(Error::new("unsupported index version"));
}
let mut fanout = [0u32; 256];
let mut previous = 0u32;
for slot in &mut fanout {
let value = reader.u32le()?;
if value < previous {
return Err(Error::new("index fanout is not sorted"));
}
previous = value;
*slot = value;
}
let body = data.len() - BASE;
if !body.is_multiple_of(40) {
return Err(Error::new("index body has an invalid length"));
}
let count = body / 40;
if count != fanout[255] as usize {
return Err(Error::new("index fanout does not match entry count"));
}
let mut entries: Vec<IndexEntry> = Vec::with_capacity(count);
for index in 0..count {
let id = reader.id()?;
if index > 0 && entries[index - 1].id.0 >= id.0 {
return Err(Error::new("index ids are not sorted"));
}
entries.push(IndexEntry { id, offset: 0 });
}
for entry in &mut entries {
entry.offset = reader.u64le()?;
}
let mut pack_trailer = [0u8; 32];
pack_trailer.copy_from_slice(reader.raw(32)?);
let trailer_offset = data.len() - 32;
if hash_bytes(&data[..trailer_offset])[..] != data[trailer_offset..] {
return Err(Error::new("index checksum mismatch"));
}
if expected_pack_trailer.is_some_and(|value| value != pack_trailer) {
return Err(Error::new("index pack trailer mismatch"));
}
let mut counts = [0u32; 256];
for entry in &entries {
counts[entry.id.0[0] as usize] += 1;
}
let mut running = 0u32;
for index in 0..256 {
running += counts[index];
if running != fanout[index] {
return Err(Error::new("index fanout is inconsistent"));
}
}
Ok(Index {
entries,
pack_trailer,
})
}
pub fn encode(entries: &[IndexEntry], pack_trailer: [u8; 32]) -> Vec<u8> {
use crate::encoding::Writer;
let mut counts = [0u32; 256];
for entry in entries {
counts[entry.id.0[0] as usize] += 1;
}
let mut writer = Writer::with_capacity(entries.len() * 40 + 128);
writer.raw(b"VIX1");
writer.u32le(1);
let mut running = 0u32;
for count in counts {
running += count;
writer.u32le(running);
}
for entry in entries {
writer.id(entry.id);
}
for entry in entries {
writer.u64le(entry.offset);
}
writer.raw(&pack_trailer);
let mut data = writer.into_vec();
let digest = hash_bytes(&data);
data.extend_from_slice(&digest);
data
}