tinystore 0.4.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
//! A backup of a whole store and its restore: every engine back as it was,
//! every byte checked, the program's files and the store's key only when
//! asked for, and nothing left by a backup or a restore that fails.

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use super::*;
use crate::metrics::Series;
use crate::text::hex;
use crate::{ErrorKind, Options, TestClock, sql};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Email {
    to: String,
}

const NOTES: &str = "create table notes (id integer primary key, text text not null) strict";
/// Bytes past what blobs keeps inline, so that they are a file of `objects/`.
const PHOTO: usize = 100 << 10;

struct Fixture {
    _dirs: tempfile::TempDir,
    data: PathBuf,
    zip: PathBuf,
    restored: PathBuf,
    clock: Arc<TestClock>,
}

fn fixture() -> Fixture {
    let dirs = tempfile::tempdir().unwrap();
    let (data, zip, restored) =
        (dirs.path().join("data"), dirs.path().join("backup.zip"), dirs.path().join("restored"));
    let clock = Arc::new(TestClock::new(UNIX_EPOCH + Duration::from_secs(1_800_000_000)));
    Fixture { _dirs: dirs, data, zip, restored, clock }
}

impl Fixture {
    fn open(&self, dir: &Path) -> Store {
        Store::open(dir, Options { clock: Some(self.clock.clone()), background: false, ..Options::default() }).unwrap()
    }

    /// A store with something in every engine.
    fn filled(&self) -> Store {
        let store = self.open(&self.data);
        store.bucket::<String>("codes").open().unwrap().set("K7Q2", &"kept".to_owned()).unwrap();
        let emails = store.queue::<Email>("emails").open().unwrap();
        emails.id("e-1").add(&Email { to: "ann@example.com".to_owned() }).unwrap();
        let app = store.database("app").migrations([("0001_notes.sql", NOTES)]).open().unwrap();
        app.exec(sql!("insert into notes (text) values (?)", "a note")).unwrap();
        store.files("photos").open().unwrap().put("cat.jpg", photo()).unwrap();
        store.events("audit").open().unwrap().append("role.changed", &serde_json::json!({ "user": 42 })).unwrap();
        let sample = Series::gauge("temperature").label("room", "kitchen").sample(store.now(), 21.5);
        store.metrics().unwrap().ingest(&[sample]).unwrap();
        store.index::<String>("notes").open().unwrap().set("n-1", &"sourdough starter".to_owned()).unwrap();
        store
    }

    /// Checks that a store holds what `filled` wrote.
    fn holds_everything(&self, store: &Store) {
        assert_eq!(store.bucket::<String>("codes").open().unwrap().get("K7Q2").unwrap().as_deref(), Some("kept"));
        let email = store.queue::<Email>("emails").open().unwrap().get("e-1").unwrap().expect("the job kept");
        assert_eq!(email.value.to, "ann@example.com");
        let app = store.database("app").migrations([("0001_notes.sql", NOTES)]).open().unwrap();
        assert_eq!(app.scalar::<String>(sql!("select text from notes")).unwrap(), "a note");
        let mut cat = store.files("photos").open().unwrap().get("cat.jpg").unwrap().expect("the file kept");
        assert_eq!(cat.read_all().unwrap(), photo());
        let events = store.events("audit").open().unwrap().list().page().unwrap().events;
        assert_eq!(events.iter().map(|event| event.name.as_str()).collect::<Vec<_>>(), ["role.changed"]);
        let read = store.metrics().unwrap().query().name("temperature").since(Duration::from_secs(3600)).read();
        assert_eq!(read.unwrap().series[0].samples()[0].value, 21.5);
        let notes = store.index::<String>("notes").open().unwrap();
        let hits = notes.search("starter").run().unwrap().hits;
        assert_eq!(hits.iter().map(|hit| hit.id.as_str()).collect::<Vec<_>>(), ["n-1"]);
    }
}

fn photo() -> Vec<u8> {
    (0..PHOTO).map(|at| (at % 251) as u8).collect()
}

/// The names of what a directory holds, its folders' too, `/` between parts.
fn tree(dir: &Path) -> Vec<String> {
    let mut names = Vec::new();
    let mut folders = vec![dir.to_path_buf()];
    while let Some(folder) = folders.pop() {
        for entry in fs::read_dir(&folder).unwrap() {
            let path = entry.unwrap().path();
            let name = path.strip_prefix(dir).unwrap().to_string_lossy().replace('\\', "/");
            if path.is_dir() {
                folders.push(path);
            } else {
                names.push(name);
            }
        }
    }
    names.sort();
    names
}

// contract: BAK-01
#[test]
fn a_backup_restores_every_engine_while_its_store_keeps_working() {
    let f = fixture();
    let store = f.filled();
    let backup = store.backup().write(&f.zip).unwrap();
    assert_eq!((backup.key_left_out, backup.files), (false, 8), "an engine's file each, a database's, a photo's bytes");
    store.bucket::<String>("codes").open().unwrap().set("after", &"not in it".to_owned()).unwrap();
    assert!(!tree(&f.data).iter().any(|name| name.starts_with(".snapshot-")), "the snapshot is gone");
    drop(store);

    assert_eq!(restore(&f.zip, &f.restored).unwrap(), backup);
    let restored = f.open(&f.restored);
    f.holds_everything(&restored);
    assert_eq!(restored.bucket::<String>("codes").open().unwrap().get("after").unwrap(), None);
}

#[test]
fn a_restored_records_and_metrics_file_gives_its_free_pages_back_as_its_store_made_it() {
    let f = fixture();
    let store = f.filled();
    store.backup().write(&f.zip).unwrap();
    store.close().unwrap();
    restore(&f.zip, &f.restored).unwrap();
    for name in ["records.db", "metrics.db"] {
        let flags = rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY;
        let connection = rusqlite::Connection::open_with_flags(f.restored.join(name), flags).unwrap();
        let mode: i64 = connection.query_row("pragma auto_vacuum", [], |row| row.get(0)).unwrap();
        assert_eq!(mode, 2, "{name}: incremental, as its store made it");
    }
}

// contract: BAK-01
#[test]
fn an_engine_no_handle_opened_is_backed_up_from_its_files() {
    let f = fixture();
    f.filled().close().unwrap();
    let store = f.open(&f.data);
    let backup = store.backup().write(&f.zip).unwrap();
    assert_eq!(backup.files, 8);
    drop(store);
    restore(&f.zip, &f.restored).unwrap();
    f.holds_everything(&f.open(&f.restored));
}

// contract: BAK-02
#[test]
fn a_changed_byte_is_refused_and_leaves_nothing() {
    let f = fixture();
    f.filled().backup().write(&f.zip).unwrap();
    let mut zip = fs::read(&f.zip).unwrap();
    let at = zip.windows(4).position(|bytes| bytes == b"SQLi").expect("an SQLite file's header") + 200;
    zip[at] ^= 0xff;
    fs::write(&f.zip, &zip).unwrap();
    let error = restore(&f.zip, &f.restored).unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Corrupt, "{error}");
    assert!(tree(&f.restored).is_empty(), "{:?}", tree(&f.restored));
}

// contract: BAK-02
#[test]
fn a_restore_writes_into_an_empty_directory_and_none_other() {
    let f = fixture();
    f.filled().backup().write(&f.zip).unwrap();
    fs::create_dir_all(&f.restored).unwrap();
    fs::write(f.restored.join("left.txt"), "x").unwrap();
    let error = restore(&f.zip, &f.restored).unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
    assert_eq!(tree(&f.restored), ["left.txt"], "what was there stays");
}

// contract: BAK-03
#[test]
fn a_file_of_the_programs_is_kept_only_when_named_and_one_of_the_stores_never() {
    let f = fixture();
    let store = f.filled();
    fs::write(f.data.join("secret.key"), "k3y").unwrap();
    fs::create_dir_all(f.data.join("keys")).unwrap();
    fs::write(f.data.join("keys/signing.pem"), "pem").unwrap();
    store.backup().write(&f.zip).unwrap();
    restore(&f.zip, &f.restored).unwrap();
    assert!(!f.restored.join("secret.key").exists(), "a file of the program's is left out unless named");

    fs::remove_dir_all(&f.restored).unwrap();
    fs::remove_file(&f.zip).unwrap();
    store.backup().include("secret.key").include("keys/signing.pem").write(&f.zip).unwrap();
    restore(&f.zip, &f.restored).unwrap();
    assert_eq!(fs::read_to_string(f.restored.join("secret.key")).unwrap(), "k3y");
    assert_eq!(fs::read_to_string(f.restored.join("keys/signing.pem")).unwrap(), "pem");

    for name in ["../outside", "/etc/passwd", "a\\b", "kv.db", "kv.db-wal", "LOCK", "manifest.json", "sql/app.db"] {
        let error = store.backup().include(name).write(f.zip.with_extension("refused")).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Invalid, "{name}: {error}");
    }
    let error = store.backup().include("encryption.key").write(f.zip.with_extension("refused")).unwrap_err();
    assert!(error.to_string().contains("include_encryption_key()"), "{error}");
}

// contract: BAK-04
#[test]
fn a_backup_that_fails_leaves_no_zip() {
    let f = fixture();
    let store = f.filled();
    let error = store.backup().include("missing.txt").write(&f.zip).unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Io, "{error}");
    let beside: Vec<String> =
        tree(f.zip.parent().unwrap()).into_iter().filter(|name| name.contains("backup.zip")).collect();
    assert!(beside.is_empty(), "{beside:?}");
    assert!(!tree(&f.data).iter().any(|name| name.starts_with(".snapshot-")), "the snapshot is gone");
}

// contract: BAK-03
#[test]
fn the_encryption_key_is_left_out_unless_asked_for_and_the_backup_says_so() {
    let f = fixture();
    let store = f.open(&f.data);
    let passwords = store.bucket::<String>("passwords").encrypted().open().unwrap();
    passwords.set("db", &"hunter2".to_owned()).unwrap();
    let without = store.backup().write(&f.zip).unwrap();
    assert!(without.key_left_out);
    restore(&f.zip, &f.restored).unwrap();
    assert!(!f.restored.join(KEY).exists());
    let error = f.open(&f.restored).bucket::<String>("passwords").encrypted().open().unwrap_err();
    assert!(error.to_string().contains("encrypted with the key"), "a bucket whose key is not there says so: {error}");

    let (zip, restored) = (f.zip.with_extension("keyed"), f.restored.with_extension("keyed"));
    let with = store.backup().include_encryption_key().write(&zip).unwrap();
    assert!(!with.key_left_out);
    restore(&zip, &restored).unwrap();
    let back = f.open(&restored);
    let passwords = back.bucket::<String>("passwords").encrypted().open().unwrap();
    assert_eq!(passwords.get("db").unwrap().as_deref(), Some("hunter2"));
}

// contract: BAK-03
#[test]
fn a_backup_with_the_key_says_it_leaves_out_an_older_key_until_a_reseal() {
    let f = fixture();
    f.open(&f.data).bucket::<String>("passwords").encrypted().open().unwrap().set("db", &"hunter2".to_owned()).unwrap();
    let (old, new) = (f.data.join("old.key"), f.data.join("encryption.key"));
    fs::rename(&new, &old).unwrap();
    fs::write(&new, "cd".repeat(32)).unwrap();
    let options = Options { clock: Some(f.clock.clone()), older_encryption_keys: vec![old], ..Options::default() };
    let store = Store::open(&f.data, Options { background: false, ..options }).unwrap();
    let passwords = store.bucket::<String>("passwords").encrypted().open().unwrap();

    let before = store.backup().include_encryption_key().write(&f.zip).unwrap();
    assert!(before.key_left_out, "the old key sealed a value, and a backup takes the sealing key alone");
    passwords.reseal().unwrap();
    let after = store.backup().include_encryption_key().write(f.zip.with_extension("resealed")).unwrap();
    assert!(!after.key_left_out);
}

// contract: BAK-03
#[test]
fn a_backup_beside_a_holder_that_keeps_its_key_apart_says_it_leaves_the_key_out() {
    let f = fixture();
    let key = f.zip.with_file_name("tinystore.key");
    fs::write(&key, "0f".repeat(32)).unwrap();
    let options = Options { encryption_key_file: Some(key), background: false, ..Options::default() };
    let held = Store::open(&f.data, options).unwrap();
    let db = held.database("app").open().unwrap();
    db.bucket::<String>("passwords").encrypted().open().unwrap().set("db", &"hunter2".to_owned()).unwrap();

    // beside its holder, the store reads a key in its directory alone, and finds none
    let beside = Store::open_beside(&f.data).unwrap();
    let asked = beside.backup().include_encryption_key().write(&f.zip).unwrap_err();
    assert_eq!(asked.kind(), ErrorKind::Io, "{asked}");
    assert!(asked.to_string().contains("values sealed with the key"), "{asked}");
    assert!(!f.zip.exists(), "no zip without the key it was asked to take");
    let without = beside.backup().write(&f.zip).unwrap();
    assert!(without.key_left_out, "the backup says the values it holds open with a key it does not");
    assert!(restore(&f.zip, &f.restored).unwrap().key_left_out, "and its restore says so too");
    held.close().unwrap();
}

// contract: BAK-03
#[test]
fn a_backup_of_a_store_that_seals_nothing_misses_no_key() {
    let f = fixture();
    let store = f.open(&f.data);
    store.bucket::<String>("tokens").encrypted().open().unwrap();
    assert!(f.data.join(KEY).exists(), "the key is made for the encrypted bucket");
    assert!(!store.backup().write(&f.zip).unwrap().key_left_out, "nothing it holds needs the key");
}

// contract: BAK-04
#[test]
fn a_closed_store_takes_no_backup() {
    let f = fixture();
    let store = f.filled();
    let again = store.clone();
    store.close().unwrap();
    let error = again.backup().write(&f.zip).unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Closed, "{error}");
    assert!(!f.zip.exists());
}

// contract: BAK-01
#[test]
fn a_backup_copies_while_a_writer_writes_and_holds_a_moment_of_it() {
    let f = fixture();
    let store = f.filled();
    let codes = store.bucket::<i64>("counted").open().unwrap();
    let writer = {
        let codes = codes.clone();
        std::thread::spawn(move || (0..300).for_each(|n| codes.set(format!("k{n:03}"), &n).unwrap()))
    };
    store.backup().write(&f.zip).unwrap();
    writer.join().unwrap();
    drop(store);
    restore(&f.zip, &f.restored).unwrap();
    let restored = f.open(&f.restored);
    let counted = restored.bucket::<i64>("counted").open().unwrap();
    let mut held = 0;
    for n in 0..300 {
        match counted.get(format!("k{n:03}")).unwrap() {
            Some(value) => {
                assert_eq!((value, n), (n, held), "the writes before the copy's moment, in order");
                held += 1;
            }
            None => break,
        }
    }
    assert!((held..300).all(|n| counted.get(format!("k{n:03}")).unwrap().is_none()), "a moment: nothing after a gap");
}

/// A collector's cursor in the program's database, saying how far its samples
/// in metrics.db go.
const CURSOR: &str = "create table cursor (id integer primary key, upto integer not null) strict; \
                      insert into cursor values (1, -1)";

// contract: BAK-01
#[test]
fn a_backup_is_one_moment_of_the_store_so_a_cursor_names_only_samples_it_holds() {
    for metrics_first in [false, true] {
        let f = fixture();
        let store = f.open(&f.data);
        // the files were copied in the order their engines opened
        let early = metrics_first.then(|| store.metrics().unwrap());
        let db = store.database("app").migrations([("0001_cursor.sql", CURSOR)]).open().unwrap();
        let stats = early.unwrap_or_else(|| store.metrics().unwrap());
        let base = store.now() - Duration::from_secs(3600);
        let stop = std::sync::atomic::AtomicBool::new(false);
        std::thread::scope(|scope| {
            scope.spawn(|| {
                for i in 0_i64.. {
                    if stop.load(std::sync::atomic::Ordering::Relaxed) {
                        break;
                    }
                    let at = base + Duration::from_millis(i as u64);
                    stats.ingest(&[Series::gauge("reading").sample(at, i as f64)]).unwrap();
                    db.exec(sql!("update cursor set upto = ? where id = 1", i)).unwrap();
                }
            });
            for n in 0..4 {
                std::thread::sleep(Duration::from_millis(30));
                let (zip, restored) = (f.zip.with_extension(format!("{n}.zip")), f.restored.join(n.to_string()));
                store.backup().write(&zip).unwrap();
                restore(&zip, &restored).unwrap();
                let again = f.open(&restored);
                let app = again.database("app").migrations([("0001_cursor.sql", CURSOR)]).open().unwrap();
                let cursor: i64 = app.scalar(sql!("select upto from cursor where id = 1")).unwrap();
                let read = again.metrics().unwrap().query().name("reading").since(Duration::from_secs(7200)).read();
                let kept = read.unwrap().series.iter().flat_map(|series| series.samples().to_vec()).count() as i64;
                assert!(
                    kept == cursor + 1 || kept == cursor + 2,
                    "metrics first {metrics_first}, backup {n}: the cursor says {cursor}, and {kept} samples are kept"
                );
            }
            stop.store(true, std::sync::atomic::Ordering::Relaxed);
        });
    }
}

// contract: BAK-02
#[test]
fn a_file_whose_sha256_is_not_its_manifests_is_refused_though_its_crc_is_the_zips() {
    let f = fixture();
    let (bytes, other) = (b"kv's bytes".to_vec(), b"kv's other".to_vec());
    let listed = manifest::Listed {
        name: "kv.db".to_owned(),
        engine: "kv",
        size: other.len() as u64,
        sha256: hex(&Sha256::digest(&other)),
    };
    let manifest = Manifest { created: 0, files: vec![listed], key_left_out: false }.to_json();
    let mut writer = zip::Writer::new(Vec::new());
    writer.add("kv.db", bytes.len() as u64, bytes.as_slice(), |_| {}).unwrap();
    writer.add(MANIFEST, manifest.len() as u64, manifest.as_bytes(), |_| {}).unwrap();
    fs::write(&f.zip, writer.finish().unwrap()).unwrap();
    let error = restore(&f.zip, &f.restored).unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Corrupt, "{error}");
    assert!(error.to_string().contains("kv.db: its SHA-256 is not its manifest's"), "{error}");
    assert!(tree(&f.restored).is_empty());
}

#[test]
fn an_archive_shorter_than_a_zip_end_is_corrupt() {
    let f = fixture();
    for length in [0, 1, 3, 21] {
        fs::write(&f.zip, vec![b'P'; length]).unwrap();
        let error = restore(&f.zip, &f.restored).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Corrupt, "{length} bytes: {error}");
    }
}