use crate::{vec::Vec, Result}; #[derive(Default)] pub struct MMRBatch> { memory_batch: Vec<(u64, Vec)>, store: Store, } impl> MMRBatch { pub fn new(store: Store) -> Self { MMRBatch { memory_batch: Vec::new(), store, } } pub fn append(&mut self, pos: u64, elems: Vec) { self.memory_batch.push((pos, elems)); } pub fn get_elem(&self, pos: u64) -> Result> { for (start_pos, elems) in self.memory_batch.iter().rev() { if pos < *start_pos { continue; } else if pos < start_pos + elems.len() as u64 { return Ok(elems.get((pos - start_pos) as usize).cloned()); } else { break; } } self.store.get_elem(pos) } pub fn commit(self) -> Result<()> { let Self { mut store, memory_batch, } = self; for (pos, elems) in memory_batch { store.append(pos, elems)?; } Ok(()) } } impl> IntoIterator for MMRBatch { type Item = (u64, Vec); type IntoIter = crate::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.memory_batch.into_iter() } } pub trait MMRStore { fn get_elem(&self, pos: u64) -> Result>; fn append(&mut self, pos: u64, elems: Vec) -> Result<()>; }