tinystore 0.9.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
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Exact sums: every finite float is a whole number of units of 2⁻¹⁰⁷⁴, the
//! least subnormal, so a sum of them is an integer, kept in a fixed
//! accumulator of 35 words and rounded to a float once, rather than a big
//! integer made of each sample.
//!
//! ```text
//! 0.1 + 0.2       as floats 0.30000000000000004
//!                 exactly   0.3000000000000000166533453693773481063544750213623046875
//!                           rounded once: 0.30000000000000004
//! 1e308 + 1e308 − 1e308     as floats inf, exactly 1e308
//! ```
//!
//! The largest float is under 2¹⁰²⁴, 2²⁰⁹⁸ units, so 35 words of 64 bits hold
//! a sum of 2⁶³ of them, times the thousand a rate multiplies it by, with
//! room to spare.

use std::cmp::Ordering;

use crate::compress::varint::{Reader, put_count};

const WORDS: usize = 35;
/// Where the units' point lies: a unit is 2⁻¹⁰⁷⁴, so 1 is 2¹⁰⁷⁴ of them.
const POINT: u32 = 1074;

/// The bits a block's exact sum of at most 320 finite floats needs: 320 is
/// under 2⁹, and a float under 2²⁰⁹⁸ units.
pub(super) const BLOCK_BITS: u32 = 2107;
const _: () = assert!(super::codec::MOST_SAMPLES < 1 << (BLOCK_BITS - 2098), "a block's sum fits its bits");

/// An integer of units of 2⁻¹⁰⁷⁴, in two's complement, its lowest word first.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) struct Exact([u64; WORDS]);

impl Default for Exact {
    fn default() -> Self {
        Exact::ZERO
    }
}

impl std::fmt::Debug for Exact {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Exact({})", self.round())
    }
}

impl Exact {
    pub(super) const ZERO: Exact = Exact([0; WORDS]);

    /// A finite float's units; none for an infinity or a NaN.
    #[cfg(test)]
    pub(super) fn of(value: f64) -> Option<Exact> {
        let mut exact = Exact::ZERO;
        exact.add_float(value).then_some(exact)
    }

    /// Adds a float's units, or says it has none.
    pub(super) fn add_float(&mut self, value: f64) -> bool {
        let Some((mantissa, shift)) = units(value) else { return false };
        if value.is_sign_negative() {
            self.sub_shifted(mantissa, shift)
        } else {
            self.add_shifted(mantissa, shift)
        }
        true
    }

    /// Takes a float's units away, or says it has none.
    pub(super) fn sub_float(&mut self, value: f64) -> bool {
        self.add_float(-value)
    }

    /// A count's units: `count` × 2¹⁰⁷⁴.
    pub(super) fn of_whole(count: u64) -> Exact {
        let mut exact = Exact::ZERO;
        exact.add_shifted(count, POINT);
        exact
    }

    /// The whole number it is, when it is one from 0 to 2⁶⁴ − 1: a count.
    pub(super) fn whole(&self) -> Option<u64> {
        if self.is_negative() || low_bits_set(&self.0, POINT) {
            return None;
        }
        let whole = shift_right(&self.0, POINT);
        whole[1..].iter().all(|word| *word == 0).then_some(whole[0])
    }

    /// It times `factor`, for a count that leaves the room: a count of 2⁶⁴
    /// times 10¹⁷ is under 2¹²¹ units past the point, far below the sign.
    pub(super) fn times(&self, factor: u64) -> Exact {
        let mut words = self.magnitude();
        multiply(&mut words, factor);
        let product = Exact(words);
        debug_assert!(!product.is_negative(), "a product past the accumulator");
        if self.is_negative() {
            let mut negative = Exact::ZERO;
            negative.sub(&product);
            return negative;
        }
        product
    }

    pub(super) fn add(&mut self, other: &Exact) {
        let mut carry = false;
        for (word, more) in self.0.iter_mut().zip(other.0) {
            let (sum, over) = word.overflowing_add(more);
            let (sum, again) = sum.overflowing_add(u64::from(carry));
            (*word, carry) = (sum, over || again);
        }
    }

    pub(super) fn sub(&mut self, other: &Exact) {
        let mut borrow = false;
        for (word, less) in self.0.iter_mut().zip(other.0) {
            let (difference, under) = word.overflowing_sub(less);
            let (difference, again) = difference.overflowing_sub(u64::from(borrow));
            (*word, borrow) = (difference, under || again);
        }
    }

    /// `mantissa << shift` added: a mantissa of 64 bits at most, which
    /// shifted by under a word fits the 128 bits it is carried in.
    fn add_shifted(&mut self, mantissa: u64, shift: u32) {
        let (word, bits) = ((shift / 64) as usize, shift % 64);
        let wide = u128::from(mantissa) << bits;
        let (low, over) = self.0[word].overflowing_add(wide as u64);
        self.0[word] = low;
        let (high, mut carry) = self.0[word + 1].overflowing_add((wide >> 64) as u64);
        let (high, again) = high.overflowing_add(u64::from(over));
        self.0[word + 1] = high;
        carry |= again;
        for next in &mut self.0[word + 2..] {
            if !carry {
                break;
            }
            (*next, carry) = next.overflowing_add(1);
        }
    }

    fn sub_shifted(&mut self, mantissa: u64, shift: u32) {
        let (word, bits) = ((shift / 64) as usize, shift % 64);
        let wide = u128::from(mantissa) << bits;
        let (low, under) = self.0[word].overflowing_sub(wide as u64);
        self.0[word] = low;
        let (high, mut borrow) = self.0[word + 1].overflowing_sub((wide >> 64) as u64);
        let (high, again) = high.overflowing_sub(u64::from(under));
        self.0[word + 1] = high;
        borrow |= again;
        for next in &mut self.0[word + 2..] {
            if !borrow {
                break;
            }
            (*next, borrow) = next.overflowing_sub(1);
        }
    }

    pub(super) fn is_zero(&self) -> bool {
        self.0.iter().all(|word| *word == 0)
    }

    pub(super) fn is_negative(&self) -> bool {
        self.0[WORDS - 1] >> 63 == 1
    }

    /// Its absolute value, as words.
    fn magnitude(&self) -> [u64; WORDS] {
        let mut magnitude = *self;
        if self.is_negative() {
            magnitude = Exact::ZERO;
            magnitude.sub(self);
        }
        magnitude.0
    }

    /// The float nearest it, a tie to the even one: an infinity past the
    /// largest float.
    pub(super) fn round(&self) -> f64 {
        signed(self.is_negative(), to_float(&self.magnitude(), Remainder::None))
    }

    /// It divided by `divisor` and rounded once: a mean.
    pub(super) fn divided(&self, divisor: u64) -> f64 {
        self.scaled(1, divisor)
    }

    /// It times `factor` over `divisor`, rounded once: a rate, a thousand
    /// over a bucket's milliseconds.
    pub(super) fn scaled(&self, factor: u64, divisor: u64) -> f64 {
        if divisor == 0 {
            return f64::NAN;
        }
        let mut words = self.magnitude();
        multiply(&mut words, factor);
        let remainder = divide(&mut words, divisor);
        let rest = match (2 * u128::from(remainder)).cmp(&u128::from(divisor)) {
            _ if remainder == 0 => Remainder::None,
            Ordering::Less => Remainder::BelowHalf,
            Ordering::Equal => Remainder::Half,
            Ordering::Greater => Remainder::AboveHalf,
        };
        signed(self.is_negative(), to_float(&words, rest))
    }

    /// Appends it canonically: its magnitude's bytes after its trailing zero
    /// bits are taken out, behind their count and a token of how many were
    /// taken and its sign; zero is the count 0 alone.
    pub(super) fn put(&self, out: &mut Vec<u8>) {
        let magnitude = self.magnitude();
        let Some(low) = magnitude.iter().position(|word| *word != 0) else { return out.push(0) };
        let zeros = low as u32 * 64 + magnitude[low].trailing_zeros();
        let shifted = shift_right(&magnitude, zeros);
        let bits = bit_length(&shifted);
        let bytes = bits.div_ceil(8) as usize;
        put_count(out, bytes as u64);
        put_count(out, (u64::from(zeros) << 1) | u64::from(self.is_negative()));
        out.extend((0..bytes).rev().map(|place| (shifted[place / 8] >> (place % 8 * 8)) as u8));
    }

    /// Reads what [`Exact::put`] wrote, of at most `most_bits`, refusing any
    /// other spelling of the same number.
    pub(super) fn read(reader: &mut Reader<'_>, most_bits: u32) -> Exact {
        let bytes = reader.count(most_bits.div_ceil(8) as usize);
        if bytes == 0 {
            return Exact::ZERO;
        }
        let token = reader.uvarint();
        let zeros = token >> 1;
        let magnitude = reader.take(bytes);
        if magnitude.len() != bytes || zeros >= u64::from(most_bits) {
            reader.fail("an exact sum past its bound");
            return Exact::ZERO;
        }
        if magnitude[0] == 0 || magnitude[bytes - 1] & 1 == 0 {
            reader.fail("an exact sum not written as its one spelling");
            return Exact::ZERO;
        }
        let mut words = [0_u64; WORDS];
        for (place, byte) in magnitude.iter().rev().enumerate() {
            words[place / 8] |= u64::from(*byte) << (place % 8 * 8);
        }
        let bits = bit_length(&words) + zeros as u32;
        if bits > most_bits {
            reader.fail("an exact sum past its bound");
            return Exact::ZERO;
        }
        let mut exact = Exact(shift_left(&words, zeros as u32));
        if token & 1 == 1 {
            let positive = exact;
            exact = Exact::ZERO;
            exact.sub(&positive);
        }
        exact
    }
}

impl PartialOrd for Exact {
    fn partial_cmp(&self, other: &Exact) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Exact {
    fn cmp(&self, other: &Exact) -> Ordering {
        match (self.is_negative(), other.is_negative()) {
            (true, false) => Ordering::Less,
            (false, true) => Ordering::Greater,
            _ => self.0.iter().rev().cmp(other.0.iter().rev()),
        }
    }
}

/// A sum of floats kept exactly and rounded once, as an aggregate of the
/// store sums its samples: for a program that sums what it never writes and
/// wants the number a query of the same samples gives.
///
/// ```
/// use tinystore::metrics::ExactSum;
/// let tenths: ExactSum = [0.1; 10].into_iter().collect();
/// assert_eq!(tenths.value(), 1.0, "where a float sum gives 0.9999999999999999");
/// let mut sum = ExactSum::default();
/// [1e308, 1e308, -1e308].into_iter().for_each(|value| sum.add(value));
/// assert_eq!(sum.value(), 1e308, "and no infinity on the way");
/// ```
///
/// An infinity or a NaN added makes the sum what a float sum makes it, since
/// it has no exact value; past what a float holds the value is an infinity.
#[derive(Clone, Copy, Default, PartialEq)]
pub struct ExactSum {
    finite: Exact,
    /// The infinities and NaNs added, summed as floats: 0 while there is none.
    beyond: f64,
}

impl ExactSum {
    pub fn add(&mut self, value: f64) {
        if !self.finite.add_float(value) {
            self.beyond += value;
        }
    }

    /// Takes `value` away, exactly: a sum less what one series gave it.
    pub fn sub(&mut self, value: f64) {
        self.add(-value);
    }

    /// Adds another sum, exactly: a group's sum of its series' sums.
    pub fn merge(&mut self, other: &ExactSum) {
        self.finite.add(&other.finite);
        self.beyond += other.beyond;
    }

    /// The float nearest the sum, a tie to the even one.
    pub fn value(&self) -> f64 {
        if self.beyond != 0.0 { self.beyond } else { self.finite.round() }
    }
}

impl std::fmt::Debug for ExactSum {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ExactSum({})", self.value())
    }
}

impl Extend<f64> for ExactSum {
    fn extend<I: IntoIterator<Item = f64>>(&mut self, values: I) {
        values.into_iter().for_each(|value| self.add(value));
    }
}

impl FromIterator<f64> for ExactSum {
    fn from_iter<I: IntoIterator<Item = f64>>(values: I) -> ExactSum {
        let mut sum = ExactSum::default();
        sum.extend(values);
        sum
    }
}

/// A finite float's mantissa and where it stands in units: a normal float's
/// 53 bits shifted by its exponent less one, a subnormal's as they are.
fn units(value: f64) -> Option<(u64, u32)> {
    let bits = value.to_bits();
    let exponent = ((bits >> 52) & 0x7ff) as u32;
    let fraction = bits & ((1 << 52) - 1);
    match exponent {
        0x7ff => None,
        0 => Some((fraction, 0)),
        _ => Some((fraction | (1 << 52), exponent - 1)),
    }
}

/// What a quotient left below its last whole unit.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Remainder {
    None,
    BelowHalf,
    Half,
    AboveHalf,
}

/// The magnitude `words` units, and a fraction of a unit `rest`, as the
/// nearest float, a tie to the even one.
///
/// A positive float's bits read as an integer grow with it, so its bits are
/// `shift << 52` plus its 53-bit mantissa: a mantissa that rounds up to 2⁵³
/// carries into the exponent by itself, and one past the largest exponent is
/// an infinity.
fn to_float(words: &[u64; WORDS], rest: Remainder) -> f64 {
    let length = bit_length(words);
    let shift = length.saturating_sub(53);
    let mantissa = shift_right(words, shift)[0];
    let up = if shift == 0 {
        match rest {
            Remainder::AboveHalf => true,
            Remainder::Half => mantissa & 1 == 1,
            Remainder::None | Remainder::BelowHalf => false,
        }
    } else {
        let dropped_high = bit(words, shift - 1);
        let dropped_low = low_bits_set(words, shift - 1) || rest != Remainder::None;
        dropped_high && (dropped_low || mantissa & 1 == 1)
    };
    let bits = (u64::from(shift) << 52) + mantissa + u64::from(up);
    if bits >= f64::INFINITY.to_bits() { f64::INFINITY } else { f64::from_bits(bits) }
}

fn signed(negative: bool, magnitude: f64) -> f64 {
    if negative { -magnitude } else { magnitude }
}

fn bit_length(words: &[u64; WORDS]) -> u32 {
    words.iter().rposition(|word| *word != 0).map_or(0, |top| top as u32 * 64 + 64 - words[top].leading_zeros())
}

fn bit(words: &[u64; WORDS], at: u32) -> bool {
    words[(at / 64) as usize] >> (at % 64) & 1 == 1
}

/// Whether any bit below `below` is set.
fn low_bits_set(words: &[u64; WORDS], below: u32) -> bool {
    let (whole, part) = ((below / 64) as usize, below % 64);
    words[..whole].iter().any(|word| *word != 0) || (part > 0 && words[whole] & ((1 << part) - 1) != 0)
}

fn shift_right(words: &[u64; WORDS], by: u32) -> [u64; WORDS] {
    let (whole, part) = ((by / 64) as usize, by % 64);
    let mut out = [0_u64; WORDS];
    for place in 0..WORDS.saturating_sub(whole) {
        let low = words[place + whole] >> part;
        let high = if part > 0 && place + whole + 1 < WORDS { words[place + whole + 1] << (64 - part) } else { 0 };
        out[place] = low | high;
    }
    out
}

fn shift_left(words: &[u64; WORDS], by: u32) -> [u64; WORDS] {
    let (whole, part) = ((by / 64) as usize, by % 64);
    let mut out = [0_u64; WORDS];
    for place in whole..WORDS {
        let high = words[place - whole] << part;
        let low = if part > 0 && place > whole { words[place - whole - 1] >> (64 - part) } else { 0 };
        out[place] = high | low;
    }
    out
}

fn multiply(words: &mut [u64; WORDS], factor: u64) {
    let mut carry = 0_u128;
    for word in words.iter_mut() {
        let product = u128::from(*word) * u128::from(factor) + carry;
        *word = product as u64;
        carry = product >> 64;
    }
}

/// Divides in place and gives the remainder.
fn divide(words: &mut [u64; WORDS], divisor: u64) -> u64 {
    let mut remainder = 0_u128;
    for word in words.iter_mut().rev() {
        let dividend = (remainder << 64) | u128::from(*word);
        *word = (dividend / u128::from(divisor)) as u64;
        remainder = dividend % u128::from(divisor);
    }
    remainder as u64
}

#[cfg(test)]
#[path = "exact_tests.rs"]
mod tests;