ra_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 { query_store: Default::default(), runtime: Default::default() }
16    }
17}
18
19impl<DB: DatabaseStorageTypes> Storage<DB> {
20    /// Gives access to the underlying salsa runtime.
21    pub fn salsa_runtime(&self) -> &Runtime {
22        &self.runtime
23    }
24
25    /// Gives access to the underlying salsa runtime.
26    pub fn salsa_runtime_mut(&mut self) -> &mut Runtime {
27        &mut self.runtime
28    }
29
30    /// Access the query storage tables. Not meant to be used directly by end
31    /// users.
32    pub fn query_store(&self) -> &DB::DatabaseStorage {
33        &self.query_store
34    }
35
36    /// Access the query storage tables. Not meant to be used directly by end
37    /// users.
38    pub fn query_store_mut(&mut self) -> (&DB::DatabaseStorage, &mut Runtime) {
39        (&self.query_store, &mut self.runtime)
40    }
41
42    /// Returns a "snapshotted" storage, suitable for use in a forked database.
43    /// This snapshot hold a read-lock on the global state, which means that any
44    /// attempt to `set` an input will block until the forked runtime is
45    /// dropped. See `ParallelDatabase::snapshot` for more information.
46    ///
47    /// **Warning.** This second handle is intended to be used from a separate
48    /// thread. Using two database handles from the **same thread** can lead to
49    /// deadlock.
50    pub fn snapshot(&self) -> Self {
51        Storage { query_store: self.query_store.clone(), runtime: self.runtime.snapshot() }
52    }
53}