add IndexFile trait

We want to be able to iterate through digests of index
files, but without always having to distinguish between
dynamic and fixed types, so add a trait we can use as a
trait object.

Unfortunately the iterator needs to yield copies as
iterators cannot yield values with life times when
represented as trait objects (Box<dyn Iterator<Item = ?>>)

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller
2019-02-27 14:32:34 +01:00
parent 5be4065baf
commit 7bc1d72778
4 changed files with 81 additions and 2 deletions

View File

@ -1,6 +1,7 @@
use failure::*;
use crate::tools;
use super::IndexFile;
use super::chunk_stat::*;
use super::chunk_store::*;
@ -132,14 +133,14 @@ impl FixedIndexReader {
if self.index == std::ptr::null_mut() { bail!("detected closed index file."); }
let index_count = (self.size + self.chunk_size - 1)/self.chunk_size;
let index_count = self.index_count();
status.used_bytes += index_count * self.chunk_size;
status.used_chunks += index_count;
for pos in 0..index_count {
let digest = unsafe { std::slice::from_raw_parts_mut(self.index.add(pos*32), 32) };
let digest = self.index_digest(pos).unwrap();
if let Err(err) = self.store.touch_chunk(digest) {
bail!("unable to access chunk {}, required by {:?} - {}",
tools::digest_to_hex(digest), self.filename, err);
@ -158,6 +159,20 @@ impl FixedIndexReader {
}
}
impl IndexFile for FixedIndexReader {
fn index_count(&self) -> usize {
(self.size + self.chunk_size - 1)/self.chunk_size
}
fn index_digest(&self, pos: usize) -> Option<&[u8; 32]> {
if pos >= self.index_count() {
None
} else {
Some(unsafe { std::mem::transmute(self.index.add(pos*32)) })
}
}
}
pub struct FixedIndexWriter {
store: Arc<ChunkStore>,
filename: PathBuf,