tinystore 0.1.1

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
//! What engines use of their store. An application never imports this module:
//! it opens engines, and engines call these.

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

use crate::Result;
pub use crate::schedule::Wake;

// a database's file lent to kv's and jobs' handles, and where a handle opens
#[cfg(any(feature = "kv", feature = "jobs", feature = "sql"))]
mod shared;
#[cfg(any(feature = "kv", feature = "jobs"))]
pub(crate) use shared::Home;
#[cfg(any(feature = "sql", feature = "wire"))]
pub(crate) use shared::SharedFile;
#[cfg(all(feature = "sql", feature = "jobs"))]
pub(crate) use shared::{LentWrite, Wrote};

/// An engine as its store sees it: something to close at the store's close.
pub trait Engine: Send + Sync + 'static {
    /// Stops the threads that run the program's code, a queue's handlers or a
    /// channel's, and waits for those running. The store stops every engine
    /// before it closes any, so that a handler finishes while every file it
    /// writes is open. A second call does nothing.
    fn stop(&self) {}

    /// Closes the engine. A second call, or a call after the engine closed by
    /// itself, does nothing.
    fn close(&self) -> Result<()>;

    /// The store's clock was moved by hand, as a test moves it: what sleeps
    /// until a time reads the clock again.
    fn clock_moved(&self) {}

    /// A commit of its home was applied to the store's file `file`, a copy:
    /// what the engine keeps in memory of the file may be its no longer.
    fn applied(&self, file: &str) {
        let _ = file;
    }

    /// Copies the engine's files into `into`, each at the path it has in the
    /// store's directory, as one moment of the engine while it keeps working.
    /// An engine whose file another lends copies nothing: the lender does.
    fn snapshot(&self, into: &Path) -> Result<Vec<Copied>> {
        let _ = into;
        Ok(Vec::new())
    }
}

/// A file of a snapshot: its path inside the store, `/` between its parts,
/// as `kv.db`, `sql/app.db` or `blobs/objects/1a3/1a3f07`, and the engine
/// whose it is.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Copied {
    pub name: String,
    pub engine: &'static str,
}

/// Copies an SQLite file of an engine's, `name` inside the store, into the
/// snapshot at `into`, its directory made.
/// Rows one maintenance transaction removes, so that no pass holds the
/// writer long, and transactions of each kind one pass runs at most.
#[cfg(any(feature = "jobs", feature = "blobs"))]
pub(crate) const BATCH: usize = 10_000;
#[cfg(any(feature = "jobs", feature = "blobs"))]
pub(crate) const BATCHES: usize = 10;

/// Runs `batch`, a transaction that removes at most `BATCH` rows and says how
/// many it removed, until one removes fewer, `BATCHES` times at most; the
/// rows removed in all.
#[cfg(any(feature = "jobs", feature = "blobs"))]
pub(crate) fn in_batches(mut batch: impl FnMut() -> Result<usize>) -> Result<usize> {
    let mut total = 0;
    for _ in 0..BATCHES {
        let removed = batch()?;
        total += removed;
        if removed < BATCH {
            break;
        }
    }
    Ok(total)
}

/// The folder of the store's directory that holds the databases' files,
/// and with them the buckets and queues opened from a database.
#[cfg(any(feature = "kv", feature = "sql", feature = "replication"))]
pub(crate) const DATABASES: &str = "sql";

/// The file a database's name keeps, by its path in the store's directory.
#[cfg(any(feature = "sql", feature = "replication"))]
pub(crate) fn database_file(name: &str) -> String {
    format!("{DATABASES}/{name}.db")
}

#[cfg_attr(
    not(any(
        feature = "kv",
        feature = "jobs",
        feature = "sql",
        feature = "blobs",
        feature = "records",
        feature = "metrics",
        feature = "search"
    )),
    expect(dead_code, reason = "every engine copies its files with it, and a store of none has none")
)]
pub(crate) fn copy_file(file: &crate::sqlite::File, name: &str, engine: &'static str, into: &Path) -> Result<Copied> {
    let target = into.join(name);
    if let Some(parent) = target.parent() {
        std::fs::create_dir_all(parent).map_err(|error| crate::Error::io(format!("snapshot {name}"), error))?;
    }
    file.copy_into(&target).map_err(|error| error.within(format!("snapshot {name}")))?;
    Ok(Copied { name: name.to_owned(), engine })
}

/// The store's calls for the engines opened against it.
pub trait Host {
    /// The engine of type `E` this store has open, or the one `open` makes and
    /// the store then closes at its own close; every handle shares it.
    fn engine<E: Engine>(&self, open: impl FnOnce(&crate::Store) -> Result<Arc<E>>) -> Result<Arc<E>>;

    /// Closes `engine` when the store closes, the last attached first.
    fn attach(&self, engine: Arc<dyn Engine>) -> Result<()>;

    /// Runs `work` every `period` on the store's background thread, one task at
    /// a time, and sooner when what it gives back wakes it. A failure is logged
    /// once and a recovery once. Nothing runs when the store was opened without
    /// background work.
    fn every(&self, name: &str, period: Duration, work: impl FnMut() -> Result<()> + Send + 'static) -> Result<Wake>;

    /// Claims a file name in the store's directory, so that no other engine
    /// opens the same file; the name is free again once the claim is dropped.
    fn claim(&self, name: &str) -> Result<Claim>;
}

/// What gives a claimed name back to its store.
type Release = Box<dyn FnOnce(&str) + Send + Sync>;

/// A file name in the store's directory that one engine holds.
pub struct Claim {
    name: String,
    path: PathBuf,
    release: Option<Release>,
}

impl Claim {
    pub(crate) fn new(name: &str, path: PathBuf, release: Release) -> Self {
        Self { name: name.to_owned(), path, release: Some(release) }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

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

impl Drop for Claim {
    fn drop(&mut self) {
        if let Some(release) = self.release.take() {
            release(&self.name);
        }
    }
}

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

/// Migrates an engine's tables in `file`. On a copy kept in a database's
/// file whose home has not made those tables yet, it asks the home to make
/// them, then checks again: a copy's migrations are its home's.
#[cfg(any(feature = "kv", feature = "jobs"))]
pub(crate) fn migrate_in(
    store: &crate::Store,
    file: &crate::sqlite::File,
    history: &str,
    migrations: &[crate::sqlite::Migration],
    lent_by: Option<&str>,
) -> Result<()> {
    let migrated = file.migrate(history, migrations);
    #[cfg(feature = "replication")]
    if let Err(error) = &migrated
        && error.kind() == crate::ErrorKind::Unavailable
        && file.is_copy()
        && let Some(database) = lent_by.and_then(crate::replication::database_of)
        && let Some(tag) = crate::replication::tag_of(history)
    {
        crate::replication::make_in(store, &crate::replication::database_file(database), tag)?;
        return file.migrate(history, migrations);
    }
    let _ = (store, lent_by);
    migrated
}