tinystore 0.1.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use super::tx::BOUND;
use crate::engine::{Claim, Engine, Host, SharedFile};
use crate::sqlite::{Config, File, Migration};
use crate::sync::lock;
use crate::{Clock, Durability, Error, Memory, Result, Store};

/// The history a database's own migrations are kept under.
const HISTORY: &str = "migrations";

use crate::engine::database_file as file_of;

/// Statements known to write that a database remembers; past them it forgets
/// them all and asks SQLite again.
const WRITES_KEPT: usize = 1024;

/// How often each database closes the readers it has left unused for a minute,
/// as kv and jobs do in their maintenance; a burst of reads otherwise kept
/// every reader it opened, and its memory, for good.
const SWEEP: Duration = Duration::from_secs(60);

/// The store's application databases, by name: one file and one writer each,
/// opened by the first handle and closed with the store.
pub(crate) struct Databases {
    open: Mutex<HashMap<String, Arc<Base>>>,
}

/// One application database: `sql/<name>.db`, its writer and its readers,
/// and the buckets and queues kept in its file.
pub(crate) struct Base {
    pub(crate) name: String,
    pub(crate) shared: Arc<SharedFile>,
    clock: Arc<dyn Clock>,
    /// The store's memory, which a call's rows hold while it holds them.
    pub(crate) memory: Arc<Memory>,
    /// Statements known to write, by their text. SQLite says whether one
    /// writes when it compiles it, and a read call sends those to the writer.
    writes: Mutex<HashSet<String>>,
    durability: Durability,
    _claim: Claim,
}

impl Databases {
    /// The store's databases, opened with the first, their idle readers swept
    /// on the store's background thread.
    pub(crate) fn of(store: &Store) -> Result<Arc<Databases>> {
        store.engine(|store| {
            let databases = Arc::new(Databases { open: Mutex::new(HashMap::new()) });
            let weak = Arc::downgrade(&databases);
            store.every("sql: idle readers", SWEEP, move || {
                if let Some(databases) = weak.upgrade() {
                    databases.sweep();
                }
                Ok(())
            })?;
            Ok(databases)
        })
    }

    /// Closes each database's readers unused for long enough.
    fn sweep(&self) {
        let open: Vec<Arc<Base>> = lock(&self.open).values().cloned().collect();
        for base in open {
            base.file().sweep();
        }
    }

    /// The database `name`, opened when it is not, after `migrations` are
    /// applied and checked; one already open applies what it lacks of them.
    /// Its commits go as far as `durability` says, or the store's options
    /// where it says nothing; a file has one, so one already open refuses
    /// another.
    pub(crate) fn open(
        &self,
        store: &Store,
        name: &str,
        migrations: Option<Vec<Migration>>,
        durability: Option<Durability>,
    ) -> Result<Arc<Base>> {
        let mut open = lock(&self.open);
        if let Some(base) = open.get(name) {
            let base = Arc::clone(base);
            drop(open);
            if let Some(asked) = durability.filter(|asked| *asked != base.durability) {
                let opened = base.durability;
                return Err(Error::invalid(format!("open with durability {opened}, and asked for {asked}")));
            }
            if let Some(migrations) = migrations {
                base.file().migrate_checked(HISTORY, &migrations)?;
            }
            return Ok(base);
        }
        let claim = store.claim(&file_of(name))?;
        let config = config(durability.or(store.durability()));
        let durability = config.durability;
        let file = store.open_file(claim.name(), config)?;
        if let Some(migrations) = migrations {
            file.migrate_checked(HISTORY, &migrations)?;
        }
        let base = Base {
            name: name.to_owned(),
            shared: Arc::new(SharedFile::new(file, format!("sql {name}"), store)),
            clock: store.clock(),
            memory: Arc::clone(store.memory()),
            writes: Mutex::new(HashSet::new()),
            durability,
            _claim: claim,
        };
        let base = Arc::new(base);
        open.insert(name.to_owned(), Arc::clone(&base));
        Ok(base)
    }
}

impl Engine for Databases {
    /// Closes every database; the first that fails is the error, and the
    /// others close either way.
    fn close(&self) -> Result<()> {
        let open: Vec<Arc<Base>> = lock(&self.open).drain().map(|(_, base)| base).collect();
        let mut first_failure = None;
        for base in open {
            if let Err(error) = base.file().close() {
                first_failure.get_or_insert(error.within(base.describe()));
            }
        }
        first_failure.map_or(Ok(()), Err)
    }

    /// Copies each database open, the buckets and queues it keeps with it.
    fn snapshot(&self, into: &std::path::Path) -> Result<Vec<crate::engine::Copied>> {
        let open: Vec<Arc<Base>> = lock(&self.open).values().cloned().collect();
        let copy = |base: &Arc<Base>| crate::engine::copy_file(base.file(), &file_of(&base.name), "sql", into);
        open.iter().map(copy).collect()
    }
}

impl Base {
    pub(crate) fn file(&self) -> &File {
        self.shared.file()
    }

    pub(crate) fn now(&self) -> SystemTime {
        self.clock.now()
    }

    /// Fails once a transaction that started at `started` has held the writer
    /// past its bound, which then rolls back.
    pub(crate) fn within_bound(&self, started: SystemTime) -> Result<()> {
        let held = self.now().duration_since(started).unwrap_or_default();
        if held <= BOUND {
            return Ok(());
        }
        Err(Error::limit(format!(
            "{}: a transaction held the writer {held:?}, past its {BOUND:?}, and rolls back",
            self.describe()
        )))
    }

    pub(crate) fn writes(&self, text: &str) -> bool {
        lock(&self.writes).contains(text)
    }

    pub(crate) fn remember_write(&self, text: &str) {
        let mut writes = lock(&self.writes);
        if writes.len() >= WRITES_KEPT {
            writes.clear();
        }
        writes.insert(text.to_owned());
    }

    pub(crate) fn describe(&self) -> String {
        described(&self.name)
    }

    /// An error of `statement`, named by it and by the database.
    pub(super) fn failed(&self, statement: &super::statement::Sql, error: Error) -> Error {
        error.within(statement.describe()).within(self.describe())
    }
}

/// The database `name` as an error names it: `sql app`.
pub(super) fn described(name: &str) -> String {
    format!("sql {name}")
}

/// A database's connections: every file's, their cache of prepared statements
/// 128 deep, with room for an application's.
fn config(durability: Option<Durability>) -> Config {
    Config { statements: 128, ..Config::committing(durability) }
}