FileChunk

Struct FileChunk 

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

Represents an immutable chunk of file data for processing

This is a Value Object in Domain-Driven Design terms - it represents data without identity that cannot be modified once created. Any “changes” create new instances, ensuring data integrity and preventing accidental mutations during processing.

§Key Features

  • Immutability: Once created, chunks cannot be modified
  • Unique Identity: Each chunk has a UUID for tracking and identification
  • Sequence Ordering: Maintains sequence numbers for proper file reassembly
  • Integrity Verification: Optional checksums for data integrity validation
  • Metadata Tracking: Creation timestamps and processing metadata

§Design Principles

  • Value Object: Compared by value, not identity
  • Self-Validation: Validates its own data integrity
  • Builder Pattern: Use methods like with_checksum() for modifications
  • Thread Safety: Fully thread-safe due to immutability

§Examples

§Developer Notes

  • Use builder methods like with_checksum() to create modified versions
  • Processing stages should create new chunks rather than modifying existing ones
  • This design prevents data corruption and ensures thread safety

Implementations§

Source§

impl FileChunk

Source

pub fn new( sequence_number: u64, offset: u64, data: Vec<u8>, is_final: bool, ) -> Result<Self, PipelineError>

Creates a new file chunk

§Purpose

Creates an immutable file chunk value object for pipeline processing. Chunks are the fundamental unit of file processing in the adaptive pipeline.

§Why

File chunking enables:

  • Parallel processing of large files
  • Memory-efficient streaming
  • Independent processing units
  • Granular error recovery
§Arguments
  • sequence_number - The order of this chunk in the file (0-based)
  • offset - Byte offset in the original file where this chunk starts
  • data - The actual chunk data bytes (must not be empty)
  • is_final - Whether this is the last chunk in the file
§Returns
  • Ok(FileChunk) - Successfully created chunk with unique UUID
  • Err(PipelineError::InvalidChunk) - Data is empty
§Errors

Returns PipelineError::InvalidChunk when data is empty.

§Side Effects
  • Generates new UUID for chunk identification
  • Sets creation timestamp to current UTC time
  • Calculates chunk size from data length
§Examples
§Developer Notes
  • Each chunk gets a unique UUID for tracking across pipeline stages
  • Chunk size is automatically validated against system limits
  • Checksum is initially None - use with_calculated_checksum() to add
  • This is a Value Object - create new instances for “changes”
Source

pub fn new_with_checksum( sequence_number: u64, offset: u64, data: Vec<u8>, checksum: String, is_final: bool, ) -> Result<Self, PipelineError>

Creates a new file chunk with checksum

§Developer Notes
  • This is a convenience constructor for chunks that already have checksums
  • Prefer using new() followed by with_checksum() for clarity
Source

pub fn id(&self) -> Uuid

Gets the chunk ID

Source

pub fn sequence_number(&self) -> u64

Gets the sequence number

Source

pub fn offset(&self) -> u64

Gets the offset in the original file

Source

pub fn size(&self) -> &ChunkSize

Gets the chunk size

Source

pub fn data(&self) -> &[u8]

Gets the chunk data (immutable reference)

Source

pub fn checksum(&self) -> Option<&str>

Gets the checksum if available

Source

pub fn is_final(&self) -> bool

Checks if this is the final chunk

Source

pub fn created_at(&self) -> DateTime<Utc>

Gets the creation timestamp

Source

pub fn data_len(&self) -> usize

Gets the actual data length

Source

pub fn is_empty(&self) -> bool

Checks if the chunk is empty

Source

pub fn with_data(&self, data: Vec<u8>) -> Result<Self, PipelineError>

Creates a new FileChunk with updated data

§Developer Notes
  • This creates a completely new chunk instance
  • The old chunk remains unchanged (immutability)
  • Checksum is cleared since data changed
  • Use this pattern: let new_chunk = old_chunk.with_data(new_data).unwrap();
Source

pub fn with_checksum(&self, checksum: String) -> Self

Creates a new FileChunk with a checksum

§Developer Notes
  • This preserves all other data and adds/updates the checksum
  • Use this after processing: let verified_chunk = chunk.with_checksum(hash);
Source

pub fn with_calculated_checksum(&self) -> Result<Self, PipelineError>

Creates a new FileChunk with calculated SHA-256 checksum

§Developer Notes
  • Calculates SHA-256 hash of current data
  • Returns new chunk with checksum set
  • Original chunk remains unchanged
Source

pub fn without_data(&self) -> Self

Creates a new FileChunk without data (for security)

§Developer Notes
  • Creates new chunk with empty data vector
  • Useful for secure cleanup while preserving metadata
  • Checksum is cleared since data is gone
Source

pub fn verify_integrity(&self) -> Result<bool, PipelineError>

Verifies the chunk integrity using the stored checksum

§Purpose

Validates that chunk data has not been corrupted by comparing the stored SHA-256 checksum against a freshly calculated hash of the current data.

§Why

Integrity verification provides:

  • Detection of data corruption during processing or storage
  • Confidence in pipeline operations
  • Early error detection before expensive operations
  • Compliance with data integrity requirements
§Returns
  • Ok(true) - Checksum matches, data is intact
  • Ok(false) - Checksum mismatch, data corrupted
  • Err(PipelineError::InvalidChunk) - No checksum available
§Errors

Returns PipelineError::InvalidChunk when the chunk has no stored checksum.

§Examples
§Developer Notes
  • This method is read-only and doesn’t modify the chunk
  • Use before critical processing to ensure data integrity
  • Consider verification before expensive operations like encryption
Source

pub fn calculate_checksum(&self) -> Result<String, PipelineError>

Calculates SHA-256 checksum without modifying the chunk

§Developer Notes
  • This is a pure function - doesn’t modify the chunk
  • Use when you need the checksum but don’t want to create a new chunk
  • For creating a chunk with checksum, use with_calculated_checksum()

Trait Implementations§

Source§

impl Clone for FileChunk

Source§

fn clone(&self) -> FileChunk

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 FileChunk

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for FileChunk

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for FileChunk

Source§

fn eq(&self, other: &FileChunk) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for FileChunk

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for FileChunk

Source§

impl StructuralPartialEq for FileChunk

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,