tinystore 0.7.1

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
use super::*;

fn points(times: &[i64], values: &[f64]) -> Vec<Point> {
    times.iter().zip(values).map(|(&at, &value)| Point { at, value }).collect()
}

fn even(count: usize, mut value: impl FnMut(usize) -> f64) -> Vec<Point> {
    (0..count).map(|index| Point { at: 1_700_000_000_000 + index as i64 * 15_000, value: value(index) }).collect()
}

/// Encodes and decodes `run`, checks every time and every bit came back, and
/// gives the flags it was kept with.
fn round_trip(run: &[Point]) -> u8 {
    let (head, body) = encode(&mut zstd(), run).unwrap();
    let mut out = vec![Point { at: 7, value: 7.0 }];
    decode(&mut zstd(), &head, &body, &mut out).unwrap();
    assert_eq!(out.len(), run.len() + 1, "decode appends");
    for (wrote, read) in run.iter().zip(&out[1..]) {
        assert_eq!((wrote.at, wrote.value.to_bits()), (read.at, read.value.to_bits()));
    }
    body[1]
}

fn value_mode(flags: u8) -> u8 {
    (flags >> 2) & 7
}

// contract: MET-01
#[test]
fn every_value_representation_keeps_the_bits() {
    let cases: [(&str, Vec<Point>, u8); 6] = [
        ("constant", even(240, |_| 42.5), VALUES_CONSTANT),
        ("integers", even(240, |index| (index as f64) * 3.0 - 100.0), VALUES_INTEGERS),
        ("decimals", even(240, |index| (index % 17) as f64 / 10.0), VALUES_DECIMALS),
        ("xor", even(240, |index| f64::from_bits(0x4059_0000_0000_0000 + ((index as u64 * 7919) % 1024))), VALUES_XOR),
        ("raw", even(16, |index| f64::from_bits((index as u64 + 1).wrapping_mul(0x9e37_79b9_7f4a_7c15))), VALUES_RAW),
        ("one", even(1, |_| f64::from_bits(0x7ff4_0000_dead_beef)), VALUES_CONSTANT),
    ];
    for (name, run, mode) in cases {
        let flags = round_trip(&run);
        assert_eq!(value_mode(flags), mode, "{name}");
    }
}

#[test]
fn the_extremes_survive_too() {
    let extremes = [
        -0.0,
        0.0,
        f64::NAN,
        f64::from_bits(0x7ff8_0000_0000_0001),
        f64::from_bits(0xfff4_0000_0000_beef),
        f64::INFINITY,
        f64::NEG_INFINITY,
        f64::MIN_POSITIVE,
        f64::from_bits(1),
        -f64::from_bits(1),
        f64::MAX,
        f64::MIN,
        9_007_199_254_740_993.0,
        -9_223_372_036_854_775_808.0,
    ];
    let values: Vec<f64> = (0..240).map(|index| extremes[index % extremes.len()]).collect();
    let times: Vec<i64> = (0..240).map(|index| i64::MIN + index as i64).collect();
    round_trip(&points(&times, &values));
    let late: Vec<i64> = (0..240).map(|index| i64::MAX - 239 + index as i64).collect();
    round_trip(&points(&late, &values));
    round_trip(&points(&[i64::MIN, i64::MAX], &[1.0, 2.0]));
}

#[test]
fn times_are_kept_evenly_as_deltas_or_as_their_changes_whichever_is_shortest() {
    let mut steady = even(240, |index| index as f64);
    assert_eq!(round_trip(&steady) & 3, TIMES_EVEN);
    steady[100].at += 1;
    assert_eq!(round_trip(&steady) & 3, TIMES_CHANGES);
    // steps of 1 and 100 by turns: each one byte, each change two
    let jumpy: Vec<i64> = (0..240).map(|index| index / 2 * 101 + index % 2).collect();
    let values = vec![1.0; 240];
    assert_eq!(round_trip(&points(&jumpy, &values)) & 3, TIMES_DELTAS);
}

#[test]
fn a_decimal_travels_as_the_integer_it_was_written_as() {
    // a price that walks by cents, which no period lets zstd fold
    let mut cents = 1999_i64;
    let run: Vec<Point> = even(240, |index| {
        cents += (index as i64 * 7919 % 13) - 6;
        cents as f64 / 100.0
    });
    assert_eq!(value_mode(round_trip(&run)), VALUES_DECIMALS);
    assert_eq!(exact_scale(19.99), Some(2));
    assert_eq!(exact_scale(1e-9), Some(9));
}

#[test]
fn deltas_that_wander_go_in_rice_codes_and_steady_ones_in_words() {
    // a round trip in milliseconds, a tenth either way of the one before
    let wandering = even(240, |index| (234 + (index * 7919 % 9) as i64 - 4) as f64 / 10.0);
    let (mode, bytes) = encode_values(&wandering);
    assert_eq!((mode, bytes[0]), (VALUES_DECIMALS, DELTAS_IN_RICE));
    round_trip(&wandering);
    // one less each time: 239 deltas in a word of 120 ones and two of 60 bits, against 32 bytes of rice
    let falling = even(240, |index| 1000.0 - index as f64);
    let (mode, bytes) = encode_values(&falling);
    assert_eq!((mode, bytes[0], bytes.len()), (VALUES_INTEGERS, DELTAS_IN_WORDS, 25));
    round_trip(&falling);
}

#[test]
fn rice_codes_that_end_early_or_late_are_refused() {
    let mut out = vec![Point::default(); 3];
    let head = Head { start: 0, end: 2, count: 3, first: 5.0 };
    // k = 1: -1 is folded 1, "0 1"; then -2, folded 3, "1 0 1"; read lowest bit first
    let fine = [DELTAS_IN_RICE, 1, 0b1_0110];
    decode_integers(&head, false, &fine, &mut out).unwrap();
    assert_eq!(out[1..].iter().map(|point| point.value).collect::<Vec<_>>(), [4.0, 2.0]);
    assert!(decode_integers(&head, false, &[DELTAS_IN_RICE, 1, 0b1_0110, 0], &mut out).is_err(), "a byte past");
    assert!(decode_integers(&head, false, &[DELTAS_IN_RICE, 1], &mut out).is_err(), "no codes");
    assert!(decode_integers(&head, false, &[DELTAS_IN_RICE, 64, 0], &mut out).is_err(), "k past 63");
}

#[test]
fn a_value_no_scale_reproduces_is_refused_rather_than_rounded() {
    assert_eq!(exact_scale(0.1 + 0.2), None);
    assert_eq!(exact_scale(-0.0), None);
    assert_eq!(exact_scale(f64::NAN), None);
    assert_eq!(exact_scale(1e-10), None);
    // one such value keeps the whole run off the decimal grid, and its bits whole
    let run = even(240, |index| if index == 7 { 0.1 + 0.2 } else { index as f64 / 10.0 });
    assert_ne!(value_mode(round_trip(&run)), VALUES_DECIMALS);
}

#[test]
fn times_alone_come_back_as_a_whole_decode_gives_them_and_a_corrupt_body_is_refused() {
    let mut steady = even(240, |index| (index as f64).sin());
    let jumpy: Vec<i64> = (0..240).map(|index| index / 2 * 101 + index % 2).collect();
    let wandering = points(&jumpy, &vec![1.0; 240]);
    steady[100].at += 1;
    for run in [even(240, |index| index as f64), steady, wandering] {
        let (head, body) = encode(&mut zstd(), &run).unwrap();
        let mut times = vec![Point { at: 7, value: 7.0 }];
        decode_times_alone(&mut zstd(), &head, &body, &mut times).unwrap();
        let read: Vec<i64> = times[1..].iter().map(|point| point.at).collect();
        assert_eq!(read, run.iter().map(|point| point.at).collect::<Vec<_>>(), "after what `out` held");
        let mut broken = body.clone();
        broken[body.len() / 2] ^= 0x10;
        assert!(decode_times_alone(&mut zstd(), &head, &broken, &mut times).is_err(), "its checksum binds it");
        assert_eq!(times.len(), 241, "a refused body appends nothing");
    }
}

#[test]
fn a_corrupt_body_is_refused() {
    let run = even(240, |index| (index as f64).sin());
    let (head, body) = encode(&mut zstd(), &run).unwrap();
    for place in 0..body.len() {
        let mut broken = body.clone();
        broken[place] ^= 0x10;
        let error = decode(&mut zstd(), &head, &broken, &mut Vec::new()).unwrap_err();
        assert_eq!(error.kind(), crate::ErrorKind::Corrupt, "byte {place}: {error}");
    }
    for cut in 0..body.len() {
        assert!(decode(&mut zstd(), &head, &body[..cut], &mut Vec::new()).is_err(), "cut at {cut}");
    }
}

#[test]
fn a_head_that_does_not_fit_its_body_is_refused() {
    let run = even(100, |index| index as f64);
    let (head, body) = encode(&mut zstd(), &run).unwrap();
    let moved = [
        Head { start: head.start + 1, ..head },
        Head { end: head.end + 15_000, ..head },
        Head { count: head.count - 1, ..head },
        Head { first: 1.0, ..head },
        Head { count: 0, ..head },
        Head { count: MOST_SAMPLES + 1, ..head },
        Head { end: head.start - 1, ..head },
    ];
    for other in moved {
        let mut out = Vec::new();
        assert!(decode(&mut zstd(), &other, &body, &mut out).is_err(), "{other:?}");
        assert!(out.is_empty(), "a refused body appends nothing");
    }
}

#[test]
fn unordered_or_oversized_input_is_refused() {
    assert!(encode(&mut zstd(), &[]).is_err());
    assert!(encode(&mut zstd(), &even(MOST_SAMPLES + 1, |_| 1.0)).is_err());
    assert!(encode(&mut zstd(), &points(&[5, 5], &[1.0, 2.0])).is_err());
    assert!(encode(&mut zstd(), &points(&[6, 5], &[1.0, 2.0])).is_err());
}

#[test]
fn values_alone_read_back_into_the_times_their_caller_gives() {
    let values: Vec<f64> = (0..240).map(|index| (index % 9) as f64 * 0.5).collect();
    let stream = encode_values_alone(&mut zstd(), &values).unwrap();
    let mut run: Vec<Point> = (0..240).map(|index| Point { at: index * 1000 + 3, value: 0.0 }).collect();
    decode_values_alone(&mut zstd(), values[0], &stream, &mut run).unwrap();
    for (index, point) in run.iter().enumerate() {
        assert_eq!((point.at, point.value.to_bits()), (index as i64 * 1000 + 3, values[index].to_bits()));
    }
}

#[test]
fn a_word_of_ones_past_its_run_or_padding_that_is_not_zero_is_refused() {
    let mut out = vec![Point::default(); 3];
    let head = Head { start: 0, end: 2, count: 3, first: 5.0 };
    // a run of 120 ones where two values are left
    let mut words = vec![DELTAS_IN_WORDS];
    words.extend_from_slice(&(1_u64 << 60).to_le_bytes());
    assert!(decode_integers(&head, false, &words, &mut out).is_err());
    // a word of 60 one-bit fields holding a set bit past the two values left
    let mut padded = vec![DELTAS_IN_WORDS];
    padded.extend_from_slice(&((2_u64 << 60) | 0b100).to_le_bytes());
    assert!(decode_integers(&head, false, &padded, &mut out).is_err());
    let mut fine = vec![DELTAS_IN_WORDS];
    fine.extend_from_slice(&((2_u64 << 60) | 0b11).to_le_bytes());
    decode_integers(&head, false, &fine, &mut out).unwrap();
    assert_eq!(out[1..].iter().map(|point| point.value).collect::<Vec<_>>(), [4.0, 3.0]);
}