pub struct BlockMatcher { /* private fields */ }Expand description
The index of a target’s blocks and the record of which are known.
Every block starts unknown. A block becomes known when a scan finds it in local data or when downloaded data verifies against its checksum, and the blocks still unknown are what remains to download.
Implementations§
Source§impl BlockMatcher
impl BlockMatcher
Sourcepub fn new(control: &ControlFile) -> Self
pub fn new(control: &ControlFile) -> Self
Indexes the block checksums in control, with every block unknown.
Sourcepub fn submit_blocks(
&mut self,
data: &[u8],
block_start: usize,
) -> Result<bool, MatchError>
pub fn submit_blocks( &mut self, data: &[u8], block_start: usize, ) -> Result<bool, MatchError>
Verifies data as consecutive whole blocks starting at block
block_start, marking each one that matches its checksum as known.
A final block shorter than the block size must be padded with zeros
to a full block, as the checksums were computed that way. Trailing
bytes short of a block are ignored, as are blocks past the end of the
target. Verification stops at the first mismatch and returns false,
leaving the blocks before it known.
§Errors
None at present. The Result leaves room for one.
Sourcepub fn submit_source_data(
&mut self,
data: &[u8],
offset: u64,
) -> Vec<(usize, usize)>
pub fn submit_source_data( &mut self, data: &[u8], offset: u64, ) -> Vec<(usize, usize)>
Scans data for target blocks at every byte offset and marks the ones
found as known.
offset is where data starts in its source, and the returned
(block_id, source_offset) pairs give absolute offsets in that
source. Offsets are tested only up to blocksize * seq_matches bytes
before the end of data, so a caller scanning a file in chunks must
overlap them by that much and pad the end of the file with zeros.
Each block is reported at most once, at the first offset found. Found blocks leave the index, so a later scan does not report them again.
Sourcepub fn needed_block_ranges(&self) -> Vec<(usize, usize)>
pub fn needed_block_ranges(&self) -> Vec<(usize, usize)>
The runs of blocks still unknown, as half-open (first, end) ranges
in ascending order.
Sourcepub fn is_block_known(&self, block_id: usize) -> bool
pub fn is_block_known(&self, block_id: usize) -> bool
Whether block block_id is known, which is false past the end of the
target.
Sourcepub fn blocks_todo(&self) -> usize
pub fn blocks_todo(&self) -> usize
The number of blocks still unknown.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Whether every block is known.
Sourcepub fn total_blocks(&self) -> usize
pub fn total_blocks(&self) -> usize
The number of blocks in the target.