zudb-encoding 0.0.1

Lightweight columnar encodings for zu (FastLanes, ALP, FSST, cascades)
Documentation
//! Dictionary encoding (encoding id 3).
//!
//! Distinct values are sorted into a dictionary and rows become codes, both
//! stored in the frame-of-reference container. Sorted dictionaries keep
//! range predicates cheap and delta-compress well.
//! Layout: `dict_len: u32 LE`, dictionary container, codes container.

use zu_common::{Result, ZuError};

use crate::for_bitpack;

/// Format rule: a dictionary holds at most this many entries, which is
/// exactly the 64 KiB scratch ceiling from the docs/04 decoding
/// contract once materialized as u64. The selector never offers Dict
/// past this cardinality, and the decoder rejects a container claiming
/// more, so readers can size the dictionary buffer before parsing.
pub const MAX_ENTRIES: usize = 8192;

/// Encodes `values` into `out`, returning the encoded byte length.
pub fn encode(values: &[u64], out: &mut Vec<u8>) -> usize {
    let start = out.len();
    let mut dict: Vec<u64> = values.to_vec();
    dict.sort_unstable();
    dict.dedup();
    let codes: Vec<u64> = values
        .iter()
        .map(|v| dict.binary_search(v).unwrap() as u64)
        .collect();
    let mut dict_buf = Vec::new();
    for_bitpack::encode(&dict, &mut dict_buf);
    out.extend_from_slice(&(dict_buf.len() as u32).to_le_bytes());
    out.extend_from_slice(&dict_buf);
    for_bitpack::encode(&codes, out);
    out.len() - start
}

/// Decodes an encoded buffer, appending at most `max_values` values to
/// `out`. The caller ceiling bounds the code stream and the format cap
/// bounds the dictionary, so the one materialized intermediate never
/// grows past 64 KiB whatever the container claims.
pub fn decode(bytes: &[u8], max_values: usize, out: &mut Vec<u64>) -> Result<()> {
    let corrupt = |detail: &str| ZuError::Corrupt {
        what: "dict",
        detail: detail.to_string(),
    };
    let dict_len = u32::from_le_bytes(
        bytes
            .get(..4)
            .ok_or_else(|| corrupt("truncated header"))?
            .try_into()
            .unwrap(),
    ) as usize;
    let dict_bytes = bytes
        .get(4..4 + dict_len)
        .ok_or_else(|| corrupt("truncated dictionary"))?;
    let codes_bytes = bytes
        .get(4 + dict_len..)
        .ok_or_else(|| corrupt("truncated codes"))?;
    let mut dict = Vec::new();
    for_bitpack::decode(dict_bytes, MAX_ENTRIES.min(max_values), &mut dict)?;
    let start = out.len();
    for_bitpack::decode(codes_bytes, max_values, out)?;
    for slot in &mut out[start..] {
        let code = *slot as usize;
        *slot = *dict.get(code).ok_or_else(|| corrupt("code out of range"))?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_low_cardinality() {
        let cities = [90_001u64, 10_115, 75_000, 90_001];
        let values: Vec<u64> = (0..10_000).map(|i| cities[i % cities.len()]).collect();
        let mut buf = Vec::new();
        let len = encode(&values, &mut buf);
        assert!(
            len < values.len(),
            "3 distinct values should pack below 1 B/row"
        );
        let mut out = Vec::new();
        decode(&buf, values.len(), &mut out).unwrap();
        assert_eq!(values, out);
    }

    #[test]
    fn roundtrip_all_distinct() {
        let values: Vec<u64> = (0..3000u64).rev().map(|i| i * 17).collect();
        let mut buf = Vec::new();
        encode(&values, &mut buf);
        let mut out = Vec::new();
        decode(&buf, values.len(), &mut out).unwrap();
        assert_eq!(values, out);
    }

    #[test]
    fn corrupt_code_rejected() {
        let mut buf = Vec::new();
        encode(&[5, 6, 5], &mut buf);
        let mut out = Vec::new();
        assert!(decode(&buf[..5], 16, &mut out).is_err());
    }

    #[test]
    fn dictionary_at_and_past_the_cap() {
        // Exactly the cap decodes; one entry more is a format violation
        // the reader must reject however large the caller ceiling is.
        let at_cap: Vec<u64> = (0..MAX_ENTRIES as u64).map(|i| i * 3).collect();
        let mut buf = Vec::new();
        encode(&at_cap, &mut buf);
        let mut out = Vec::new();
        decode(&buf, at_cap.len(), &mut out).unwrap();
        assert_eq!(out, at_cap);
        let past: Vec<u64> = (0..MAX_ENTRIES as u64 + 1).map(|i| i * 3).collect();
        let mut buf = Vec::new();
        encode(&past, &mut buf);
        let mut out = Vec::new();
        assert!(decode(&buf, past.len(), &mut out).is_err());
        assert!(out.is_empty(), "a rejected payload must not touch out");
    }
}