tinystore 0.10.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! What the messages crates/protocol writes read their fields with and write
//! them with: a function a type of the schema, each the one place its rule
//! lives. A message is read straight from its body, a field at a time, and
//! written straight into the bytes that carry it, with no tree of values
//! between:
//!
//! ```text
//! kv.Call { handle: 7, key: "a" }   →   82 01 07 03 a1 61
//!                                        │  └─┬─┘ └──┬───┘
//!                                        │    1: 7   3: "a"
//!                                        └ a map of 2, its count written last
//! ```

use std::collections::BTreeMap;

use super::msgpack::{self, Reader, Refused, Token};
use super::protocol::Failure;
use crate::limits::Limit;

/// A message of the schema: read whole or refused, and written with every
/// field at its zero value left out.
pub(crate) trait Message: Sized + Default {
    const NAME: &'static str;

    /// Reads the message whose map comes next.
    fn read(r: &mut Reader<'_>) -> Result<Self, Failure>;

    /// Writes the message's map at the end of `out`.
    fn write(&self, out: &mut Vec<u8>);

    /// Whether every field is at its zero value and every optional one absent,
    /// which leaves the message out where a field holds it, as an empty map.
    fn is_zero(&self) -> bool;

    /// The most bytes the message writes as, which `encode` reserves: a
    /// buffer that grew as it was written was a realloc, which takes the
    /// allocator's lock, and an answer's every write on sixteen workers made
    /// them wait for it.
    fn size(&self) -> usize;

    fn decode(body: &[u8]) -> Result<Self, Failure> {
        let mut r = Reader::new(body);
        let message = Self::read(&mut r)?;
        r.end()?;
        Ok(message)
    }

    fn encode(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(self.size());
        self.write(&mut out);
        out
    }
}

/// A kv row as it travels: nothing, an integer or bytes.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) enum Row {
    #[default]
    Nil,
    Int(i64),
    Bin(Vec<u8>),
}

/// A value as SQLite keeps it, as it travels: nothing, an integer, a float,
/// text or bytes. A bool from a client is the integer SQLite keeps for it.
#[cfg(any(feature = "sql", feature = "search"))]
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) enum Cell {
    #[default]
    Nil,
    Int(i64),
    Float(f64),
    Str(String),
    Bin(Vec<u8>),
}

impl From<Refused> for Failure {
    /// Bytes that are not the profile's, refused for the rule they break.
    fn from(refused: Refused) -> Failure {
        Failure::invalid(refused.0)
    }
}

/// A message's map as it is written. Its count is known only once the
/// fields are, since a field at its zero value is left out, so the byte a
/// fixmap's count takes is kept, and filled when the map closes.
pub(crate) struct Map {
    head: usize,
    count: usize,
}

impl Map {
    pub(crate) fn open(out: &mut Vec<u8>) -> Map {
        out.push(0x80);
        Map { head: out.len() - 1, count: 0 }
    }

    /// Writes a field's number, one byte as every number of the schema is;
    /// its value comes next.
    pub(crate) fn field(&mut self, out: &mut Vec<u8>, number: u8) {
        out.push(number);
        self.count += 1;
    }

    /// Writes the count in its byte. Past fifteen fields the count is a
    /// map 16's three bytes, which the fields move up to make room for: the
    /// profile writes the shortest head, so none is kept wider in advance.
    pub(crate) fn close(self, out: &mut Vec<u8>) {
        if self.count <= 15 {
            out[self.head] = 0x80 | self.count as u8;
        } else {
            let count = u16::try_from(self.count).expect("a schema numbers its fields below 128");
            let [high, low] = count.to_be_bytes();
            out.splice(self.head..=self.head, [0xde, high, low]);
        }
    }
}

/// How many fields the message `name` has, refusing a body that is not a map.
pub(crate) fn fields(r: &mut Reader<'_>, name: &str) -> Result<usize, Failure> {
    match r.next()? {
        Token::Map(count) => Ok(count),
        _ => Err(Failure::invalid(format!("a {name} that is not a map"))),
    }
}

/// The number of the message `name`'s next field. A field named rather than
/// numbered is refused, and a number twice: `seen` keeps the numbers read,
/// which a schema keeps below 128.
pub(crate) fn field(r: &mut Reader<'_>, name: &str, seen: &mut u128) -> Result<u64, Failure> {
    match r.next()? {
        Token::Uint(number) => {
            if let Some(bit) = 1u128.checked_shl(u32::try_from(number).unwrap_or(u32::MAX)) {
                if *seen & bit != 0 {
                    return Err(Failure::invalid("a key twice"));
                }
                *seen |= bit;
            }
            Ok(number)
        }
        Token::Str(_) => Err(Failure::invalid(format!("a {name} with a field named rather than numbered"))),
        _ => Err(Failure::invalid("a key that is neither an unsigned integer nor a name")),
    }
}

/// A field the message `name` lacks, from a newer client: named with this
/// server's version, so that the client knows which server to upgrade.
pub(crate) fn unknown(number: u64, name: &str) -> Failure {
    Failure::unimplemented(format!("field {number} of {name}"))
}

/// Reads past the value of a field the message `name` lacks, well formed and
/// nested no deeper than the profile's eight levels: what a server reading a
/// newer client's HELLO does with a field it does not know.
pub(crate) fn skip(r: &mut Reader<'_>, name: &str) -> Result<(), Failure> {
    skip_at(r, name, 1)
}

fn skip_at(r: &mut Reader<'_>, name: &str, depth: usize) -> Result<(), Failure> {
    if depth > 8 {
        return Err(Failure::invalid(format!("a field of {name} nested past eight levels")));
    }
    match r.next()? {
        Token::Array(count) => {
            for _ in 0..count {
                skip_at(r, name, depth + 1)?;
            }
        }
        Token::Map(count) => {
            for _ in 0..count {
                if !matches!(r.next()?, Token::Uint(_) | Token::Str(_)) {
                    return Err(Failure::invalid("a key that is neither an unsigned integer nor a name"));
                }
                skip_at(r, name, depth + 1)?;
            }
        }
        _ => {}
    }
    Ok(())
}

pub(crate) fn bool(r: &mut Reader<'_>, name: &str) -> Result<bool, Failure> {
    match r.next()? {
        Token::Bool(flag) => Ok(flag),
        _ => Err(not(name, "a bool")),
    }
}

pub(crate) fn uint(r: &mut Reader<'_>, name: &str) -> Result<u64, Failure> {
    match r.next()? {
        Token::Uint(number) => Ok(number),
        _ => Err(not(name, "an unsigned integer")),
    }
}

pub(crate) fn int(r: &mut Reader<'_>, name: &str) -> Result<i64, Failure> {
    match r.next()? {
        Token::Uint(number) => i64::try_from(number).map_err(|_| not(name, "an integer")),
        Token::Int(number) => Ok(number),
        _ => Err(not(name, "an integer")),
    }
}

/// A float, or an integer a float holds exactly, as JavaScript writes 3.0.
#[cfg_attr(
    not(any(feature = "records", feature = "metrics", feature = "search")),
    expect(dead_code, reason = "only records', metrics' and search's messages have a float field")
)]
pub(crate) fn float(r: &mut Reader<'_>, name: &str) -> Result<f64, Failure> {
    let float = match r.next()? {
        Token::Float(float) => Some(float),
        Token::Uint(number) => {
            Some(number as f64).filter(|float| *float as u64 == number && *float < 18_446_744_073_709_551_616.0)
        }
        Token::Int(number) => Some(number as f64).filter(|float| *float as i64 == number),
        _ => None,
    };
    float.ok_or_else(|| not(name, "a float"))
}

pub(crate) fn str(r: &mut Reader<'_>, name: &str) -> Result<String, Failure> {
    match r.next()? {
        Token::Str(text) => Ok(text.to_owned()),
        _ => Err(not(name, "a str")),
    }
}

pub(crate) fn bin(r: &mut Reader<'_>, name: &str) -> Result<Vec<u8>, Failure> {
    match r.next()? {
        Token::Bin(bytes) => Ok(bytes.to_vec()),
        _ => Err(not(name, "a bin")),
    }
}

/// A key's text: a str, a bin that is UTF-8, or an integer's decimal spelling.
pub(crate) fn key(r: &mut Reader<'_>, name: &str) -> Result<String, Failure> {
    match r.next()? {
        Token::Str(text) => Ok(text.to_owned()),
        Token::Uint(number) => Ok(number.to_string()),
        Token::Int(number) => Ok(number.to_string()),
        Token::Bin(bytes) => String::from_utf8(bytes.to_vec())
            .map_err(|_| Failure::invalid(format!("{name} of bytes that are not UTF-8"))),
        _ => Err(not(name, "a str, a bin or an integer")),
    }
}

pub(crate) fn row(r: &mut Reader<'_>, name: &str) -> Result<Row, Failure> {
    match r.next()? {
        Token::Nil => Ok(Row::Nil),
        Token::Uint(number) => {
            i64::try_from(number).map(Row::Int).map_err(|_| Failure::invalid(format!("{name} past an int 64")))
        }
        Token::Int(number) => Ok(Row::Int(number)),
        Token::Bin(bytes) => Ok(Row::Bin(bytes.to_vec())),
        _ => Err(not(name, "nil, an integer or a bin")),
    }
}

#[cfg(any(feature = "sql", feature = "search"))]
pub(crate) fn cell(r: &mut Reader<'_>, name: &str) -> Result<Cell, Failure> {
    match r.next()? {
        Token::Nil => Ok(Cell::Nil),
        Token::Bool(flag) => Ok(Cell::Int(i64::from(flag))),
        Token::Uint(number) => {
            i64::try_from(number).map(Cell::Int).map_err(|_| Failure::invalid(format!("{name} past an int 64")))
        }
        Token::Int(number) => Ok(Cell::Int(number)),
        Token::Float(number) => Ok(Cell::Float(number)),
        Token::Str(text) => Ok(Cell::Str(text.to_owned())),
        Token::Bin(bytes) => Ok(Cell::Bin(bytes.to_vec())),
        _ => Err(not(name, "nil, a bool, a number, a str or a bin")),
    }
}

/// Takes the room `count` items of `each` bytes need in memory, or refuses
/// the list `name` as `limit` before any of it is made: a body of empty
/// items would otherwise grow a hundredfold as it is read.
fn room(r: &mut Reader<'_>, name: &str, count: usize, each: usize) -> Result<(), Failure> {
    let needed = count.saturating_mul(each);
    let room = Limit { name: "body memory", bound: r.room() as u64 };
    if r.take_room(needed) {
        return Ok(());
    }
    let refused = format!("{name}: {count} items, {needed} bytes in memory, past what a body of its size may take");
    Err(Failure::from(room.refuse(needed as u64, refused)))
}

pub(crate) fn list<T>(
    r: &mut Reader<'_>,
    name: &str,
    item: impl Fn(&mut Reader<'_>, &str) -> Result<T, Failure>,
) -> Result<Vec<T>, Failure> {
    let Token::Array(count) = r.next()? else {
        return Err(not(name, "an array"));
    };
    // the reader has checked the count against the bytes left, each item a byte at least
    room(r, name, count, size_of::<T>())?;
    let mut items = Vec::with_capacity(count);
    for _ in 0..count {
        items.push(item(r, name)?);
    }
    Ok(items)
}

pub(crate) fn names<T>(
    r: &mut Reader<'_>,
    name: &str,
    item: impl Fn(&mut Reader<'_>, &str) -> Result<T, Failure>,
) -> Result<BTreeMap<String, T>, Failure> {
    let Token::Map(count) = r.next()? else {
        return Err(not(name, "a map of names"));
    };
    room(r, name, count, size_of::<(String, T)>())?;
    let mut named = BTreeMap::new();
    for _ in 0..count {
        let key = match r.next()? {
            Token::Str(key) => key.to_owned(),
            Token::Uint(_) => return Err(not(name, "a map whose keys are names")),
            _ => return Err(Failure::invalid("a key that is neither an unsigned integer nor a name")),
        };
        if named.insert(key, item(r, name)?).is_some() {
            return Err(Failure::invalid("a key twice"));
        }
    }
    Ok(named)
}

pub(crate) fn message<M: Message>(r: &mut Reader<'_>, _name: &str) -> Result<M, Failure> {
    M::read(r)
}

pub(crate) fn write_bool(out: &mut Vec<u8>, flag: &bool) {
    msgpack::write_bool(out, *flag);
}

pub(crate) fn write_uint(out: &mut Vec<u8>, number: &u64) {
    msgpack::write_uint(out, *number);
}

/// An integer as the profile writes it: unsigned when it is not negative.
pub(crate) fn write_int(out: &mut Vec<u8>, number: &i64) {
    msgpack::write_int(out, *number);
}

#[cfg_attr(
    not(any(feature = "records", feature = "metrics", feature = "search")),
    expect(dead_code, reason = "only records', metrics' and search's messages have a float field")
)]
pub(crate) fn write_float(out: &mut Vec<u8>, float: &f64) {
    msgpack::write_float(out, *float);
}

pub(crate) fn write_str(out: &mut Vec<u8>, text: &impl AsRef<str>) {
    msgpack::write_str(out, text.as_ref());
}

pub(crate) fn write_bin(out: &mut Vec<u8>, bytes: &impl AsRef<[u8]>) {
    msgpack::write_bin(out, bytes.as_ref());
}

/// Writes a bin of `length` bytes as `write_bin` does, its bytes written in
/// place by `fill`: a file's bytes are read straight into the message that
/// carries them, rather than into bytes of their own and then copied.
#[cfg(feature = "blobs")]
pub(crate) fn write_bin_in_place<E>(
    out: &mut Vec<u8>,
    length: usize,
    fill: impl FnOnce(&mut [u8]) -> Result<(), E>,
) -> Result<(), E> {
    msgpack::write_bin_head(out, length);
    let start = out.len();
    out.resize(start + length, 0);
    fill(&mut out[start..])
}

pub(crate) fn write_row(out: &mut Vec<u8>, row: &Row) {
    match row {
        Row::Nil => msgpack::write_nil(out),
        Row::Int(number) => msgpack::write_int(out, *number),
        Row::Bin(bytes) => msgpack::write_bin(out, bytes),
    }
}

#[cfg(any(feature = "sql", feature = "search"))]
pub(crate) fn write_cell(out: &mut Vec<u8>, cell: &Cell) {
    match cell {
        Cell::Nil => msgpack::write_nil(out),
        Cell::Int(number) => msgpack::write_int(out, *number),
        Cell::Float(number) => msgpack::write_float(out, *number),
        Cell::Str(text) => msgpack::write_str(out, text),
        Cell::Bin(bytes) => msgpack::write_bin(out, bytes),
    }
}

pub(crate) fn write_list<T>(out: &mut Vec<u8>, items: &[T], item: impl Fn(&mut Vec<u8>, &T)) {
    msgpack::write_array_head(out, items.len());
    for each in items {
        item(out, each);
    }
}

/// Names in the byte order of their text, which a map's canonical order is.
pub(crate) fn write_names<T>(out: &mut Vec<u8>, named: &BTreeMap<String, T>, item: impl Fn(&mut Vec<u8>, &T)) {
    msgpack::write_map_head(out, named.len());
    for (name, each) in named {
        msgpack::write_str(out, name);
        item(out, each);
    }
}

pub(crate) fn write_message<M: Message>(out: &mut Vec<u8>, message: &M) {
    message.write(out);
}

/// The most bytes a row writes as: a bin's head of five and its bytes, or an
/// integer's nine.
pub(crate) fn row_size(row: &Row) -> usize {
    match row {
        Row::Bin(bytes) => 5 + bytes.len(),
        Row::Nil | Row::Int(_) => 9,
    }
}

/// The most bytes a cell writes as: a head of five and its text or bytes, or
/// a number's nine.
#[cfg(any(feature = "sql", feature = "search"))]
pub(crate) fn cell_size(cell: &Cell) -> usize {
    match cell {
        Cell::Str(text) => 5 + text.len(),
        Cell::Bin(bytes) => 5 + bytes.len(),
        Cell::Nil | Cell::Int(_) | Cell::Float(_) => 9,
    }
}

pub(crate) fn list_size<T>(items: &[T], item: impl Fn(&T) -> usize) -> usize {
    5 + items.iter().map(item).sum::<usize>()
}

pub(crate) fn names_size<T>(named: &BTreeMap<String, T>, item: impl Fn(&T) -> usize) -> usize {
    5 + named.iter().map(|(name, each)| 5 + name.len() + item(each)).sum::<usize>()
}

pub(crate) fn message_size<M: Message>(message: &M) -> usize {
    message.size()
}

/// Whether a row writes as a zero value of the profile, which a field that
/// holds it leaves out: nil, the integer 0 or no bytes.
#[expect(dead_code, reason = "a field that is not optional holds a row with it, and every row field is optional yet")]
pub(crate) fn row_is_zero(row: &Row) -> bool {
    match row {
        Row::Nil | Row::Int(0) => true,
        Row::Int(_) => false,
        Row::Bin(bytes) => bytes.is_empty(),
    }
}

/// Whether a cell writes as a zero value of the profile: nil, the integer 0,
/// a float whose bits are all 0, no text or no bytes.
#[cfg(any(feature = "sql", feature = "search"))]
#[expect(dead_code, reason = "a field that is not optional holds a cell with it, and cells are in lists yet")]
pub(crate) fn cell_is_zero(cell: &Cell) -> bool {
    match cell {
        Cell::Nil | Cell::Int(0) => true,
        Cell::Int(_) => false,
        Cell::Float(number) => number.to_bits() == 0,
        Cell::Str(text) => text.is_empty(),
        Cell::Bin(bytes) => bytes.is_empty(),
    }
}

fn not(name: &str, kind: &str) -> Failure {
    Failure::invalid(format!("{name} is not {kind}"))
}

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