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_sizeinto the current chunk, computerolling_hash(data[pos-W..pos])whereW = window_size. - If
hash & mask == 0(wheremask = 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
impl BlockDeduplicator
Sourcepub fn new(config: ChunkingConfig) -> Self
pub fn new(config: ChunkingConfig) -> Self
Create a new BlockDeduplicator with the given configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a BlockDeduplicator with default configuration.
Sourcepub fn chunk_data(&self, data: &[u8]) -> Vec<(ChunkHash, Vec<u8>)>
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.
Sourcepub fn store_object(
&mut self,
object_id: String,
data: Vec<u8>,
) -> Result<ObjectManifest, DeduplicatorError>
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).
Sourcepub fn retrieve_object(
&self,
object_id: &str,
) -> Result<Vec<u8>, DeduplicatorError>
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
DeduplicatorError::ObjectNotFoundifobject_idhas no manifest.DeduplicatorError::ChunkNotFoundif a chunk referenced by the manifest is missing from the store (indicates data corruption).DeduplicatorError::InvalidManifestif the reconstructed size does not match the manifest’s declaredtotal_size.
Sourcepub fn delete_object(
&mut self,
object_id: &str,
) -> Result<Vec<ChunkHash>, DeduplicatorError>
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
DeduplicatorError::ObjectNotFoundifobject_idhas no manifest.
Sourcepub fn get_chunk(&self, hash: &ChunkHash) -> Result<&Chunk, DeduplicatorError>
pub fn get_chunk(&self, hash: &ChunkHash) -> Result<&Chunk, DeduplicatorError>
Return a reference to the Chunk metadata for the given hash.
§Errors
DeduplicatorError::ChunkNotFoundif no chunk with that hash exists.
Sourcepub fn chunk_exists(&self, hash: &ChunkHash) -> bool
pub fn chunk_exists(&self, hash: &ChunkHash) -> bool
Return true if a chunk with the given hash is present in the store.
Sourcepub fn compact(&mut self) -> usize
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.
Sourcepub fn stats(&self) -> DeduplicationStats
pub fn stats(&self) -> DeduplicationStats
Compute and return a snapshot of current deduplication statistics.
Sourcepub fn list_objects(&self) -> Vec<String>
pub fn list_objects(&self) -> Vec<String>
Return a sorted list of all stored object IDs.
Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Return the number of unique chunks currently in the store.
Sourcepub fn object_count(&self) -> usize
pub fn object_count(&self) -> usize
Return the number of objects currently stored.
Sourcepub fn get_manifest(&self, object_id: &str) -> Option<&ObjectManifest>
pub fn get_manifest(&self, object_id: &str) -> Option<&ObjectManifest>
Retrieve the manifest for a stored object without reconstructing data.
Sourcepub fn config(&self) -> &ChunkingConfig
pub fn config(&self) -> &ChunkingConfig
Return a reference to the current chunking configuration.
Auto Trait Implementations§
impl Freeze for BlockDeduplicator
impl RefUnwindSafe for BlockDeduplicator
impl Send for BlockDeduplicator
impl Sync for BlockDeduplicator
impl Unpin for BlockDeduplicator
impl UnsafeUnpin for BlockDeduplicator
impl UnwindSafe for BlockDeduplicator
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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