inc_complete/storage/
mod.rs

1use crate::{Cell, DbHandle};
2
3mod hashmapped;
4mod indexmapped;
5mod macros;
6mod singleton;
7
8pub use hashmapped::HashMapStorage;
9pub use indexmapped::TreeIndexStorage;
10pub use singleton::SingletonStorage;
11
12/// The Storage trait is implemented on a type which can cache all of the computations
13/// used in the program (or a subset of it). These types are typically composed of
14/// several fields with each field implementing `StorageFor<T>` where `T` is one
15/// computation type. Note that each computation type must be unique within a `Storage` type.
16///
17/// This trait is most often automatically implemented by `impl_storage!`, see the documentation
18/// on that macro for usage details.
19///
20/// Note that during serialization, the entire Storage is serialized along with the `Db` object.
21/// To achieve backwards-compatible serialization even when new fields for new computation types
22/// are added, it is recommended to use `#[serde(default)]` on any newly-added fields to still
23/// be able to deserialize from older versions without that field.
24pub trait Storage: Sized {
25    /// For the computation type with the given computation id, return true if the
26    /// output with the given Cell has not yet been set.
27    fn output_is_unset(&self, cell: Cell, computation_id: u32) -> bool;
28
29    /// For the computation type with the given computation id, run the computation
30    /// with the corresponding Cell, returning true if the result changed from its previous value.
31    fn run_computation(db: &DbHandle<Self>, cell: Cell, computation_id: u32) -> bool;
32
33    /// Shrink the storage by removing any data not in use by the given cells
34    fn gc(&mut self, used_cells: &std::collections::HashSet<Cell>);
35}
36
37/// This trait is implemented by a type storing a single computation type `C`.
38/// Examples include `HashMapStorage<C>`, `SingletonStorage<C>`, and `BTreeMapStorage<C>`.
39///
40/// To implement this efficiently, most types implementing this are two-way maps
41/// from `C` to `Cell` and from `Cell` to `(C, Option<C::Output>)`.
42pub trait StorageFor<C: OutputType> {
43    /// Given a computation key, return the cell associated with it, if it exists.
44    fn get_cell_for_computation(&self, key: &C) -> Option<Cell>;
45
46    /// Insert a new Cell with the given computation that has yet to be run
47    fn insert_new_cell(&self, cell: Cell, key: C);
48
49    /// Retrieve the input for this computation.
50    /// The input is expected to already be inserted into this storage.
51    fn get_input(&self, cell: Cell) -> C;
52
53    /// Retrieve the output for the given cell, if it exists
54    fn get_output(&self, cell: Cell) -> Option<C::Output>;
55
56    /// `C` has been re-run and has returned the output `new_value`, return `true`
57    /// if `new_value` has changed from its previous value, and cache the new value
58    /// if needed. If `C::ASSUME_CHANGED` is true, skip the comparison and assume the value changed.
59    fn update_output(&self, cell: Cell, new_value: C::Output) -> bool;
60
61    fn gc(&mut self, used_cells: &std::collections::HashSet<Cell>);
62}
63
64pub trait ComputationId {
65    fn computation_id() -> u32;
66}
67
68pub trait OutputType {
69    type Output;
70    const IS_INPUT: bool;
71    const ASSUME_CHANGED: bool;
72}
73
74pub trait Run<Storage>: OutputType {
75    fn run(&self, db: &DbHandle<Storage>) -> Self::Output;
76}