Skip to main content

BlockMatcher

Struct BlockMatcher 

Source
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

Source

pub fn new(control: &ControlFile) -> Self

Indexes the block checksums in control, with every block unknown.

Source

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.

Source

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.

Source

pub fn needed_block_ranges(&self) -> Vec<(usize, usize)>

The runs of blocks still unknown, as half-open (first, end) ranges in ascending order.

Source

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.

Source

pub fn blocks_todo(&self) -> usize

The number of blocks still unknown.

Source

pub fn is_complete(&self) -> bool

Whether every block is known.

Source

pub fn total_blocks(&self) -> usize

The number of blocks in the target.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.