objshelf 0.1.0

A portable object store that caches plain Rust values on the heap, over pluggable backends from bare-metal to hosted systems.
Documentation
//! The engine behind the public API of `api.rs`: the state of an `ObjShelf` (`Core`), and the code
//! that moves objects between RAM and the backend, reaching that state through the lock of the
//! `ObjShelf` (`Locked`).
//!
//! - `object`: cached objects with their type erased.
//! - `cache`: cached objects, their use by `with` and `with_mut`, and their reads.
//! - `names`: the names, `insert` and `remove`.
//! - `write`: `flush` and `flush_object`, the only code that writes to the backend.
//! - `lifecycle`: mount, which reads the names stored with the objects, and unmount.
//!
//! The lock is the one of the RTIC resource: it blocks the tasks that share the `ObjShelf`, and
//! only them. The work falls in three kinds:
//!
//! - Changes (`with_mut`, `insert`, `remove`): in RAM only, inside the lock. They stay there
//!   until a flush.
//! - Reads of objects that are not in RAM: inside the lock too, so the tasks that share the memory
//!   wait for them, and none of them ever finds a read in progress.
//! - Writes, by the flushes only: the backend and the object to write are taken out of `Core`
//!   inside the lock, written outside of it, and put back inside the lock. While the backend is
//!   out, the storage is busy: a call that needs it returns `None`. Nothing waits.

mod cache;
mod lifecycle;
mod names;
mod object;
#[cfg(test)]
mod tests;
mod write;

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use hashbrown::HashMap;
use rtic_core::Mutex;

use crate::backend::{Backend, Usage, Wear};
use crate::{ObjShelf, ObjectId};
use cache::Entry;

/// The state of an `ObjShelf`.
pub(crate) struct Core<B: Backend> {
    /// The backend, `None` while a write uses it: the storage is busy.
    backend: Option<B>,
    /// How the backend's space is used, as of the last storage operation.
    usage: Usage,
    /// How worn the backend's storage is, as of the last storage operation.
    wear: Option<Wear>,

    // Names (`names.rs`). The backend stores each one with its object.
    directory: BTreeMap<String, ObjectId>,
    /// Removed objects, with their names, that the backend may still hold.
    removed: Vec<(ObjectId, String)>,
    next_id: u32,

    // Cached objects (`cache.rs`).
    cache: HashMap<ObjectId, Entry<B>>,
    /// RAM budget of the cached objects.
    cache_bytes: usize,
    /// RAM taken by the cached objects, including the one being written.
    used: usize,
    /// RAM taken by the modified objects, including the one being written.
    modified_bytes: usize,
    /// Called when the modified objects fill the budget, to start a flush.
    on_full: fn(),
    /// Clock of the last uses, for the LRU drop.
    tick: u64,
}

/// An `ObjShelf` reached through its lock `M`.
pub(crate) struct Locked<'a, M>(pub(crate) &'a mut M);

impl<B: Backend, M: Mutex<T = ObjShelf<B>>> Locked<'_, M> {
    /// Runs `f` on the core, inside the lock.
    fn lock<R>(&mut self, f: impl FnOnce(&mut Core<B>) -> R) -> R {
        self.0.lock(|mem| f(&mut mem.core))
    }

    pub(crate) fn usage(&mut self) -> Usage {
        self.lock(|core| core.usage)
    }

    pub(crate) fn wear(&mut self) -> Option<Wear> {
        self.lock(|core| core.wear)
    }
}

impl<B: Backend> Core<B> {
    fn next_tick(&mut self) -> u64 {
        self.tick += 1;
        self.tick
    }

    /// Whether `size` more bytes of modified objects fit in the budget.
    fn fits_modified(&self, size: usize) -> bool {
        self.modified_bytes + size <= self.cache_bytes
    }

    /// Calls `on_full` and returns `None`: the modified objects fill the budget.
    fn full<R>(&self) -> Option<R> {
        (self.on_full)();
        None
    }

    /// Puts the backend back after a write.
    fn restore(&mut self, backend: B) {
        self.usage = backend.usage();
        self.wear = backend.wear();
        self.backend = Some(backend);
    }
}