salsa/
storage.rs

1use crate::{plumbing::DatabaseStorageTypes, Runtime};
2use triomphe::Arc;
3
4/// Stores the cached results and dependency information for all the queries
5/// defined on your salsa database. Also embeds a [`Runtime`] which is used to
6/// manage query execution. Every database must include a `storage:
7/// Storage<Self>` field.
8pub struct Storage<DB: DatabaseStorageTypes> {
9    query_store: Arc<DB::DatabaseStorage>,
10    runtime: Runtime,
11}
12
13impl<DB: DatabaseStorageTypes> Default for Storage<DB> {
14    fn default() -> Self {
15        Self {
16            query_store: Default::default(),
17            runtime: Default::default(),
18        }
19    }
20}
21
22impl<DB: DatabaseStorageTypes> Storage<DB> {
23    /// Gives access to the underlying salsa runtime.
24    pub fn salsa_runtime(&self) -> &Runtime {
25        &self.runtime
26    }
27
28    /// Gives access to the underlying salsa runtime.
29    pub fn salsa_runtime_mut(&mut self) -> &mut Runtime {
30        &mut self.runtime
31    }
32
33    /// Access the query storage tables. Not meant to be used directly by end
34    /// users.
35    pub fn query_store(&self) -> &DB::DatabaseStorage {
36        &self.query_store
37    }
38
39    /// Access the query storage tables. Not meant to be used directly by end
40    /// users.
41    pub fn query_store_mut(&mut self) -> (&DB::DatabaseStorage, &mut Runtime) {
42        (&self.query_store, &mut self.runtime)
43    }
44
45    /// Returns a "snapshotted" storage, suitable for use in a forked database.
46    /// This snapshot hold a read-lock on the global state, which means that any
47    /// attempt to `set` an input will block until the forked runtime is
48    /// dropped. See `ParallelDatabase::snapshot` for more information.
49    ///
50    /// **Warning.** This second handle is intended to be used from a separate
51    /// thread. Using two database handles from the **same thread** can lead to
52    /// deadlock.
53    pub fn snapshot(&self) -> Self {
54        Storage {
55            query_store: self.query_store.clone(),
56            runtime: self.runtime.snapshot(),
57        }
58    }
59}