ChunkedEntry

Struct ChunkedEntry 

Source
pub struct ChunkedEntry {
    pub metadata: ChunkMetadata,
    pub chunks: Arc<DashMap<u64, Bytes>>,
    pub chunk_size: usize,
}
Expand description

Chunk-based cache entry for large files.

A ChunkedEntry splits a large file into fixed-size chunks, allowing efficient storage and retrieval of partial content. This is particularly useful for range requests where only a portion of the file is needed.

Fields§

§metadata: ChunkMetadata

Metadata about the full file

§chunks: Arc<DashMap<u64, Bytes>>

Map of chunk index to chunk data

§chunk_size: usize

Chunk size in bytes (default: 1MB)

Implementations§

Source§

impl ChunkedEntry

Source

pub fn new(metadata: ChunkMetadata, chunk_size: usize) -> Self

Creates a new chunked entry with the given metadata and chunk size.

§Arguments
  • metadata - File metadata including size, content type, and headers
  • chunk_size - Size of each chunk in bytes
§Examples
use tower_http_cache::chunks::{ChunkedEntry, ChunkMetadata};
use http::StatusCode;

let metadata = ChunkMetadata {
    total_size: 10_000_000,
    content_type: "video/mp4".to_string(),
    etag: None,
    last_modified: None,
    status: StatusCode::OK,
    version: http::Version::HTTP_11,
    headers: vec![],
};

let entry = ChunkedEntry::new(metadata, 1024 * 1024);
assert_eq!(entry.chunk_size, 1024 * 1024);
Source

pub fn add_chunk(&self, index: u64, data: Bytes)

Adds a chunk to the entry.

§Arguments
  • index - Chunk index (0-based)
  • data - Chunk data
§Examples
let entry = ChunkedEntry::new(metadata, 512);
entry.add_chunk(0, Bytes::from("Hello, world!"));
Source

pub fn get_chunk(&self, index: u64) -> Option<Bytes>

Gets a chunk by index.

Returns None if the chunk is not cached.

§Arguments
  • index - Chunk index (0-based)
§Examples
let entry = ChunkedEntry::new(metadata, 512);
entry.add_chunk(0, Bytes::from("Hello"));

let chunk = entry.get_chunk(0);
assert!(chunk.is_some());
assert_eq!(chunk.unwrap(), "Hello");
Source

pub fn get_range(&self, start: u64, end: u64) -> Option<Bytes>

Gets a byte range from the cached chunks.

Returns None if any required chunks are missing from the cache. The range is inclusive: [start, end].

§Arguments
  • start - Starting byte offset (inclusive)
  • end - Ending byte offset (inclusive)
§Examples
let entry = ChunkedEntry::new(metadata, 1024);
entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));
entry.add_chunk(1, Bytes::from(vec![1u8; 1024]));

// Get first 512 bytes
let range = entry.get_range(0, 511);
assert!(range.is_some());
assert_eq!(range.unwrap().len(), 512);
Source

pub fn is_complete(&self) -> bool

Checks if all chunks are cached.

Returns true if the entry contains all chunks needed to represent the full file.

§Examples
let entry = ChunkedEntry::new(metadata, 1024);
assert!(!entry.is_complete());

entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));
entry.add_chunk(1, Bytes::from(vec![1u8; 1024]));
assert!(entry.is_complete());
Source

pub fn coverage(&self) -> f64

Gets the cache coverage percentage.

Returns a percentage (0-100) indicating how much of the file is cached.

§Examples
let entry = ChunkedEntry::new(metadata, 1024);
assert_eq!(entry.coverage(), 0.0);

entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));
assert_eq!(entry.coverage(), 25.0);

entry.add_chunk(1, Bytes::from(vec![1u8; 1024]));
assert_eq!(entry.coverage(), 50.0);

Trait Implementations§

Source§

impl Clone for ChunkedEntry

Source§

fn clone(&self) -> ChunkedEntry

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChunkedEntry

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.