use std::path::{Path, PathBuf};
use crate::error::{StorageError, io_err};
use crate::real_fs::RealFs;
use crate::vfs::Vfs;
use super::ledger::{complete_lines, is_segment};
pub(crate) struct SegmentBytes {
bytes: Vec<u8>,
}
impl SegmentBytes {
pub(crate) fn lines(&self) -> impl Iterator<Item = &[u8]> {
complete_lines(&self.bytes)
.0
.into_iter()
.filter(|line| !line.is_empty())
}
}
pub(crate) fn read_segment(segment: &Path) -> Result<SegmentBytes, StorageError> {
RealFs::new()
.read(segment)
.map(|bytes| SegmentBytes { bytes })
.map_err(io_err("read segment", segment))
}
pub fn read_raw_lines_at(dir: &Path) -> Result<Vec<Vec<u8>>, StorageError> {
let mut out = Vec::new();
for segment in ledger_segments_at(dir)? {
out.extend(read_segment(&segment)?.lines().map(<[u8]>::to_vec));
}
Ok(out)
}
pub fn ledger_segments_at(dir: &Path) -> Result<Vec<PathBuf>, StorageError> {
let vfs = RealFs::new();
Ok(vfs
.list(dir)
.map_err(io_err("list ledger dir", dir))?
.into_iter()
.filter(|p| is_segment(p))
.collect())
}