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
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
//! A copy of a file another node writes: its home's entries applied in
//! order, each a whole commit, the position applied kept beside them, and
//! each entry kept in the copy's own log for the nodes that copy it.
//!
//! A position kept early would claim pages the file lacks, so it is written
//! only after its commit, and so may be late: a process killed between the
//! two, or a write of it a power loss took, leaves the file holding commits
//! its position does not name. The position keeps where the file's WAL ended
//! after the commit it names, and the WAL holds every commit past that until a
//! checkpoint, which runs only once the position is synced. A copy that opens
//! counts the commits its WAL holds past its position: those its own log
//! names, kept before the position, it takes as applied, and the others it
//! names as its home sends them again, writing none of their pages twice. A
//! promotion that cannot name them begins its log again
//! ([`super::promote`]).

use std::collections::VecDeque;
use std::fs::{self, OpenOptions};
use std::io::{self, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, PoisonError};

use sha2::{Digest, Sha256};

use super::entry::{Entry, Kind, Point};
use super::log::Log;
use crate::sqlite::{Applier, Durability, Observe, Position as WalEnd};
use crate::store::WeakStore;
use crate::{Error, ErrorKind, Result, Store};

/// The file in a copy's log directory that marks its file a copy.
pub(crate) const MARK: &str = "copy";

pub(crate) struct Copy {
    name: String,
    path: PathBuf,
    store: WeakStore,
    applier: Applier,
    position: Mutex<Position>,
    log: Log,
    /// Each commit the file holds past the position, as a crash left them:
    /// the next entries, whose pages it holds already.
    unnamed: Mutex<VecDeque<Unnamed>>,
}

/// A commit a copy's file holds that its position does not name.
pub(crate) struct Unnamed {
    /// Where it ends in the file's WAL.
    end: WalEnd,
    /// What it wrote, as `pages_sum` sums it.
    sum: [u8; 32],
}

/// The SHA-256 of a commit's pages and the file's size after it, page 1's
/// change counters, which SQLite writes at each commit of its own, taken as
/// zeros: the same for an entry and for the commit a copy made of it.
fn pages_sum(page_size: u32, size: u32, pages: &[(u32, &[u8])]) -> [u8; 32] {
    let mut sum = Sha256::new();
    sum.update(page_size.to_le_bytes());
    sum.update(size.to_le_bytes());
    for &(number, page) in pages {
        sum.update(number.to_le_bytes());
        if number == 1 && page.len() >= 96 {
            let mut first = page.to_vec();
            first[24..28].fill(0);
            first[92..96].fill(0);
            sum.update(&first);
        } else {
            sum.update(page);
        }
    }
    sum.finalize().into()
}

/// The entry applied last, where the file's WAL ended after its commit, and
/// the file that keeps them.
pub(crate) struct Position {
    point: Point,
    wal: WalEnd,
    kept: fs::File,
    path: PathBuf,
}

impl Copy {
    /// Makes the store's file `name` a copy of its home's from its first
    /// entry, or opens the copy it is already. A file the store wrote itself
    /// is its own and becomes a copy only from a snapshot of its home.
    #[cfg(test)]
    pub(crate) fn start(store: &Store, name: &str) -> Result<Copy> {
        let what = || format!("copy {name}");
        let dir = store.log_of(name).map_err(|error| error.within(what()))?;
        if !dir.join(MARK).exists() {
            if store.dir().join(name).exists() {
                return Err(Error::invalid(
                    "the store holds its own file of that name: a copy takes its place only from a snapshot of its home",
                )
                .within(what()));
            }
            mark(&dir).map_err(|error| error.within(what()))?;
        }
        Self::open(store, name)
    }

    /// Opens the copy of `name` the store holds, going on from its position.
    pub(crate) fn open(store: &Store, name: &str) -> Result<Copy> {
        let what = || format!("copy {name}");
        let dir = store.log_of(name).map_err(|error| error.within(what()))?;
        let path = store.dir().join(name);
        let mut position = Position::read(dir.join(Position::FILE)).map_err(|error| error.within(what()))?;
        let unnamed = position.settle(&path, &dir).map_err(|error| error.within(what()))?;
        let log = Log::open(dir, store.node_bounds(), store.node_notify()).map_err(|e| e.within(what()))?;
        Ok(Copy {
            name: name.to_owned(),
            applier: Applier::new(&path, Durability::Full),
            path,
            store: store.downgrade(),
            position: Mutex::new(position),
            log,
            unnamed: Mutex::new(unnamed),
        })
    }

    pub(crate) fn path(&self) -> &Path {
        &self.path
    }

    /// Runs `read` with the entry applied last, no entry applied meanwhile.
    pub(crate) fn holding<T>(&self, read: impl FnOnce(Point) -> Result<T>) -> Result<T> {
        let position = self.lock();
        read(position.point)
    }

    /// The sequence of the entry applied last: the next it takes is the one after.
    pub(crate) fn applied(&self) -> u64 {
        self.lock().point.sequence
    }

    /// The entry applied last. Taken while no entry is being applied, so the
    /// file is that entry's commit.
    pub(crate) fn point(&self) -> Point {
        self.lock().point
    }

    /// Applies the home's next entry, or nothing for one applied already. An
    /// entry that does not follow the last applied, by its sequence and by the
    /// check it names, is refused: entries are missing, or another home wrote it.
    /// `bytes` are the entry as its home wrote it, which the copy's log keeps.
    pub(crate) fn apply(&self, entry: &Entry<'_>, bytes: Option<&[u8]>) -> Result<()> {
        let mut position = self.lock();
        if !position.takes(entry).map_err(|error| error.within(format!("copy {}", self.name)))? {
            return Ok(());
        }
        let wal = match entry.kind {
            Kind::Commit => self.commit(entry, &position)?,
            Kind::Begin if entry.sequence == 1 && entry.size <= 1 => position.wal,
            Kind::Begin | Kind::Gap => {
                return Err(Error::new(
                    ErrorKind::Conflict,
                    format!(
                        "copy {}: its home's log starts again at entry {}: the copy starts again from a snapshot",
                        self.name, entry.sequence
                    ),
                ));
            }
        };
        // the log keeps the entry before the position names it, so that a log ahead names what the file holds
        if let Some(bytes) = bytes {
            self.log.keep(entry, bytes).map_err(|error| error.within(format!("copy {}", self.name)))?;
        }
        position.keep(entry.point(), wal).map_err(|error| error.within(format!("copy {}", self.name)))?;
        if self.applier.is_long() {
            self.sync(&mut position).map_err(|error| error.within(format!("copy {}", self.name)))?;
            self.applier.checkpoint()?;
        }
        drop(position);
        if let Some(store) = self.store.upgrade() {
            store.applied(&self.name);
        }
        Ok(())
    }

    /// Commits `entry`'s pages, or names the next commit the file holds
    /// already, a crash having left the position behind it; where the WAL
    /// ends after it. Those commits are the old home's, of the position's
    /// epoch: an entry of a later epoch while some remain is another home's,
    /// and the copy starts again from a snapshot. So is one of its epoch whose
    /// pages are not that commit's: a home that lost the commit too, in the
    /// same power loss, made another after the same entry.
    fn commit(&self, entry: &Entry<'_>, position: &Position) -> Result<WalEnd> {
        let mut unnamed = self.unnamed.lock().unwrap_or_else(PoisonError::into_inner);
        let Some(next) = unnamed.front() else {
            return self.applier.apply(entry.page_size, entry.size, &entry.pages);
        };
        if entry.epoch != position.point.epoch {
            return Err(Position::conflict(format!(
                "copy {}: its file holds {} commits of epoch {} past its position, and entry {} is of epoch {}: it \
                 starts again from a snapshot",
                self.name,
                unnamed.len(),
                position.point.epoch,
                entry.sequence,
                entry.epoch
            )));
        }
        if next.sum != pages_sum(entry.page_size, entry.size, &entry.pages) {
            return Err(Position::conflict(format!(
                "copy {}: the commit its file holds past its position is not entry {}, which its home made again \
                 after the same entry: it starts again from a snapshot",
                self.name, entry.sequence
            )));
        }
        Ok(unnamed.pop_front().expect("one is left").end)
    }

    /// Makes the log and then the position durable: a position synced first
    /// would, after a power loss, name entries the log lost.
    fn sync(&self, position: &mut Position) -> Result<()> {
        self.log.sync()?;
        position.sync()
    }

    fn lock(&self) -> MutexGuard<'_, Position> {
        self.position.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

#[cfg(test)]
#[allow(dead_code, reason = "the copy tests call them, and they build only with every engine")]
impl Copy {
    /// Commits an entry's pages and keeps nothing of it, as a process killed
    /// right after the commit leaves a copy.
    pub(crate) fn commit_and_die(&self, entry: &Entry<'_>) -> Result<()> {
        self.applier.apply(entry.page_size, entry.size, &entry.pages).map(drop)
    }

    pub(crate) fn unnamed(&self) -> u64 {
        self.unnamed.lock().unwrap_or_else(PoisonError::into_inner).len() as u64
    }

    pub(crate) fn log_unsynced(&self) -> bool {
        self.log.unsynced()
    }
}

impl Drop for Copy {
    fn drop(&mut self) {
        // the position is what says how far the file went once its
        // connections close: synced before them
        let _ = self.sync(&mut self.lock());
        self.applier.close();
    }
}

/// Whether the store's file whose log is in `log` is a copy, which its
/// engines open to read.
pub(crate) fn is_copy(log: &Path) -> bool {
    log.join(MARK).exists()
}

/// Whether the copy whose log is in `log` holds a whole file at a position:
/// its file is there, and the position it applied.
pub(crate) fn is_ready(log: &Path, file: &Path) -> bool {
    is_copy(log) && file.exists() && fs::metadata(log.join(Position::FILE)).is_ok_and(|kept| kept.len() > 0)
}

/// Marks the file whose log is in `dir` a copy, before anything of its
/// home's is applied.
pub(crate) fn mark(dir: &Path) -> Result<()> {
    fs::create_dir_all(dir).map_err(|error| io_error(dir, error))?;
    fs::write(dir.join(MARK), b"").map_err(|error| io_error(dir, error))?;
    crate::durable::sync_directory(dir).map_err(|error| io_error(dir, error))
}

impl Position {
    /// Where the copy keeps the entry it applied last: its sequence, epoch
    /// and check, where the file's WAL ended after it, as its checkpoints, two
    /// salts and frame, and the first four bytes of their SHA-256.
    pub(crate) const FILE: &str = "applied";
    const NAMED: usize = 8 + 8 + 16 + 16;
    const BYTES: usize = Self::NAMED + 4;

    /// The entry the position names.
    pub(crate) fn point(&self) -> Point {
        self.point
    }

    /// Reads the position kept in `path`, or the copy's start when there is
    /// none yet.
    pub(crate) fn read(path: PathBuf) -> Result<Position> {
        let kept = OpenOptions::new().read(true).write(true).create(true).truncate(false).open(&path);
        let kept = kept.map_err(|error| io_error(&path, error))?;
        let (point, wal) = Self::read_kept(&path)?.unwrap_or_default();
        Ok(Position { point, wal, kept, path })
    }

    /// Finds the commits the file at `file` holds past the position, as a
    /// crash leaves them: those the copy's log in `log` names become the
    /// position, and the others are answered, each with where it ends in the
    /// WAL and what it wrote, for the entries to come.
    pub(crate) fn settle(&mut self, file: &Path, log: &Path) -> Result<VecDeque<Unnamed>> {
        let mut past = VecDeque::new();
        crate::sqlite::each_commit_past(file, self.wal, |commit| {
            let sum = pages_sum(commit.page_size, commit.size, &commit.pages);
            past.push_back(Unnamed { end: commit.position, sum });
        })?;
        if let Some(last) = Log::last_in(log)?.filter(|last| last.sequence > self.point.sequence) {
            let mut commits = 0;
            Log::read(log, |entry| {
                commits += usize::from(entry.sequence > self.point.sequence && entry.kind == Kind::Commit);
                Ok(())
            })?;
            if commits > past.len() {
                return Err(Self::corrupt(
                    "its log names commits its file does not hold: it starts again from a snapshot",
                ));
            }
            let wal = past.drain(..commits).next_back().map_or(self.wal, |named| named.end);
            self.keep_durably_at(last, wal)?;
        }
        if !past.is_empty() {
            tracing::warn!(
                target: "tinystore",
                file = %file.display(),
                commits = past.len(),
                "a copy holds commits its position does not name, as a crash left it: it names them as they come again"
            );
        }
        Ok(past)
    }

    /// The point kept in `path`, with nothing opened to write: none for a
    /// file that is not there or holds none yet.
    pub(crate) fn read_point(path: &Path) -> Result<Option<Point>> {
        Ok(Self::read_kept(path)?.map(|(point, _)| point))
    }

    /// The point and the WAL's end kept in `path`.
    fn read_kept(path: &Path) -> Result<Option<(Point, WalEnd)>> {
        let bytes = match fs::read(path) {
            Ok(bytes) => bytes,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
            Err(error) => return Err(io_error(path, error)),
        };
        if bytes.is_empty() {
            return Ok(None);
        }
        let named = Self::NAMED;
        let sum_holds = bytes.len() == Self::BYTES && bytes[named..] == Self::checksum(&bytes[..named]).to_le_bytes();
        if !sum_holds {
            return Err(Self::corrupt("its position does not read: the copy starts again from a snapshot"));
        }
        let word = |at: usize| u32::from_le_bytes(bytes[at..at + 4].try_into().expect("four bytes"));
        let point = Point {
            sequence: u64::from_le_bytes(bytes[..8].try_into().expect("eight bytes")),
            epoch: u64::from_le_bytes(bytes[8..16].try_into().expect("eight bytes")),
            check: bytes[16..32].try_into().expect("sixteen bytes"),
        };
        let wal = WalEnd { checkpoints: word(32), salts: [word(36), word(40)], frame: word(44) };
        Ok(Some((point, wal)))
    }

    /// Whether `entry` is the next to apply: `false` for one applied already,
    /// an error for one that does not follow.
    fn takes(&self, entry: &Entry<'_>) -> Result<bool> {
        let point = &self.point;
        if entry.sequence <= point.sequence {
            if entry.sequence == point.sequence && entry.check != point.check {
                return Err(Self::conflict(format!(
                    "entry {} is not the one it applied: another home wrote it",
                    entry.sequence
                )));
            }
            return Ok(false);
        }
        if entry.sequence > point.sequence + 1 {
            return Err(Error::invalid(format!(
                "entry {} follows {}: the entries between are missing",
                entry.sequence, point.sequence
            )));
        }
        if !entry.follows(point) {
            return Err(Self::conflict(format!(
                "entry {} does not follow the one it applied: another home wrote it",
                entry.sequence
            )));
        }
        Ok(true)
    }

    /// Keeps `point`, the file's WAL ending at `wal` after its commit.
    pub(crate) fn keep(&mut self, point: Point, wal: WalEnd) -> Result<()> {
        let mut bytes = Vec::with_capacity(Self::BYTES);
        bytes.extend_from_slice(&point.sequence.to_le_bytes());
        bytes.extend_from_slice(&point.epoch.to_le_bytes());
        bytes.extend_from_slice(&point.check);
        for word in [wal.checkpoints, wal.salts[0], wal.salts[1], wal.frame] {
            bytes.extend_from_slice(&word.to_le_bytes());
        }
        bytes.extend_from_slice(&Self::checksum(&bytes).to_le_bytes());
        self.kept
            .seek(SeekFrom::Start(0))
            .and_then(|_| self.kept.write_all(&bytes))
            .map_err(|e| io_error(&self.path, e))?;
        (self.point, self.wal) = (point, wal);
        Ok(())
    }

    /// Makes what was kept durable.
    pub(crate) fn sync(&mut self) -> Result<()> {
        self.kept.sync_data().map_err(|error| io_error(&self.path, error))
    }

    /// Keeps `point` durably, a point of no file's WAL: a promotion's, or the
    /// position a snapshot leaves, whose file has no WAL yet.
    pub(crate) fn keep_durably(&mut self, point: Point) -> Result<()> {
        self.keep_durably_at(point, WalEnd::default())
    }

    /// Keeps `point` durably, the file's WAL ending at `wal`.
    pub(crate) fn keep_durably_at(&mut self, point: Point, wal: WalEnd) -> Result<()> {
        self.keep(point, wal)?;
        self.sync()
    }

    fn checksum(bytes: &[u8]) -> u32 {
        let digest = Sha256::digest(bytes);
        u32::from_le_bytes(digest[..4].try_into().expect("four bytes"))
    }

    fn conflict(what: String) -> Error {
        Error::new(ErrorKind::Conflict, what)
    }

    fn corrupt(what: &str) -> Error {
        Error::new(ErrorKind::Corrupt, what)
    }
}

pub(crate) fn io_error(path: &Path, error: io::Error) -> Error {
    Error::io(path.display().to_string(), error)
}

/// Removes the file at `path`; one that is not there is removed already.
pub(crate) fn remove_if_there(path: &Path) -> Result<()> {
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(io_error(path, error)),
    }
}