Skip to main content

SparseCache

Struct SparseCache 

Source
pub struct SparseCache { /* private fields */ }
Expand description

A tempfile-backed cache that tracks populated byte ranges.

Implementations§

Source§

impl SparseCache

Source

pub fn new(total_size: u64, spool_dir: &Path) -> Result<Self>

Create a new sparse cache for a file of the given total size.

Allocates an unlinked temp file in spool_dir (see crate::spool; the cache holds every byte read so far, so it must not land in a tmpfs) and sets its length so that sparse reads past the end don’t accidentally return 0 bytes before the caller has fetched them.

Source

pub fn missing_ranges(&self, offset: u64, size: u64) -> Vec<Range<u64>>

Returns the byte ranges within [offset, offset+size) that are NOT yet populated.

The returned ranges are sorted and clipped to [0, total_size). If the entire requested range is already populated, returns an empty Vec.

Source

pub fn write_at(&mut self, offset: u64, data: &[u8]) -> Result<()>

Write data at offset and mark [offset, offset+data.len()) as populated.

Source

pub fn read_at(&mut self, offset: u64, size: u64) -> Result<Vec<u8>>

Read size bytes at offset from the tempfile.

Callers must ensure the requested range is fully populated (check with missing_ranges and fill gaps via write_at). Reads past total_size return a short slice.

Trait Implementations§

Source§

impl Debug for SparseCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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, 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.