pub struct ZstdCodec {
pub compression_level: i32,
pub dict: Option<Vec<u8>>,
}Expand description
Zstandard codec with optional pre-trained dictionary support.
ZstdCodec provides both basic Zstd compression and dictionary-accelerated
compression. The dictionary approach is particularly effective for RDF triple
blocks where subjects, predicates, and objects share common URI prefixes.
Fields§
§compression_level: i32Compression level (1-22, default 3)
dict: Option<Vec<u8>>Optional pre-trained Zstd dictionary bytes
Implementations§
Source§impl ZstdCodec
impl ZstdCodec
Sourcepub fn new(level: i32) -> Self
pub fn new(level: i32) -> Self
Create a new ZstdCodec with the given compression level and no dictionary.
§Arguments
level- Compression level clamped to 1-22. Level 3 is the recommended default.
Sourcepub fn with_dict(level: i32, dict: Vec<u8>) -> Self
pub fn with_dict(level: i32, dict: Vec<u8>) -> Self
Create a new ZstdCodec with the given compression level and a pre-trained dictionary.
Dictionary-based compression is typically 2-5x more effective on small blocks compared to raw Zstd when the data shares structural patterns with the training samples.
§Arguments
level- Compression level clamped to 1-22dict- Pre-trained Zstd dictionary bytes (produced byZstdCodec::train_dict)
Sourcepub fn compress(&self, data: &[u8]) -> Result<Vec<u8>>
pub fn compress(&self, data: &[u8]) -> Result<Vec<u8>>
Compress data using Zstd, optionally with a pre-trained dictionary.
Returns the compressed bytes. Use decompress with the
same dictionary to recover the original data.
Sourcepub fn decompress(&self, data: &[u8]) -> Result<Vec<u8>>
pub fn decompress(&self, data: &[u8]) -> Result<Vec<u8>>
Decompress data previously compressed with this codec (same dictionary required).
§Errors
Returns TdbError::Other if decompression fails or dictionary mismatch occurs.
Sourcepub fn compress_with_stats(
&self,
data: &[u8],
) -> Result<(Vec<u8>, ZstdCodecStats)>
pub fn compress_with_stats( &self, data: &[u8], ) -> Result<(Vec<u8>, ZstdCodecStats)>
Compress data and return both the compressed bytes and statistics.
Sourcepub fn train_dict(samples: &[Vec<u8>], dict_size: usize) -> Result<Vec<u8>>
pub fn train_dict(samples: &[Vec<u8>], dict_size: usize) -> Result<Vec<u8>>
Train a Zstd dictionary from a collection of data samples.
This is the key capability for RDF graph compression: by training on a representative sample of RDF triples, the resulting dictionary captures common URI prefixes and literal patterns, yielding significantly better compression for small triple blocks.
§Arguments
samples- Slice of byte vectors representing individual data samples (e.g., serialized triples)dict_size- Target dictionary size in bytes. Recommended: 1024-65536 bytes. Larger dictionaries capture more patterns but consume more memory.
§Returns
The trained dictionary as a byte vector suitable for passing to ZstdCodec::with_dict.
§Errors
Returns TdbError::Other if:
- No samples are provided
- Samples are too small (< 8 bytes each)
- Total sample data exceeds 1GB
- The zstd dictionary trainer fails
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ZstdCodec
impl RefUnwindSafe for ZstdCodec
impl Send for ZstdCodec
impl Sync for ZstdCodec
impl Unpin for ZstdCodec
impl UnsafeUnpin for ZstdCodec
impl UnwindSafe for ZstdCodec
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<T> Fruit for T
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