use std::collections::HashMap;
use crate::imp::core::Bytes32;
#[derive(Debug, Default)]
pub struct ChunkIndex {
map: HashMap<Bytes32, u32>,
bodies: Vec<Vec<u8>>,
}
impl ChunkIndex {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, hash: Bytes32, body: Vec<u8>) -> u32 {
if let Some(&i) = self.map.get(&hash) {
return i;
}
let i = self.bodies.len() as u32;
self.map.insert(hash, i);
self.bodies.push(body);
i
}
pub fn index_of(&self, hash: &Bytes32) -> Option<u32> {
self.map.get(hash).copied()
}
pub fn len(&self) -> usize {
self.bodies.len()
}
pub fn is_empty(&self) -> bool {
self.bodies.is_empty()
}
pub fn bodies_in_order(&self) -> impl Iterator<Item = &[u8]> {
self.bodies.iter().map(|b| b.as_slice())
}
}