use std::io;
use bao_tree::io::sync::WriteAt;
use crate::store::IROH_BLOCK_SIZE;
#[derive(Debug, Default)]
pub struct SizeInfo {
pub offset: u64,
pub size: u64,
}
#[allow(dead_code)]
impl SizeInfo {
pub(crate) fn complete(size: u64) -> Self {
let mask = (1 << IROH_BLOCK_SIZE.chunk_log()) - 1;
let last_chunk_offset = size & mask;
Self {
offset: last_chunk_offset,
size,
}
}
pub fn write(&mut self, offset: u64, size: u64) {
if offset >= self.offset {
self.offset = offset;
self.size = size;
}
}
pub fn current_size(&self) -> u64 {
self.size
}
pub fn persist(&self, mut target: impl WriteAt) -> io::Result<()> {
let size_offset = (self.offset >> IROH_BLOCK_SIZE.chunk_log()) << 3;
target.write_all_at(size_offset, self.size.to_le_bytes().as_slice())?;
Ok(())
}
#[allow(dead_code)]
pub fn to_vec(&self) -> Vec<u8> {
let mut res = Vec::new();
self.persist(&mut res).expect("io error writing to vec");
res
}
}