tinystore 0.7.1

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
//! Where a content's bytes lie on disk, named by its id in hex: in `uploads/`
//! until its commit, then in a directory of `objects/` for each 4,096 ids.
//!
//! ```text
//! 0x1a3f07 → uploads/1a3f07 → objects/1a3/1a3f07
//! ```

use std::collections::{HashMap, HashSet};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex, PoisonError};
use std::time::Duration;

use super::ids::Ids;
use crate::durable::sync_directory;
use crate::sync::lock;
use crate::{Error, Result};

pub(crate) const UPLOADS: &str = "uploads";
pub(crate) const OBJECTS: &str = "objects";

/// Bits of an id that name its file within its directory: 4,096 a directory.
const IDS_PER_DIR: u32 = 12;

/// Tries of a rename a scanner holds on Windows.
const RENAME_TRIES: u32 = 5;

#[cfg(test)]
thread_local! {
    /// Whether a directory's sync fails on this thread, as a failing disk's
    /// does: no file system fails one on demand.
    pub(crate) static SYNC_FAILS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

pub(crate) struct Disk {
    dir: PathBuf,
    /// Whether a file and its directory are synced before a commit names
    /// them: the store's durability is `Full`.
    durable: bool,
    made: Mutex<HashSet<i64>>,
    syncs: Mutex<HashMap<PathBuf, Arc<DirSync>>>,
    /// The snapshots under way that link the files their copies name, which
    /// no removal that begins meanwhile takes.
    paused: Mutex<usize>,
    resumed: Condvar,
}

impl Disk {
    pub(crate) fn dir(&self) -> &Path {
        &self.dir
    }

    pub(crate) fn new(dir: PathBuf, durable: bool) -> Disk {
        Disk {
            dir,
            durable,
            made: Mutex::new(HashSet::new()),
            syncs: Mutex::new(HashMap::new()),
            paused: Mutex::new(0),
            resumed: Condvar::new(),
        }
    }

    pub(crate) fn durable(&self) -> bool {
        self.durable
    }

    /// The directory of `objects/` an id's file lies in.
    pub(crate) fn fan(id: i64) -> i64 {
        id >> IDS_PER_DIR
    }

    /// Content `id`'s file by its path under the blobs' directory.
    pub(crate) fn object_name(id: i64) -> String {
        format!("{OBJECTS}/{:x}/{id:x}", Disk::fan(id))
    }

    /// The first and the last id of a directory of `objects/`.
    pub(crate) fn span(fan: i64) -> (i64, i64) {
        (fan << IDS_PER_DIR, ((fan + 1) << IDS_PER_DIR) - 1)
    }

    pub(crate) fn upload_path(&self, id: i64) -> PathBuf {
        self.dir.join(UPLOADS).join(format!("{id:x}"))
    }

    pub(crate) fn object_path(&self, id: i64) -> PathBuf {
        self.fan_dir(Disk::fan(id)).join(format!("{id:x}"))
    }

    fn fan_dir(&self, fan: i64) -> PathBuf {
        self.dir.join(OBJECTS).join(format!("{fan:x}"))
    }

    /// Creates an upload's file, which no other id ever names.
    pub(crate) fn create_upload(&self, id: i64) -> Result<std::fs::File> {
        let path = self.upload_path(id);
        std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&path)
            .map_err(|error| Error::io(format!("blobs: create {}", path.display()), error))
    }

    /// Moves an upload's file into `objects/`, where it is never written
    /// again nor named otherwise: its directory made and synced the first
    /// time, and the rename retried while a scanner holds the file on Windows.
    /// [`Disk::sync_placed`] makes its name durable before a commit names it.
    pub(crate) fn place(&self, id: i64) -> Result<()> {
        let fan = Disk::fan(id);
        self.make_fan(fan)?;
        let (from, to) = (self.upload_path(id), self.object_path(id));
        for attempt in 0.. {
            match std::fs::rename(&from, &to) {
                Ok(()) => break,
                Err(error) if held_elsewhere(&error) && attempt + 1 < RENAME_TRIES => {
                    std::thread::sleep(Duration::from_millis(1 << attempt));
                }
                Err(error) => return Err(Error::io(format!("blobs: publish {}", to.display()), error)),
            }
        }
        Ok(())
    }

    /// Syncs the directory a placed file went to, with the uploads that
    /// finish beside it.
    pub(crate) fn sync_placed(&self, id: i64) -> Result<()> {
        self.sync_dir(self.fan_dir(Disk::fan(id)))
    }

    /// Creates a directory of `objects/` the first time a file goes there,
    /// and syncs `objects/`, so that the directory's name is durable before
    /// any file in it is.
    fn make_fan(&self, fan: i64) -> Result<()> {
        if lock(&self.made).contains(&fan) {
            return Ok(());
        }
        let dir = self.fan_dir(fan);
        match std::fs::create_dir(&dir) {
            Ok(()) => self.sync_dir(self.dir.join(OBJECTS))?,
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}
            Err(error) => return Err(Error::io(format!("blobs: create {}", dir.display()), error)),
        }
        lock(&self.made).insert(fan);
        Ok(())
    }

    /// Syncs a directory once for the files that arrive in it together: each
    /// caller waits for a sync that began after it called.
    fn sync_dir(&self, dir: PathBuf) -> Result<()> {
        if !self.durable {
            return Ok(());
        }
        #[cfg(test)]
        if SYNC_FAILS.get() {
            return Err(Error::io(format!("blobs: sync {}", dir.display()), io::Error::other("a disk that fails")));
        }
        let shared = Arc::clone(lock(&self.syncs).entry(dir.clone()).or_default());
        shared.run(|| sync_directory(&dir)).map_err(|error| Error::io(format!("blobs: sync {}", dir.display()), error))
    }

    /// Removes a content's file, which no file names any more; one already
    /// gone counts as removed. It waits while a snapshot links files.
    ///
    /// One that began before the snapshot's moment goes on: the commit that
    /// left its content unnamed was before it, so the copy names it no more.
    pub(crate) fn remove_object(&self, id: i64) -> Result<()> {
        let mut paused = lock(&self.paused);
        while *paused > 0 {
            paused = self.resumed.wait(paused).unwrap_or_else(PoisonError::into_inner);
        }
        drop(paused);
        remove(&self.object_path(id))
    }

    /// Keeps every content's file where it is from the snapshot's moment
    /// until the guard is dropped. It waits for no removal under way, which a
    /// thread may run holding a writer that the moment holds too.
    pub(crate) fn pause_removals(&self) -> PausedRemovals<'_> {
        *lock(&self.paused) += 1;
        PausedRemovals(self)
    }

    /// Puts content `id`'s file into `into` at the path it has under the
    /// blobs' directory, which it gives back: a hard link, or a copy where the
    /// system links nothing. A file that is missing is left out and logged,
    /// as the scrub will find it missing in the store.
    pub(crate) fn link_object(&self, id: i64, into: &Path) -> Result<Option<String>> {
        let name = Disk::object_name(id);
        let (source, target) = (self.object_path(id), into.join(&name));
        let failed = |error: io::Error| Error::io(format!("snapshot blobs: {name}"), error);
        if let Some(parent) = target.parent() {
            std::fs::create_dir_all(parent).map_err(failed)?;
        }
        match std::fs::hard_link(&source, &target) {
            Ok(()) => Ok(Some(name)),
            Err(error) if error.kind() == io::ErrorKind::NotFound => {
                tracing::warn!(target: "tinystore", content = id, "blobs: a file the snapshot names is missing");
                Ok(None)
            }
            Err(_) => {
                std::fs::copy(&source, &target).map_err(failed)?;
                Ok(Some(name))
            }
        }
    }

    /// Removes an upload's file wherever it was left.
    pub(crate) fn remove_upload(&self, id: i64) -> Result<()> {
        remove(&self.upload_path(id))
    }

    /// Removes the files of `uploads/` that no upload of this process holds:
    /// at open every one of them, later what an upload failed to remove. Says
    /// how many went.
    pub(crate) fn sweep_uploads(&self, ids: &Ids) -> Result<usize> {
        let mut removed = 0;
        for (name, id) in self.names_in(&self.dir.join(UPLOADS))? {
            if id.is_some_and(|id| ids.holds(id)) {
                continue;
            }
            let path = self.dir.join(UPLOADS).join(&name);
            std::fs::remove_file(&path)
                .map_err(|error| Error::io(format!("blobs: remove {}", path.display()), error))?;
            removed += 1;
        }
        Ok(removed)
    }

    /// The last directory of `objects/` that holds files, by its fan; none
    /// when none does.
    pub(crate) fn last_fan(&self) -> Result<Option<i64>> {
        let fans = self.names_in(&self.dir.join(OBJECTS))?;
        Ok(fans.into_iter().filter_map(|(name, _)| i64::from_str_radix(&name, 16).ok()).max())
    }

    /// The ids of the files a directory of `objects/` holds.
    pub(crate) fn ids_in(&self, fan: i64) -> Result<Vec<i64>> {
        Ok(self.names_in(&self.fan_dir(fan))?.into_iter().filter_map(|(_, id)| id).collect())
    }

    /// The names a directory holds, each with the id it spells when the
    /// engine gave it; none when the directory does not exist.
    fn names_in(&self, dir: &Path) -> Result<Vec<(String, Option<i64>)>> {
        let entries = match std::fs::read_dir(dir) {
            Ok(entries) => entries,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
            Err(error) => return Err(Error::io(format!("blobs: list {}", dir.display()), error)),
        };
        let mut names = Vec::new();
        for entry in entries {
            let entry = entry.map_err(|error| Error::io(format!("blobs: list {}", dir.display()), error))?;
            let name = entry.file_name().to_string_lossy().into_owned();
            let id = i64::from_str_radix(&name, 16).ok().filter(|id| *id > 0 && format!("{id:x}") == name);
            names.push((name, id));
        }
        Ok(names)
    }
}

/// Removes a file; one already gone counts as removed.
fn remove(path: &Path) -> Result<()> {
    match std::fs::remove_file(path) {
        Err(error) if error.kind() != io::ErrorKind::NotFound => {
            Err(Error::io(format!("blobs: remove {}", path.display()), error))
        }
        _ => Ok(()),
    }
}

/// A directory's sync shared by the callers that arrive while one runs: a
/// caller waits for a sync that began after it called, and starts one when
/// none runs.
#[derive(Default)]
struct DirSync {
    state: Mutex<SyncState>,
    done: Condvar,
}

#[derive(Default)]
struct SyncState {
    running: bool,
    begun: u64,
    synced: u64,
    failure: Option<(io::ErrorKind, String)>,
}

impl DirSync {
    fn run(&self, flush: impl Fn() -> io::Result<()>) -> io::Result<()> {
        let mut state = lock(&self.state);
        let wanted = state.begun + 1;
        while state.synced < wanted {
            if state.running {
                state = self.done.wait(state).unwrap_or_else(std::sync::PoisonError::into_inner);
                continue;
            }
            state.running = true;
            state.begun += 1;
            let mine = state.begun;
            drop(state);
            let outcome = flush();
            state = lock(&self.state);
            state.running = false;
            state.synced = mine;
            state.failure = outcome.err().map(|error| (error.kind(), error.to_string()));
            self.done.notify_all();
        }
        match &state.failure {
            Some((kind, what)) => Err(io::Error::new(*kind, what.clone())),
            None => Ok(()),
        }
    }
}

/// Whether a rename failed because another program, a scanner or a backup
/// tool, opened the file without sharing it.
#[cfg(windows)]
fn held_elsewhere(error: &io::Error) -> bool {
    const ERROR_SHARING_VIOLATION: i32 = 32;
    error.kind() == io::ErrorKind::PermissionDenied || error.raw_os_error() == Some(ERROR_SHARING_VIOLATION)
}

#[cfg(not(windows))]
fn held_elsewhere(_: &io::Error) -> bool {
    false
}

/// Whether an open failed on a file whose removal has begun: until the handle
/// that removes it closes, Windows refuses to open it rather than saying it
/// is gone.
#[cfg(windows)]
pub(crate) fn being_removed(error: &io::Error) -> bool {
    error.kind() == io::ErrorKind::PermissionDenied
}

#[cfg(not(windows))]
pub(crate) fn being_removed(_: &io::Error) -> bool {
    false
}

/// Reads at `offset`, from several threads at once. On Unix the file's own
/// position stays; on Windows it moves, and no reader here uses it.
#[cfg(unix)]
pub(crate) fn read_at(file: &std::fs::File, bytes: &mut [u8], offset: u64) -> io::Result<usize> {
    std::os::unix::fs::FileExt::read_at(file, bytes, offset)
}

#[cfg(windows)]
pub(crate) fn read_at(file: &std::fs::File, bytes: &mut [u8], offset: u64) -> io::Result<usize> {
    std::os::windows::fs::FileExt::seek_read(file, bytes, offset)
}

/// The removals of a content's file held back while a snapshot links files.
pub(crate) struct PausedRemovals<'a>(&'a Disk);

impl Drop for PausedRemovals<'_> {
    fn drop(&mut self) {
        *lock(&self.0.paused) -= 1;
        self.0.resumed.notify_all();
    }
}