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
//! What the store runs every minute: expired files and the files marked
//! clears hid, removed a batch at a time; the files of contents no row names
//! any more; the lines of directories' files removed 30 days ago; what
//! uploads left; the settled mark; and a slice of the scrub.

use std::sync::Arc;

use rusqlite::params;

use super::change::Change;
use super::engine::{Blobs, rows_of};
use super::list::PAST_EVERY;
use super::paths::past;
use crate::sqlite::sql_error;
use crate::{Result, Store};

use crate::engine::{BATCH, BATCHES, in_batches};

const EXPIRE: &str = "delete from _tinystore_blobs_files where (set_id, path) in (
    select set_id, path from _tinystore_blobs_files where expires <= ?1 order by expires limit cast(?2 as integer)
) returning content";
const SELECT_CLEARED: &str = "select set_id, prefix, revision from _tinystore_blobs_cleared";
const DROP_HIDDEN: &str = "delete from _tinystore_blobs_files where (set_id, path) in (
    select set_id, path from _tinystore_blobs_files where set_id = ?1 and path >= ?2 and path < ?3 and revision <= ?4
    limit cast(?5 as integer)
) returning content";
const UNMARK: &str = "delete from _tinystore_blobs_cleared where set_id = ?1 and prefix = ?2 and revision = ?3";
const SELECT_UNNAMED: &str =
    "select id from _tinystore_blobs_contents where names = 0 order by id limit cast(?1 as integer)";
const FORGET: &str = "delete from _tinystore_blobs_contents where id = ?1 and names = 0";

/// What one maintenance did.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Maintenance {
    /// Files past their term, removed.
    pub expired: usize,
    /// Files a marked clear hid, removed.
    pub cleared: usize,
    /// Files on disk of contents no file names, removed.
    pub removed: usize,
    /// Bytes the scrub read.
    pub scrubbed: u64,
    /// Contents the scrub found changed, missing, or that the disk will not
    /// read.
    pub damaged: usize,
}

/// Removes expired files, the files large clears hid, and bytes no file names
/// any more, and scrubs a slice of the stored bytes. The store runs it every
/// minute; a store opened without background work leaves it to the caller.
pub fn maintain(store: &Store) -> Result<Maintenance> {
    let blobs = Blobs::of(store)?;
    crate::sqlite::own_work(|| blobs.maintain())
}

impl Blobs {
    /// Every part runs, whatever another's failure; the first failure is the
    /// answer.
    pub(crate) fn maintain(self: &Arc<Self>) -> Result<Maintenance> {
        let mut done = Maintenance::default();
        let mut first = None;
        let mut keep = |failed: Result<()>| {
            if let Err(error) = failed {
                first.get_or_insert(error);
            }
        };
        keep(self.expire().map(|expired| done.expired = expired));
        keep(self.drop_cleared().map(|cleared| done.cleared = cleared));
        keep(self.remove_unnamed().map(|removed| done.removed = removed));
        keep(self.purge_removed().map(drop));
        keep(self.disk.sweep_uploads(&self.ids).map(drop));
        for id in self.ids.doubtful() {
            self.resolve(id);
        }
        keep(self.settle());
        keep(self.scrub().map(|(scrubbed, damaged)| (done.scrubbed, done.damaged) = (scrubbed, damaged)));
        first.map_or(Ok(done), Err)
    }

    fn expire(self: &Arc<Self>) -> Result<usize> {
        let now = self.now();
        self.batches(move |change| change.deleted(EXPIRE, params![now, BATCH as i64]))
    }

    /// Deletes the files marked clears hid, a batch a transaction, and each
    /// mark with the last of its files.
    fn drop_cleared(self: &Arc<Self>) -> Result<usize> {
        let cleared = (SELECT_CLEARED, "blobs: the marked clears");
        let marks: Vec<(i64, String, i64)> = self
            .file
            .read(|connection| rows_of(connection, cleared, [], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?))))?;
        let mut total = 0;
        for (set, prefix, revision) in marks {
            let to = past(&prefix).unwrap_or_else(|| PAST_EVERY.clone());
            total += self.batches(move |change| {
                let contents = change.deleted(DROP_HIDDEN, params![set, prefix, to, revision, BATCH as i64])?;
                if contents.len() < BATCH {
                    change.execute(UNMARK, params![set, prefix, revision])?;
                }
                Ok(contents)
            })?;
        }
        Ok(total)
    }

    /// Runs `batch` in a transaction of its own until one deletes fewer than
    /// a full batch, ten at most. Each takes a name from the contents of the
    /// files it deleted, and removes the files it leaves without names once
    /// it has committed.
    fn batches<F>(self: &Arc<Self>, batch: F) -> Result<usize>
    where
        F: Fn(&mut Change<'_, '_>) -> Result<Vec<i64>> + Clone + Send + 'static,
    {
        in_batches(|| {
            let (blobs, batch) = (Arc::clone(self), batch.clone());
            let (deleted, freed) = self.file.write(0, move |tx| {
                let mut change = Change::new(tx, &blobs);
                let contents = batch(&mut change)?;
                let deleted = contents.len();
                change.release_all(contents)?;
                Ok((deleted, change.freed))
            })?;
            self.remove_freed(&freed);
            Ok(deleted)
        })
    }

    /// Removes the files of contents no file names, which a commit left
    /// because its own removal failed, and then their rows. A file that will
    /// not go stays listed, and is tried again next time.
    fn remove_unnamed(&self) -> Result<usize> {
        let mut removed = 0;
        for _ in 0..BATCHES {
            let unnamed = (SELECT_UNNAMED, "blobs: the contents no file names");
            let unnamed: Vec<i64> =
                self.file.read(|connection| rows_of(connection, unnamed, [BATCH as i64], |row| row.get(0)))?;
            let gone: Vec<i64> = unnamed.iter().copied().filter(|&id| self.disk.remove_object(id).is_ok()).collect();
            if !gone.is_empty() {
                let forgotten = gone.clone();
                self.file.write(0, move |tx| {
                    for id in &forgotten {
                        tx.execute(FORGET, [id]).map_err(|error| sql_error("blobs: forget a content", error))?;
                    }
                    Ok(())
                })?;
            }
            removed += gone.len();
            if unnamed.len() < BATCH || gone.len() < unnamed.len() {
                break;
            }
        }
        Ok(removed)
    }
}

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