use oxabl_cache_format::SegmentId;
use zeroize::Zeroizing;
use zstd::dict::{DecoderDictionary, EncoderDictionary};
use crate::error::{Error, Result};
pub const DEFAULT_DICTIONARY_BYTES: usize = 112 * 1024;
pub const MAX_DICTIONARY_BYTES: usize = 8 * 1024 * 1024;
pub struct Dictionary {
id: SegmentId,
bytes: Zeroizing<Vec<u8>>,
encoder: EncoderDictionary<'static>,
decoder: DecoderDictionary<'static>,
}
impl Dictionary {
pub fn new(id: SegmentId, bytes: Vec<u8>, level: i32) -> Result<Self> {
let bytes = Zeroizing::new(bytes);
if bytes.is_empty() || bytes.len() > MAX_DICTIONARY_BYTES {
return Err(Error::Limit("dictionary size"));
}
Ok(Self {
id,
encoder: EncoderDictionary::copy(&bytes, level),
decoder: DecoderDictionary::copy(&bytes),
bytes,
})
}
pub fn id(&self) -> SegmentId {
self.id
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub(crate) fn encoder(&self) -> &EncoderDictionary<'static> {
&self.encoder
}
pub(crate) fn decoder(&self) -> &DecoderDictionary<'static> {
&self.decoder
}
}
impl std::fmt::Debug for Dictionary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Dictionary({})", self.id)
}
}
pub fn train<S: AsRef<[u8]>>(samples: &[S], max_size: usize) -> Result<Vec<u8>> {
if max_size == 0 || max_size > MAX_DICTIONARY_BYTES {
return Err(Error::Limit("dictionary size"));
}
zstd::dict::from_samples(samples, max_size)
.map_err(|_| Error::Compression("dictionary training failed"))
}