Skip to main content

heddle_format/compression/
dictionaries.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Immutable dictionaries bundled with the storage-format decoder.
3
4const TREE_STATE_V1_ID: u32 = 1;
5const TREE_STATE_V1: &[u8] = include_bytes!("dictionaries/tree-state-v1.zdict");
6
7/// A versioned zstd dictionary available to object encoders.
8///
9/// IDs are durable storage-format identifiers. Once an ID has shipped, its
10/// bytes must never change or be removed from the decoder registry.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum CompressionDictionary {
13    /// Dictionary v1, trained offline over serialized tree and state objects.
14    TreeStateV1,
15}
16
17impl CompressionDictionary {
18    pub(crate) const fn id(self) -> u32 {
19        match self {
20            Self::TreeStateV1 => TREE_STATE_V1_ID,
21        }
22    }
23
24    pub(crate) const fn bytes(self) -> &'static [u8] {
25        match self {
26            Self::TreeStateV1 => TREE_STATE_V1,
27        }
28    }
29}
30
31pub(crate) const fn lookup(id: u32) -> Option<&'static [u8]> {
32    match id {
33        TREE_STATE_V1_ID => Some(TREE_STATE_V1),
34        _ => None,
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn dictionary_id_is_registered() {
44        let dictionary = CompressionDictionary::TreeStateV1;
45
46        assert_eq!(lookup(dictionary.id()), Some(dictionary.bytes()));
47    }
48
49    #[test]
50    fn tree_state_v1_asset_is_a_trained_zstd_dictionary() {
51        const ZSTD_DICTIONARY_MAGIC: [u8; 4] = [0x37, 0xA4, 0x30, 0xEC];
52        const EXPECTED_BLAKE3: &str =
53            "ca0ee171814fa7bf91f4c22ef7a1a4d87f7f13a4052b47774e50a8c90a85cd80";
54
55        assert_eq!(TREE_STATE_V1.len(), 8 * 1024);
56        assert_eq!(&TREE_STATE_V1[..4], &ZSTD_DICTIONARY_MAGIC);
57        assert_eq!(
58            blake3::hash(TREE_STATE_V1).to_hex().as_str(),
59            EXPECTED_BLAKE3
60        );
61    }
62}