#![warn(rust_2018_idioms)]
#![warn(missing_docs)]
mod blocking_future;
mod derived;
mod doctest;
mod durability;
mod input;
mod intern_id;
mod interned;
mod lru;
mod revision;
mod runtime;
mod storage;
pub mod debug;
#[doc(hidden)]
pub mod plumbing;
use crate::plumbing::DerivedQueryStorageOps;
use crate::plumbing::InputQueryStorageOps;
use crate::plumbing::LruQueryStorageOps;
use crate::plumbing::QueryStorageMassOps;
use crate::plumbing::QueryStorageOps;
pub use crate::revision::Revision;
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::sync::Arc;
pub use crate::durability::Durability;
pub use crate::intern_id::InternId;
pub use crate::interned::InternKey;
pub use crate::runtime::Runtime;
pub use crate::runtime::RuntimeId;
pub use crate::storage::Storage;
pub trait Database: plumbing::DatabaseOps {
fn sweep_all(&self, strategy: SweepStrategy) {
let runtime = self.salsa_runtime();
self.for_each_query(&mut |query_storage| query_storage.sweep(runtime, strategy));
}
fn salsa_event(&self, event_fn: Event) {
#![allow(unused_variables)]
}
fn on_propagated_panic(&self) -> ! {
panic!("concurrent salsa query panicked")
}
fn salsa_runtime(&self) -> &Runtime {
self.ops_salsa_runtime()
}
fn salsa_runtime_mut(&mut self) -> &mut Runtime {
self.ops_salsa_runtime_mut()
}
}
pub struct Event {
pub runtime_id: RuntimeId,
pub kind: EventKind,
}
impl fmt::Debug for Event {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Event")
.field("runtime_id", &self.runtime_id)
.field("kind", &self.kind)
.finish()
}
}
pub enum EventKind {
DidValidateMemoizedValue {
database_key: DatabaseKeyIndex,
},
WillBlockOn {
other_runtime_id: RuntimeId,
database_key: DatabaseKeyIndex,
},
WillExecute {
database_key: DatabaseKeyIndex,
},
}
impl fmt::Debug for EventKind {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EventKind::DidValidateMemoizedValue { database_key } => fmt
.debug_struct("DidValidateMemoizedValue")
.field("database_key", database_key)
.finish(),
EventKind::WillBlockOn {
other_runtime_id,
database_key,
} => fmt
.debug_struct("WillBlockOn")
.field("other_runtime_id", other_runtime_id)
.field("database_key", database_key)
.finish(),
EventKind::WillExecute { database_key } => fmt
.debug_struct("WillExecute")
.field("database_key", database_key)
.finish(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum DiscardIf {
Never,
Outdated,
Always,
}
impl Default for DiscardIf {
fn default() -> DiscardIf {
DiscardIf::Never
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum DiscardWhat {
Nothing,
Values,
Everything,
}
impl Default for DiscardWhat {
fn default() -> DiscardWhat {
DiscardWhat::Nothing
}
}
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct SweepStrategy {
discard_if: DiscardIf,
discard_what: DiscardWhat,
shrink_to_fit: bool,
}
impl SweepStrategy {
pub fn discard_outdated() -> SweepStrategy {
SweepStrategy::default()
.discard_everything()
.sweep_outdated()
}
pub fn discard_values(self) -> SweepStrategy {
SweepStrategy {
discard_what: self.discard_what.max(DiscardWhat::Values),
..self
}
}
pub fn discard_everything(self) -> SweepStrategy {
SweepStrategy {
discard_what: self.discard_what.max(DiscardWhat::Everything),
..self
}
}
pub fn sweep_outdated(self) -> SweepStrategy {
SweepStrategy {
discard_if: self.discard_if.max(DiscardIf::Outdated),
..self
}
}
pub fn sweep_all_revisions(self) -> SweepStrategy {
SweepStrategy {
discard_if: self.discard_if.max(DiscardIf::Always),
..self
}
}
}
pub trait ParallelDatabase: Database + Send {
fn snapshot(&self) -> Snapshot<Self>;
}
#[derive(Debug)]
pub struct Snapshot<DB: ?Sized>
where
DB: ParallelDatabase,
{
db: DB,
}
impl<DB> Snapshot<DB>
where
DB: ParallelDatabase,
{
pub fn new(db: DB) -> Self {
Snapshot { db }
}
}
impl<DB> std::ops::Deref for Snapshot<DB>
where
DB: ParallelDatabase,
{
type Target = DB;
fn deref(&self) -> &DB {
&self.db
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct DatabaseKeyIndex {
group_index: u16,
query_index: u16,
key_index: u32,
}
impl DatabaseKeyIndex {
#[inline]
pub fn group_index(self) -> u16 {
self.group_index
}
#[inline]
pub fn query_index(self) -> u16 {
self.query_index
}
#[inline]
pub fn key_index(self) -> u32 {
self.key_index
}
pub fn debug<D: ?Sized>(self, db: &D) -> impl std::fmt::Debug + '_
where
D: plumbing::DatabaseOps,
{
DatabaseKeyIndexDebug { index: self, db }
}
}
struct DatabaseKeyIndexDebug<'me, D: ?Sized>
where
D: plumbing::DatabaseOps,
{
index: DatabaseKeyIndex,
db: &'me D,
}
impl<D: ?Sized> std::fmt::Debug for DatabaseKeyIndexDebug<'_, D>
where
D: plumbing::DatabaseOps,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.db.fmt_index(self.index, fmt)
}
}
pub trait QueryDb<'d>: Sized {
type DynDb: ?Sized + Database + HasQueryGroup<Self::Group> + 'd;
type Group: plumbing::QueryGroup<GroupStorage = Self::GroupStorage>;
type GroupStorage;
}
pub trait Query: Debug + Default + Sized + for<'d> QueryDb<'d> {
type Key: Clone + Debug + Hash + Eq;
type Value: Clone + Debug;
type Storage;
const QUERY_INDEX: u16;
const QUERY_NAME: &'static str;
fn query_storage<'a>(
group_storage: &'a <Self as QueryDb<'_>>::GroupStorage,
) -> &'a Arc<Self::Storage>;
}
pub struct QueryTable<'me, Q>
where
Q: Query,
{
db: &'me <Q as QueryDb<'me>>::DynDb,
storage: &'me Q::Storage,
}
impl<'me, Q> QueryTable<'me, Q>
where
Q: Query,
Q::Storage: QueryStorageOps<Q>,
{
pub fn new(db: &'me <Q as QueryDb<'me>>::DynDb, storage: &'me Q::Storage) -> Self {
Self { db, storage }
}
pub fn get(&self, key: Q::Key) -> Q::Value {
self.try_get(key).unwrap_or_else(|err| panic!("{}", err))
}
fn try_get(&self, key: Q::Key) -> Result<Q::Value, CycleError<DatabaseKeyIndex>> {
self.storage.try_fetch(self.db, &key)
}
pub fn sweep(&self, strategy: SweepStrategy)
where
Q::Storage: plumbing::QueryStorageMassOps,
{
self.storage.sweep(self.db.salsa_runtime(), strategy);
}
pub fn purge(&self)
where
Q::Storage: plumbing::QueryStorageMassOps,
{
self.storage.purge();
}}
pub struct QueryTableMut<'me, Q>
where
Q: Query + 'me,
{
db: &'me mut <Q as QueryDb<'me>>::DynDb,
storage: Arc<Q::Storage>,
}
impl<'me, Q> QueryTableMut<'me, Q>
where
Q: Query,
{
pub fn new(db: &'me mut <Q as QueryDb<'me>>::DynDb, storage: Arc<Q::Storage>) -> Self {
Self { db, storage }
}
pub fn set(&mut self, key: Q::Key, value: Q::Value)
where
Q::Storage: plumbing::InputQueryStorageOps<Q>,
{
self.set_with_durability(key, value, Durability::LOW);
}
pub fn set_with_durability(&mut self, key: Q::Key, value: Q::Value, durability: Durability)
where
Q::Storage: plumbing::InputQueryStorageOps<Q>,
{
self.storage.set(self.db, &key, value, durability);
}
pub fn set_lru_capacity(&self, cap: usize)
where
Q::Storage: plumbing::LruQueryStorageOps,
{
self.storage.set_lru_capacity(cap);
}
pub fn invalidate(&mut self, key: &Q::Key)
where
Q::Storage: plumbing::DerivedQueryStorageOps<Q>,
{
self.storage.invalidate(self.db, key)
}
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct CycleError<K> {
cycle: Vec<K>,
changed_at: Revision,
durability: Durability,
}
impl<K> fmt::Display for CycleError<K>
where
K: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Internal error, cycle detected:\n")?;
for i in &self.cycle {
writeln!(f, "{:?}", i)?;
}
Ok(())
}
}
#[allow(unused_imports)]
#[macro_use]
extern crate salsa_macros;
use plumbing::HasQueryGroup;
#[doc(hidden)]
pub use salsa_macros::*;