pub struct ChunkSize { /* private fields */ }Expand description
Value object representing a chunk size with validation
This struct provides a type-safe representation of chunk sizes used throughout the adaptive pipeline system. It ensures chunk sizes are within valid bounds and provides convenient methods for working with chunk sizes.
§Key Features
- Validation: Ensures chunk sizes are within acceptable bounds (1 byte to 512MB)
- Type Safety: Prevents invalid chunk sizes at compile time
- Immutability: Once created, chunk sizes cannot be modified
- Serialization: Full JSON and binary serialization support
- Performance: Optimized for frequent use in processing pipelines
§Constraints
- Minimum Size: 1 byte (prevents degenerate cases)
- Maximum Size: 512MB (prevents memory exhaustion)
- Default Size: 1MB (balanced for most use cases)
§Examples
Implementations§
Source§impl ChunkSize
impl ChunkSize
Sourcepub const MIN_SIZE: usize = 1usize
pub const MIN_SIZE: usize = 1usize
Minimum chunk size (1 byte) - must be at least 1 byte for processing
Sourcepub const MAX_SIZE: usize = 536_870_912usize
pub const MAX_SIZE: usize = 536_870_912usize
Maximum chunk size (512MB) - prevents memory exhaustion
Sourcepub const DEFAULT_SIZE: usize = 1_048_576usize
pub const DEFAULT_SIZE: usize = 1_048_576usize
Default chunk size (1MB)
Sourcepub fn new(bytes: usize) -> Result<Self, PipelineError>
pub fn new(bytes: usize) -> Result<Self, PipelineError>
Creates a new chunk size with validation
Validates that the specified size is within acceptable bounds before creating the chunk size instance.
§Arguments
bytes- Size in bytes (must be between 1 byte and 512MB)
§Returns
Ok(ChunkSize)- Valid chunk sizeErr(PipelineError::InvalidConfiguration)- If size is out of bounds
§Errors
Returns an error if:
- Size is less than
MIN_SIZE(1 byte) - Size exceeds
MAX_SIZE(512MB)
§Examples
Sourcepub fn from_kb(kb: usize) -> Result<Self, PipelineError>
pub fn from_kb(kb: usize) -> Result<Self, PipelineError>
Sourcepub fn from_mb(mb: usize) -> Result<Self, PipelineError>
pub fn from_mb(mb: usize) -> Result<Self, PipelineError>
Sourcepub fn as_bytes(&self) -> usize
pub fn as_bytes(&self) -> usize
Gets the size in bytes (alias for test framework compatibility)
Sourcepub fn optimal_for_file_size(file_size: u64) -> Self
pub fn optimal_for_file_size(file_size: u64) -> Self
Calculates the optimal chunk size based on file size
This method implements an empirically-optimized strategy based on comprehensive benchmark results across file sizes from 5MB to 2GB.
§Empirical Optimization Results
- 100MB files: 16MB chunks optimal (vs 2MB adaptive = +43.7% performance)
- 500MB files: 16MB chunks optimal (vs 4MB adaptive = +56.2% performance)
- 2GB files: 128MB chunks optimal (current algorithm validated)
- Small files: Current algorithm performing reasonably well
Sourcepub fn chunks_needed_for_file(&self, file_size: u64) -> u64
pub fn chunks_needed_for_file(&self, file_size: u64) -> u64
Calculates the number of chunks needed for a given file size
Sourcepub fn is_optimal_for_file(&self, file_size: u64) -> bool
pub fn is_optimal_for_file(&self, file_size: u64) -> bool
Checks if this chunk size is optimal for the given file size
Sourcepub fn adjust_for_memory(
&self,
available_memory: usize,
max_parallel_chunks: usize,
) -> Result<Self, PipelineError>
pub fn adjust_for_memory( &self, available_memory: usize, max_parallel_chunks: usize, ) -> Result<Self, PipelineError>
Adjusts the chunk size based on available memory
Sourcepub fn validate_user_input(
user_chunk_size_mb: usize,
file_size: u64,
) -> Result<usize, String>
pub fn validate_user_input( user_chunk_size_mb: usize, file_size: u64, ) -> Result<usize, String>
Validates user-provided chunk size input with sanity checks Returns validated chunk size in bytes or error message
Sourcepub fn strategy_description(file_size: u64) -> &'static str
pub fn strategy_description(file_size: u64) -> &'static str
Returns a description of the chunk size strategy for the given file size
Trait Implementations§
Source§impl<'de> Deserialize<'de> for ChunkSize
impl<'de> Deserialize<'de> for ChunkSize
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Ord for ChunkSize
impl Ord for ChunkSize
Source§impl PartialOrd for ChunkSize
impl PartialOrd for ChunkSize
impl Copy for ChunkSize
impl Eq for ChunkSize
impl StructuralPartialEq for ChunkSize
Auto Trait Implementations§
impl Freeze for ChunkSize
impl RefUnwindSafe for ChunkSize
impl Send for ChunkSize
impl Sync for ChunkSize
impl Unpin for ChunkSize
impl UnwindSafe for ChunkSize
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more