add and implement chunk_from_offset for IndexFile

Necessary for byte-wise seeking through chunks in an index.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
This commit is contained in:
Stefan Reiter 2020-07-22 15:56:21 +02:00 committed by Thomas Lamprecht
parent 2ff4c2cd5f
commit d0463b67ca
3 changed files with 32 additions and 0 deletions

View File

@ -216,6 +216,24 @@ impl IndexFile for DynamicIndexReader {
digest: self.index[pos].digest.clone(), digest: self.index[pos].digest.clone(),
}) })
} }
fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)> {
let end_idx = self.index.len() - 1;
let end = self.chunk_end(end_idx);
let found_idx = self.binary_search(0, 0, end_idx, end, offset);
let found_idx = match found_idx {
Ok(i) => i,
Err(_) => return None
};
let found_start = if found_idx == 0 {
0
} else {
self.chunk_end(found_idx - 1)
};
Some((found_idx, offset - found_start))
}
} }
struct CachedChunk { struct CachedChunk {

View File

@ -219,6 +219,17 @@ impl IndexFile for FixedIndexReader {
(csum, chunk_end) (csum, chunk_end)
} }
fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)> {
if offset >= self.size {
return None;
}
Some((
(offset / self.chunk_size as u64) as usize,
offset % self.chunk_size as u64
))
}
} }
pub struct FixedIndexWriter { pub struct FixedIndexWriter {

View File

@ -22,6 +22,9 @@ pub trait IndexFile {
fn index_bytes(&self) -> u64; fn index_bytes(&self) -> u64;
fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo>; fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo>;
/// Get the chunk index and the relative offset within it for a byte offset
fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)>;
/// Compute index checksum and size /// Compute index checksum and size
fn compute_csum(&self) -> ([u8; 32], u64); fn compute_csum(&self) -> ([u8; 32], u64);