define and use BufferedReader trait

This commit is contained in:
Dietmar Maurer 2019-01-05 17:28:20 +01:00
parent 1006acd626
commit 0a72e26704
2 changed files with 26 additions and 13 deletions

View File

@ -213,7 +213,23 @@ impl <'a> BufferedArchiveReader<'a> {
pub fn archive_size(&self) -> u64 { self.archive_size } pub fn archive_size(&self) -> u64 { self.archive_size }
pub fn read(&mut self, offset: u64) -> Result<&[u8], Error> { fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
let index = self.index;
let end = index.chunk_end(idx);
let digest = index.chunk_digest(idx);
index.store.read_chunk(digest, &mut self.read_buffer)?;
self.buffered_chunk_idx = idx;
self.buffered_chunk_start = end - (self.read_buffer.len() as u64);
//println!("BUFFER {} {}", self.buffered_chunk_start, end);
Ok(())
}
}
impl <'a> crate::tools::BufferedReader for BufferedArchiveReader<'a> {
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
let buffer_len = self.read_buffer.len(); let buffer_len = self.read_buffer.len();
let index = self.index; let index = self.index;
@ -246,18 +262,6 @@ impl <'a> BufferedArchiveReader<'a> {
Ok(&self.read_buffer[buffer_offset..]) Ok(&self.read_buffer[buffer_offset..])
} }
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
let index = self.index;
let end = index.chunk_end(idx);
let digest = index.chunk_digest(idx);
index.store.read_chunk(digest, &mut self.read_buffer)?;
self.buffered_chunk_idx = idx;
self.buffered_chunk_start = end - (self.read_buffer.len() as u64);
//println!("BUFFER {} {}", self.buffered_chunk_start, end);
Ok(())
}
} }
pub struct ArchiveIndexWriter<'a> { pub struct ArchiveIndexWriter<'a> {

View File

@ -17,6 +17,15 @@ use std::os::unix::io::AsRawFd;
pub mod timer; pub mod timer;
/// The `BufferedReader` trait provides a single function
/// `buffered_read`. It returns a reference to an internal buffer. The
/// purpose of this traid is to avoid unnecessary data copies.
pub trait BufferedReader {
/// This functions tries to fill the internal buffers, then returns
/// a reference to the available data.
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error>;
}
/// Directly map a type into a binary buffer. This is mostly useful /// Directly map a type into a binary buffer. This is mostly useful
/// for reading structured data from a byte stream (file). You need to /// for reading structured data from a byte stream (file). You need to
/// make sure that the buffer location does not change, so please /// make sure that the buffer location does not change, so please