backup/chunk_store.rs: avoid allocation in read_chunk

This commit is contained in:
Dietmar Maurer 2019-01-04 17:16:56 +01:00
parent 7394ca3e95
commit df9973e8f5
2 changed files with 18 additions and 7 deletions

View File

@ -132,13 +132,15 @@ impl <'a> ArchiveIndexReader<'a> {
pub fn dump_catar(&self, mut writer: Box<Write>) -> Result<(), Error> {
let mut buffer = Vec::with_capacity(1024*1024);
for pos in 0..self.index_entries {
let offset = unsafe { *(self.index.add(pos*40) as *const u64) };
let digest = unsafe { std::slice::from_raw_parts(self.index.add(pos*40+8), 32) };
let chunk = self.store.read_chunk(digest)?;
println!("Dump {:08x} {}", offset, chunk.len());
writer.write_all(&chunk)?;
self.store.read_chunk(digest, &mut buffer)?;
println!("Dump {:08x} {}", offset, buffer.len(), );
writer.write_all(&buffer)?;
}

View File

@ -165,7 +165,7 @@ impl ChunkStore {
Ok(())
}
pub fn read_chunk(&self, digest:&[u8]) -> Result<Vec<u8>, Error> {
pub fn read_chunk(&self, digest:&[u8], buffer: &mut Vec<u8>) -> Result<(), Error> {
let mut chunk_path = self.chunk_dir.clone();
let prefix = digest_to_prefix(&digest);
@ -174,13 +174,22 @@ impl ChunkStore {
chunk_path.push(&digest_str);
let mut f = std::fs::File::open(&chunk_path)?;
let mut chunk = Vec::with_capacity(64*1024);
let stat = nix::sys::stat::fstat(f.as_raw_fd())?;
let size = stat.st_size as usize;
unsafe { buffer.set_len(buffer.capacity()); }
if buffer.len() < size {
let additional = size - buffer.len();
buffer.reserve(additional);
}
unsafe { buffer.set_len(size); }
use std::io::Read;
f.read_to_end(&mut chunk)?;
f.read_exact(buffer.as_mut_slice())?;
Ok(chunk)
Ok(())
}
fn sweep_old_files(&self, handle: &mut nix::dir::Dir, status: &mut GarbageCollectionStatus) -> Result<(), Error> {