tinystore 0.12.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Bits: fields packed one after another, the lowest bit first, integers of
//! one width, and rice codes, which give a small integer few bits and any
//! integer some.

/// The bytes `count` values of `width` bits take.
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) fn packed_len(count: usize, width: u32) -> usize {
    (count * width as usize).div_ceil(8)
}

/// Appends `values`, each in `width` bits; a value wider than that loses its
/// high bits, which a caller's width never lets happen.
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) fn pack(out: &mut Vec<u8>, values: impl Iterator<Item = u64>, width: u32) {
    let mut writer = BitWriter::onto(std::mem::take(out));
    values.for_each(|value| writer.write(value, width));
    *out = writer.finish();
}

/// `count` values of `width` bits, 64 at most, from `data`, which holds the
/// `packed_len` of them.
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) fn unpack(data: &[u8], count: usize, width: u32) -> Vec<u64> {
    let mut reader = BitReader::of(data);
    let values = (0..count).map(|_| reader.read(width)).collect();
    debug_assert!(!reader.short(), "unpack's caller checks that `data` holds {count} values of {width} bits");
    values
}

/// A writer of bits onto bytes, the lowest bit of a byte first.
pub(crate) struct BitWriter {
    out: Vec<u8>,
    held: u128,
    bits: u32,
}

impl BitWriter {
    pub(crate) fn onto(out: Vec<u8>) -> BitWriter {
        BitWriter { out, held: 0, bits: 0 }
    }

    /// Appends the low `width` bits of `value`, 64 at most.
    pub(crate) fn write(&mut self, value: u64, width: u32) {
        let low = if width >= 64 { value } else { value & ((1_u64 << width) - 1) };
        self.held |= u128::from(low) << self.bits;
        self.bits += width;
        while self.bits >= 8 {
            self.out.push(self.held as u8);
            self.held >>= 8;
            self.bits -= 8;
        }
    }

    /// The bytes written, the last one's unused bits zero.
    pub(crate) fn finish(mut self) -> Vec<u8> {
        if self.bits > 0 {
            self.out.push(self.held as u8);
        }
        self.out
    }
}

/// A reader of the bits a writer wrote, a word at a time: research's 239
/// residuals of 13 bits went from 4.01 µs read a bit at a time to 0.32 µs
/// read so. Past the end it reads zeros and says so in `short`.
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) struct BitReader<'a> {
    data: &'a [u8],
    held: u128,
    bits: u32,
    short: bool,
}

#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
impl<'a> BitReader<'a> {
    pub(crate) fn of(data: &'a [u8]) -> BitReader<'a> {
        BitReader { data, held: 0, bits: 0, short: false }
    }

    /// The next `width` bits, 64 at most.
    #[inline]
    pub(crate) fn read(&mut self, width: u32) -> u64 {
        if self.bits < width {
            self.refill();
            if self.bits < width {
                self.short = true;
                self.bits = width;
            }
        }
        let value = if width >= 64 { self.held as u64 } else { self.held as u64 & ((1_u64 << width) - 1) };
        self.held >>= width;
        self.bits -= width;
        value
    }

    /// How many 1 bits come next, `most` at most, 63 or fewer; the 0 that
    /// ends fewer is read with them. They are counted in the word that
    /// waits, since a rice code read a bit at a time took most of a run's
    /// decoding.
    #[inline]
    pub(crate) fn read_ones(&mut self, most: u32) -> u32 {
        if self.bits <= most {
            self.refill();
        }
        if self.bits <= most {
            // the data ends here: a bit at a time, which marks a read past it short
            let mut ones = 0;
            while ones < most && self.read(1) == 1 {
                ones += 1;
            }
            return ones;
        }
        // no bit is held past `bits`, so the ones counted are all there
        let ones = (self.held as u64).trailing_ones().min(most);
        let width = if ones == most { most } else { ones + 1 };
        self.held >>= width;
        self.bits -= width;
        ones
    }

    /// A rice code of parameter `k` whose bits all wait in one word, read
    /// with one shift; none for an escape, a longer code, or one the data
    /// ends in, which [`read_rice`] reads a field at a time.
    #[inline]
    fn rice_in_a_word(&mut self, k: u32) -> Option<u64> {
        if self.bits < 64 {
            self.refill();
        }
        let low = self.held as u64;
        let ones = low.trailing_ones();
        let width = ones + 1 + k;
        if self.bits < 64 || ones >= RICE_ESCAPE || width > 64 {
            return None;
        }
        // the ones and their zero are 32 bits at most, so neither shift is of a whole word
        let rest = (low >> (ones + 1)) & ((1_u64 << k) - 1);
        self.held >>= width;
        self.bits -= width;
        Some(u64::from(ones) << k | rest)
    }

    /// Loads whole bytes until a word or more waits.
    fn refill(&mut self) {
        while self.bits <= 64 {
            if let Some((word, rest)) = self.data.split_first_chunk::<8>() {
                self.held |= u128::from(u64::from_le_bytes(*word)) << self.bits;
                self.bits += 64;
                self.data = rest;
            } else if let Some((&byte, rest)) = self.data.split_first() {
                self.held |= u128::from(byte) << self.bits;
                self.bits += 8;
                self.data = rest;
            } else {
                return;
            }
        }
    }

    /// Whether a read went past the end.
    #[cfg_attr(
        all(not(test), not(any(feature = "records", feature = "metrics"))),
        expect(dead_code, reason = "records and metrics use it")
    )]
    pub(crate) fn short(&self) -> bool {
        self.short
    }

    /// Every field was there, and what is left is the last byte's padding,
    /// zero.
    #[cfg_attr(all(not(test), not(feature = "metrics")), expect(dead_code, reason = "only metrics uses it"))]
    pub(crate) fn finished(&self) -> bool {
        !self.short && self.data.is_empty() && self.bits < 8 && self.held == 0
    }
}

/// A quotient this large or larger is written as the escape and 64 raw bits.
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
const RICE_ESCAPE: u32 = 32;

/// Appends the rice code of `value` at parameter `k`, 63 at most: `value >>
/// k` in unary and the low `k` bits as they are.
///
/// ```text
/// k=2   5 → 1 0 01     0 → 0 00     13 → 111 0 01
/// ```
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) fn write_rice(writer: &mut BitWriter, value: u64, k: u32) {
    let quotient = value >> k;
    if quotient < u64::from(RICE_ESCAPE) {
        writer.write((1_u64 << quotient) - 1, quotient as u32);
        writer.write(0, 1);
        writer.write(value, k);
    } else {
        writer.write(u64::MAX, RICE_ESCAPE);
        writer.write(value, 64);
    }
}

#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) fn read_rice(reader: &mut BitReader<'_>, k: u32) -> u64 {
    if let Some(value) = reader.rice_in_a_word(k) {
        return value;
    }
    let quotient = reader.read_ones(RICE_ESCAPE);
    if quotient == RICE_ESCAPE { reader.read(64) } else { u64::from(quotient) << k | reader.read(k) }
}

/// Integers counted by their length in bits, with the sums that price a
/// rice code at every parameter exactly, in one pass over them.
#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
pub(crate) struct RiceCost {
    counts: [u64; 65],
    /// By a length and how many bits above a parameter it stands, one to
    /// five: the sum of the values of that length shifted down to that many
    /// bits, which is the sum of their quotients.
    quotients: [[u64; 6]; 65],
    longest: usize,
}

#[cfg_attr(
    all(not(test), not(any(feature = "records", feature = "metrics"))),
    expect(dead_code, reason = "records and metrics use it")
)]
impl RiceCost {
    pub(crate) fn of(values: &[u64]) -> RiceCost {
        let mut cost = RiceCost { counts: [0; 65], quotients: [[0; 6]; 65], longest: 0 };
        for value in values {
            let length = (64 - value.leading_zeros()) as usize;
            cost.counts[length] += 1;
            cost.longest = cost.longest.max(length);
            for above in 1..=length.min(5) {
                cost.quotients[length][above] += value >> (length - above);
            }
        }
        cost
    }

    /// The bits the values take at parameter `k`.
    fn bits(&self, k: usize) -> u64 {
        let of_length = |length: usize| match self.counts[length] {
            0 => 0,
            count if length <= k => count * (1 + k as u64),
            count if length <= k + 5 => self.quotients[length][length - k] + count * (1 + k as u64),
            count => count * u64::from(RICE_ESCAPE + 64),
        };
        (0..=self.longest).map(of_length).sum()
    }

    /// The cheapest parameter and the bits the values take at it. The mean
    /// is a poor guide, since a few wide values raise it for all:
    ///
    /// ```text
    /// 14 zeros and 1000000 twice   k 0: 14×1 + 2×96 = 206 bits
    ///                              k 16, near log2 of the mean: 14×17 + 2×(15+17) = 302 bits
    /// ```
    pub(crate) fn cheapest(&self) -> (u32, u64) {
        // a parameter past the longest value only adds bits
        let tried = (0..=self.longest.min(63)).map(|k| (k as u32, self.bits(k)));
        tried.min_by_key(|(_, bits)| *bits).unwrap_or((0, 0))
    }
}

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

    #[test]
    fn values_of_every_width_come_back_in_the_bytes_their_bits_take() {
        for width in [0, 1, 3, 7, 8, 13, 31, 32, 63, 64] {
            let most = if width == 64 { u64::MAX } else { (1_u64 << width) - 1 };
            let values: Vec<u64> = (0..37_u64).map(|n| n.wrapping_mul(0x9e37_79b9_7f4a_7c15) & most).collect();
            let mut packed = Vec::new();
            pack(&mut packed, values.iter().copied(), width);
            assert_eq!(packed.len(), packed_len(values.len(), width), "width {width}");
            assert_eq!(unpack(&packed, values.len(), width), values, "width {width}");
        }
    }

    #[test]
    fn a_rice_code_gives_back_any_value_at_any_parameter_in_the_bits_its_cost_says() {
        let values: Vec<u64> = [0, 1, 5, 13, 31, 32, 1000, 1_000_000, u64::from(u32::MAX), u64::MAX, 1 << 63]
            .into_iter()
            .chain((0..40).map(|n: u64| n.wrapping_mul(0x9e37_79b9_7f4a_7c15) >> (n % 64)))
            .collect();
        let cost = RiceCost::of(&values);
        for k in 0..64 {
            let mut writer = BitWriter::onto(Vec::new());
            values.iter().for_each(|value| write_rice(&mut writer, *value, k));
            let coded = writer.finish();
            assert_eq!(coded.len() as u64, cost.bits(k as usize).div_ceil(8), "k {k}");
            let mut reader = BitReader::of(&coded);
            let read: Vec<u64> = (0..values.len()).map(|_| read_rice(&mut reader, k)).collect();
            assert!(!reader.short() && read == values, "k {k}");
        }
    }

    #[test]
    fn a_run_of_ones_reads_as_it_does_a_bit_at_a_time_whole_or_cut_anywhere() {
        let mut writer = BitWriter::onto(Vec::new());
        for ones in (0..=40).chain([0, 63, 64, 1, 100, 7]) {
            (0..ones).for_each(|_| writer.write(1, 1));
            writer.write(0, 1);
            writer.write(0b101, 3);
        }
        let coded = writer.finish();
        for cut in 0..=coded.len() {
            for most in [1, 5, 32, 63] {
                let (mut counted, mut one_by_one) = (BitReader::of(&coded[..cut]), BitReader::of(&coded[..cut]));
                for _ in 0..80 {
                    let mut ones = 0;
                    while ones < most && one_by_one.read(1) == 1 {
                        ones += 1;
                    }
                    assert_eq!(counted.read_ones(most), ones, "cut at {cut}, {most} at most");
                    assert_eq!(counted.read(3), one_by_one.read(3), "cut at {cut}, {most} at most");
                    assert_eq!(counted.short(), one_by_one.short(), "cut at {cut}, {most} at most");
                }
                assert_eq!(counted.finished(), one_by_one.finished(), "cut at {cut}, {most} at most");
            }
        }
    }

    #[test]
    fn a_rice_code_cut_anywhere_reads_as_it_does_a_field_at_a_time() {
        let values: Vec<u64> =
            (0..60).map(|n: u64| n.wrapping_mul(0x9e37_79b9_7f4a_7c15) >> (n * 7 % 64)).chain([0, u64::MAX]).collect();
        for k in [0, 1, 7, 11, 31, 32, 33, 62, 63] {
            let mut writer = BitWriter::onto(Vec::new());
            values.iter().for_each(|value| write_rice(&mut writer, *value, k));
            let coded = writer.finish();
            for cut in 0..=coded.len() {
                let (mut whole, mut by_field) = (BitReader::of(&coded[..cut]), BitReader::of(&coded[..cut]));
                for _ in 0..values.len() {
                    let mut quotient = 0;
                    while quotient < RICE_ESCAPE && by_field.read(1) == 1 {
                        quotient += 1;
                    }
                    let field = if quotient == RICE_ESCAPE {
                        by_field.read(64)
                    } else {
                        u64::from(quotient) << k | by_field.read(k)
                    };
                    assert_eq!(read_rice(&mut whole, k), field, "k {k}, cut at {cut}");
                    assert_eq!(whole.short(), by_field.short(), "k {k}, cut at {cut}");
                }
                assert_eq!(whole.finished(), by_field.finished(), "k {k}, cut at {cut}");
            }
        }
    }

    #[test]
    fn the_cheapest_parameter_is_priced_and_not_guessed_from_the_mean() {
        let mut values = vec![0_u64; 14];
        values.extend([1_000_000, 1_000_000]);
        assert_eq!(RiceCost::of(&values).cheapest(), (0, 206));
        assert_eq!(RiceCost::of(&values).bits(16), 302);
        assert_eq!(RiceCost::of(&[]).cheapest(), (0, 0));
        let steady: Vec<u64> = (0..100).map(|n| 1000 + n % 7).collect();
        assert_eq!(RiceCost::of(&steady).cheapest().0, 9);
    }

    #[test]
    fn bits_read_past_their_end_are_zeros_and_said() {
        let mut reader = BitReader::of(&[0xff]);
        assert_eq!((reader.read(8), reader.short()), (0xff, false));
        assert_eq!((reader.read(3), reader.short()), (0, true));
        let mut reader = BitReader::of(&[0xff; 3]);
        read_rice(&mut reader, 0);
        assert!(reader.short());
    }

    #[test]
    fn fields_of_every_width_read_back_and_end_on_zero_padding() {
        let fields: Vec<(u64, u32)> = (0..=64_u32)
            .flat_map(|width| {
                let most = if width == 64 { u64::MAX } else { (1_u64 << width) - 1 };
                [(most, width), (most / 3, width), (0, width)]
            })
            .collect();
        let mut writer = BitWriter::onto(Vec::new());
        fields.iter().for_each(|&(value, width)| writer.write(value, width));
        let bytes = writer.finish();
        let mut reader = BitReader::of(&bytes);
        for &(value, width) in &fields {
            assert_eq!(reader.read(width), value, "{width}");
        }
        assert!(reader.finished());
    }

    #[test]
    fn a_reader_that_left_a_field_or_found_padding_not_zero_has_not_finished() {
        let mut packed = Vec::new();
        pack(&mut packed, [5, 6, 7].into_iter(), 3);
        let mut early = BitReader::of(&packed);
        early.read(3);
        assert!(!early.finished(), "two fields are left");
        let mut padded = BitReader::of(&[0b1000_0000]);
        padded.read(3);
        assert!(!padded.finished(), "padding that is not zero");
        // a field read inside the last byte's padding reads its zeros; one
        // past the last byte is short
        let mut past = BitReader::of(&packed);
        for _ in 0..6 {
            past.read(3);
        }
        assert!(past.short() && !past.finished(), "a sixth field ends past the bytes");
    }
}