Skip to main content

BlockDeduplicator

Struct BlockDeduplicator 

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

Content-aware block deduplicator using variable-length CDC chunking.

Stores objects by splitting them into content-defined chunks, using the chunk hash as the storage key. Identical chunks are stored only once and reference-counted. When all objects referencing a chunk are deleted, the chunk data is freed.

§Algorithm

Content-Defined Chunking (CDC) uses a rolling hash over a sliding window:

  • For each byte position after min_chunk_size into the current chunk, compute rolling_hash(data[pos-W..pos]) where W = window_size.
  • If hash & mask == 0 (where mask = 2^target_bits - 1), emit a boundary.
  • Also emit a boundary when chunk_length >= max_chunk_size.
  • The final remaining bytes are always emitted as the last chunk.

§Thread safety

BlockDeduplicator is not Sync. Use external locking (e.g. Mutex) when sharing across threads.

Implementations§

Source§

impl BlockDeduplicator

Source

pub fn new(config: ChunkingConfig) -> Self

Create a new BlockDeduplicator with the given configuration.

Source

pub fn with_defaults() -> Self

Create a BlockDeduplicator with default configuration.

Source

pub fn chunk_data(&self, data: &[u8]) -> Vec<(ChunkHash, Vec<u8>)>

Split data into content-defined chunks using the rolling-hash CDC algorithm.

Returns an ordered list of (hash, chunk_bytes) pairs. The concatenation of all chunk_bytes is guaranteed to equal data exactly.

§Edge cases
  • Empty input → returns an empty Vec.
  • Input shorter than min_chunk_size → returns a single chunk.
  • Input shorter than window_size → rolling hash uses the full available prefix.
Source

pub fn store_object( &mut self, object_id: String, data: Vec<u8>, ) -> Result<ObjectManifest, DeduplicatorError>

Store an object, deduplicating its content-defined chunks.

If an identical chunk (same hash) already exists, its ref_count is incremented and the duplicate bytes are not stored again. A manifest is built and stored internally.

§Errors

Currently infallible, but returns Result for future extensibility (e.g., when storage-full limits are enforced).

Source

pub fn retrieve_object( &self, object_id: &str, ) -> Result<Vec<u8>, DeduplicatorError>

Retrieve the full byte content of a stored object by reconstructing it from its chunk manifest.

§Errors
Source

pub fn delete_object( &mut self, object_id: &str, ) -> Result<Vec<ChunkHash>, DeduplicatorError>

Delete a stored object, decrementing ref counts of its chunks.

Any chunk whose ref_count reaches zero is removed from the store. Returns the hashes of all physically removed chunks.

§Errors
Source

pub fn get_chunk(&self, hash: &ChunkHash) -> Result<&Chunk, DeduplicatorError>

Return a reference to the Chunk metadata for the given hash.

§Errors
Source

pub fn chunk_exists(&self, hash: &ChunkHash) -> bool

Return true if a chunk with the given hash is present in the store.

Source

pub fn compact(&mut self) -> usize

Remove all chunks whose ref_count == 0 from the store.

This is a garbage-collection pass for chunks that were decremented to zero during earlier delete_object calls but not yet physically removed (e.g., if delete_object was called but removal was deferred). In the current implementation, delete_object removes zero-ref chunks eagerly, so this acts as a safety pass.

Returns the number of chunks removed.

Source

pub fn stats(&self) -> DeduplicationStats

Compute and return a snapshot of current deduplication statistics.

Source

pub fn list_objects(&self) -> Vec<String>

Return a sorted list of all stored object IDs.

Source

pub fn chunk_count(&self) -> usize

Return the number of unique chunks currently in the store.

Source

pub fn object_count(&self) -> usize

Return the number of objects currently stored.

Source

pub fn get_manifest(&self, object_id: &str) -> Option<&ObjectManifest>

Retrieve the manifest for a stored object without reconstructing data.

Source

pub fn config(&self) -> &ChunkingConfig

Return a reference to the current chunking configuration.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more