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
//! A file as a read finds it: its fields and the bytes it held then, which a
//! replace, a delete or an expiry after do not change.

use std::io::{self, Read, Seek, SeekFrom};

use rusqlite::{OptionalExtension, params};
use sha2::{Digest, Sha256};

use super::change::hidden;
use super::directory::{Outside, time_of, version_etag};
use super::disk::{being_removed, read_at};
use super::files::Files;
use super::info::{FileInfo, FileRow, file_columns};
use crate::limits::Limit;
use crate::sqlite::sql_error;
use crate::{Error, Result};

/// Lookups a `get` makes while the path changes under it.
const LOOKUPS: usize = 3;
/// The bytes a whole read takes at once, each piece hashed while it is still
/// in the processor's cache rather than once the whole file has passed; and
/// a piece of an awaited file's stream.
pub(crate) const PIECE: usize = 256 << 10;

const SELECT_OPEN: &str = concat!(
    "select ",
    file_columns!(),
    ", f.content, c.id, c.inline, c.damaged, b.bytes
    from _tinystore_blobs_files as f left join _tinystore_blobs_contents as c on c.id = f.content
        left join _tinystore_blobs_bodies as b on c.inline and b.id = f.content
    where f.set_id = ?1 and f.path = ?2 and (f.expires is null or f.expires > ?3) and not ",
    hidden!()
);

/// A file and its bytes as [`Files::get`] found it. A whole read, from its
/// first byte to its last in order, is checked against the SHA-256 taken when
/// it was written, and its last bytes are held back until it matches; a range
/// is not checked.
///
/// A directory's file, as [`Directory::get`](super::Directory::get) opened
/// it, has no hash: each piece read holds to the size and the time it was
/// opened with, and fails `conflict` once the file moved. In a growing
/// directory a read holds to no version, and its [`info`](Self::info) takes
/// the file's size and time from each piece read in order.
pub struct StoredFile {
    info: FileInfo,
    body: Body,
    /// What an error calls the file: `files avatars: "1.png"`.
    label: String,
    sha256: Vec<u8>,
    /// Where the next read begins.
    position: u64,
    /// The hash of the bytes read in order from the first, and how far.
    hash: Sha256,
    hashed: u64,
}

enum Body {
    /// An inline file's bytes, checked when it was found.
    Inline(Vec<u8>),
    /// A file held open: the engine can remove it, and this reader reads on.
    File(std::fs::File),
    /// A directory's file on the disk, held to the version it opened.
    Outside(Outside),
}

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

/// What a lookup of a path found: its row, and its content's, which a
/// missing content row leaves empty.
struct Found {
    row: FileRow,
    content: i64,
    named: Option<i64>,
    inline: Option<bool>,
    damaged: Option<bool>,
    body: Option<Vec<u8>>,
}

impl StoredFile {
    /// The file at a whole path and its bytes. A file gone between the lookup
    /// and its open went because its path changed, so the path is looked up
    /// again, three lookups in all.
    pub(crate) fn open(files: &Files, whole: &str) -> Result<Option<StoredFile>> {
        let mut failed = None;
        for _ in 0..LOOKUPS {
            let Some(found) = lookup(files, whole)? else { return Ok(None) };
            if failed == Some(found.content) {
                return Err(Error::corrupt(format!("the file of content {} is missing", found.content)));
            }
            // an awaited get opens a file on the store's pool, never where its future is polled
            if found.inline != Some(true) && crate::polled::waits_refused() {
                return Err(crate::polled::refused(|| format!("{}: {whole:?}: open its file", files.describe())));
            }
            match StoredFile::of(files, whole, found) {
                Err(Gone(content)) => failed = Some(content),
                Ok(opened) => {
                    if let Ok(opened) = &opened {
                        files.blobs.probe.count("tinystore_blobs_read_total", &[], 1.0);
                        files.blobs.probe.count("tinystore_blobs_read_bytes_total", &[], opened.info().size as f64);
                    }
                    return opened.map(Some);
                }
            }
        }
        Err(Error::new(crate::ErrorKind::Conflict, format!("the path changed {LOOKUPS} times while it was read")))
    }

    fn of(files: &Files, whole: &str, found: Found) -> std::result::Result<Result<StoredFile>, Gone> {
        let content = found.content;
        let checked = (|| {
            if found.named.is_none() {
                return Err(Error::corrupt(format!("content {content}, which the file names, is missing")));
            }
            if found.damaged == Some(true) {
                return Err(Error::corrupt(format!("the scrub found content {content} changed or missing")));
            }
            Ok(())
        })();
        if let Err(error) = checked {
            return Ok(Err(error));
        }
        let sha256 = found.row.sha256.clone();
        let size = found.row.size;
        let inline = found.inline == Some(true);
        let body = if inline {
            let body = found.body.unwrap_or_default();
            if i64::try_from(body.len()).ok() != Some(size) || Sha256::digest(&body).as_slice() != sha256.as_slice() {
                return Ok(Err(Error::corrupt(format!("the bytes of content {content} do not hash to its SHA-256"))));
            }
            Body::Inline(body)
        } else {
            match std::fs::File::open(files.blobs.disk.object_path(content)) {
                Ok(file) => Body::File(file),
                Err(error) if error.kind() == io::ErrorKind::NotFound || being_removed(&error) => {
                    return Err(Gone(content));
                }
                Err(error) => return Ok(Err(Error::io("blobs: open a file", error))),
            }
        };
        let info = match found.row.info(&files.folder) {
            Ok(info) => info,
            Err(error) => return Ok(Err(error)),
        };
        // an inline file's bytes were hashed as they were found, and a read
        // of them hashes nothing again
        let hashed = if inline { info.size } else { 0 };
        Ok(Ok(StoredFile {
            info,
            body,
            label: format!("{}: {whole:?}", files.describe()),
            sha256,
            position: 0,
            hash: Sha256::new(),
            hashed,
        }))
    }

    /// A directory's file, as its `get` opened it.
    pub(crate) fn outside(info: FileInfo, outside: Outside, label: String) -> StoredFile {
        let hashed = info.size;
        StoredFile {
            info,
            body: Body::Outside(outside),
            label,
            sha256: Vec::new(),
            position: 0,
            hash: Sha256::new(),
            hashed,
        }
    }

    pub fn info(&self) -> &FileInfo {
        &self.info
    }

    /// Whether an ETag a read was given names these bytes, as a write's
    /// condition compares it: quoted or not, in either case.
    #[cfg(feature = "wire")]
    pub(crate) fn matches(&self, etag: &str) -> bool {
        match &self.body {
            Body::Outside(_) => etag.trim_matches('"') == self.info.etag.trim_matches('"'),
            Body::Inline(_) | Body::File(_) => super::info::matches(etag, &self.sha256),
        }
    }

    /// Whether it is a growing directory's file, whose reads go up to the size
    /// it has as they read, each bringing that size and time.
    #[cfg(feature = "wire")]
    pub(crate) fn is_growing(&self) -> bool {
        matches!(&self.body, Body::Outside(outside) if outside.is_growing())
    }

    /// What its errors call it: `files avatars: "users/42/1.png"`.
    #[cfg(feature = "async")]
    pub(crate) fn label(&self) -> &str {
        &self.label
    }

    /// Whether its bytes are in memory, kept inline in blobs.db: a read of
    /// them touches no disk.
    #[cfg(feature = "async")]
    pub(crate) fn is_inline(&self) -> bool {
        matches!(self.body, Body::Inline(_))
    }

    /// Reads up to `most` bytes from `at`, as many as the file holds there,
    /// and leaves the next read after them: checked from the first byte as a
    /// read in order is, its last bytes held back until they match.
    #[cfg(feature = "async")]
    pub(crate) fn read_piece(&mut self, at: u64, most: usize) -> Result<Vec<u8>> {
        self.seek(SeekFrom::Start(at)).map_err(|error| self.error_of(error))?;
        let left = usize::try_from(self.info.size.saturating_sub(at)).unwrap_or(usize::MAX);
        let mut piece = vec![0; most.min(left)];
        let mut filled = 0;
        while filled < piece.len() {
            match self.read(&mut piece[filled..]) {
                Ok(0) => return Err(self.corrupt("its file ends before it does")),
                Ok(read) => filled += read,
                Err(error) => return Err(self.error_of(error)),
            }
        }
        Ok(piece)
    }

    /// The whole file from its first byte, checked; it holds every byte in
    /// memory, so a large file is read as a stream.
    pub fn read_all(&mut self) -> Result<Vec<u8>> {
        let size = usize::try_from(self.info.size).map_err(|_| {
            let memory = Limit { name: "file bytes", bound: usize::MAX as u64 };
            memory.refuse(self.info.size, "a file past this machine's memory").within(&self.label)
        })?;
        let mut bytes = vec![0; size];
        self.read_whole(&mut bytes)?;
        Ok(bytes)
    }

    /// Reads the whole file, checked, into `into`, which is as long as the
    /// file: where its bytes leave, so that they are written once. A token of
    /// the call stops it between two pieces, as when an awaited read's future
    /// is dropped (STORE-09).
    pub(crate) fn read_whole(&mut self, into: &mut [u8]) -> Result<()> {
        if into.len() as u64 != self.info.size {
            let why = format!("a file of {} bytes read into {}", self.info.size, into.len());
            return Err(Error::internal(why).within(&self.label));
        }
        self.seek(SeekFrom::Start(0)).map_err(|error| Error::io("blobs: read a file", error))?;
        for piece in into.chunks_mut(PIECE) {
            crate::cancel::check(|| format!("{}: a whole read", self.label))?;
            let mut filled = 0;
            while filled < piece.len() {
                match self.read(&mut piece[filled..]) {
                    Ok(0) => return Err(self.corrupt("its file ends before it does")),
                    Ok(read) => filled += read,
                    Err(error) => return Err(self.error_of(error)),
                }
            }
        }
        Ok(())
    }

    /// Reads at `offset` without moving where the next read begins, from
    /// several threads at once; like any range it is not checked.
    pub fn read_at(&self, bytes: &mut [u8], offset: u64) -> io::Result<usize> {
        if let Body::Outside(outside) = &self.body {
            return outside.read_at(bytes, offset).map(|piece| piece.read).map_err(|error| self.labelled(error));
        }
        if offset >= self.info.size {
            return Ok(0);
        }
        let wanted = usize::try_from(self.info.size - offset).map_or(bytes.len(), |left| left.min(bytes.len()));
        let read = match &self.body {
            Body::Inline(body) => {
                let at = usize::try_from(offset).unwrap_or(usize::MAX);
                bytes[..wanted].copy_from_slice(&body[at..at + wanted]);
                wanted
            }
            Body::File(file) => read_at(file, &mut bytes[..wanted], offset)?,
            Body::Outside(_) => unreachable!("a directory's file reads above"),
        };
        if read == 0 && wanted > 0 {
            return Err(io::Error::other(self.corrupt("its file ends before it does")));
        }
        Ok(read)
    }

    /// Hashes the bytes of a read that carries on from the bytes hashed
    /// before it, and compares the hash once they reach the last byte:
    ///
    /// ```text
    /// read [0, 512), seek 0, read [0, 4096) → hashed to 4096, the first 512 once
    /// seek 1000, read                        → a range: not hashed
    /// ```
    fn check(&mut self, bytes: &[u8]) -> io::Result<()> {
        let end = self.position + bytes.len() as u64;
        if self.position > self.hashed || end <= self.hashed {
            return Ok(());
        }
        let from = usize::try_from(self.hashed - self.position).unwrap_or(0);
        if end < self.info.size {
            self.hash.update(&bytes[from..]);
            self.hashed = end;
            return Ok(());
        }
        // The last bytes count as hashed only once they match, so that a read
        // again after a failure fails again rather than find them hashed.
        let mut whole = self.hash.clone();
        whole.update(&bytes[from..]);
        if whole.finalize().as_slice() != self.sha256.as_slice() {
            return Err(io::Error::other(self.corrupt("its bytes do not hash to its SHA-256")));
        }
        self.hashed = end;
        Ok(())
    }

    /// An error of a directory's read, said of the file.
    fn labelled(&self, error: io::Error) -> io::Error {
        let ours = error.get_ref().is_some_and(|inner| inner.is::<Error>());
        let error = self.error_of(error);
        io::Error::other(if ours { error.within(&self.label) } else { error })
    }

    fn corrupt(&self, why: &str) -> Error {
        Error::corrupt(why).within(&self.label)
    }

    /// The store's error an `io::Error` of this reader carries, or one of its own.
    fn error_of(&self, error: io::Error) -> Error {
        // An OS error carries no inner error, and `into_inner` would drop it whole.
        if !error.get_ref().is_some_and(|inner| inner.is::<Error>()) {
            return Error::io("blobs: read a file", error).within(&self.label);
        }
        match error.into_inner().map(|inner| inner.downcast::<Error>()) {
            Some(Ok(error)) => *error,
            _ => Error::internal("blobs: a read failed").within(&self.label),
        }
    }
}

/// A file gone between its lookup and its open, by its content's id.
struct Gone(i64);

impl Read for StoredFile {
    fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> {
        if let Body::Outside(outside) = &self.body {
            let piece = outside.read_at(bytes, self.position).map_err(|error| self.labelled(error))?;
            if outside.is_growing() {
                self.info.size = piece.size;
                self.info.last_modified = time_of(piece.modified);
                self.info.etag = version_etag(piece.size, piece.modified);
            }
            self.position += piece.read as u64;
            return Ok(piece.read);
        }
        if self.position >= self.info.size {
            return Ok(0);
        }
        let read = self.read_at(bytes, self.position)?;
        self.check(&bytes[..read])?;
        self.position += read as u64;
        Ok(read)
    }
}

impl Seek for StoredFile {
    fn seek(&mut self, to: SeekFrom) -> io::Result<u64> {
        self.position = position_after(self.position, self.info.size, to)?;
        Ok(self.position)
    }
}

/// Where a read begins once a reader at `position` of a file of `size` bytes
/// seeks `to`: past the end is allowed, and reads nothing.
pub(crate) fn position_after(position: u64, size: u64, to: SeekFrom) -> io::Result<u64> {
    let after = match to {
        SeekFrom::Start(at) => Some(at),
        SeekFrom::Current(by) => position.checked_add_signed(by),
        SeekFrom::End(by) => size.checked_add_signed(by),
    };
    after.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "a seek before the file's start"))
}

fn lookup(files: &Files, whole: &str) -> Result<Option<Found>> {
    let now = files.blobs.now();
    files.blobs.file.read(|connection| {
        connection
            .prepare_cached(SELECT_OPEN)
            .and_then(|mut statement| {
                statement
                    .query_row(params![files.set, whole, now], |row| {
                        Ok(Found {
                            row: FileRow::read(row)?,
                            content: row.get(7)?,
                            named: row.get(8)?,
                            inline: row.get(9)?,
                            damaged: row.get(10)?,
                            body: row.get(11)?,
                        })
                    })
                    .optional()
            })
            .map_err(|error| sql_error("blobs: a file's lookup", error))
    })
}

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