Skip to content

feature: expose database contents as inherent method on Ingredients #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ impl<C: Configuration> IngredientImpl<C> {
&value.fields
}

#[cfg(feature = "salsa_unstable")]
/// Returns all data corresponding to the input struct.
pub fn entries<'db>(
&'db self,
db: &'db dyn crate::Database,
) -> impl Iterator<Item = &'db Value<C>> {
db.zalsa()
.table()
.pages
.iter()
.filter_map(|page| page.cast_type::<crate::table::Page<Value<C>>>())
.flat_map(|page| page.slots())
}

/// Peek at the field values without recording any read dependency.
/// Used for debug printouts.
pub fn leak_fields<'db>(&'db self, db: &'db dyn Database, id: C::Struct) -> &'db C::Fields {
Expand Down
14 changes: 14 additions & 0 deletions src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ where
self.data(db, C::deref_struct(s))
}

#[cfg(feature = "salsa_unstable")]
/// Returns all data corresponding to the interned struct.
pub fn entries<'db>(
&'db self,
db: &'db dyn crate::Database,
) -> impl Iterator<Item = &'db Value<C>> {
db.zalsa()
.table()
.pages
.iter()
.filter_map(|page| page.cast_type::<crate::table::Page<Value<C>>>())
.flat_map(|page| page.slots())
}

pub fn reset(&mut self, revision: Revision) {
assert!(revision > self.reset_at);
self.reset_at = revision;
Expand Down
47 changes: 0 additions & 47 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{marker::PhantomData, panic::RefUnwindSafe, sync::Arc};
use parking_lot::{Condvar, Mutex};

use crate::{
plumbing::{input, interned, tracked_struct},
zalsa::{Zalsa, ZalsaDatabase},
zalsa_local::{self, ZalsaLocal},
Database, Event, EventKind,
Expand Down Expand Up @@ -61,52 +60,6 @@ impl<Db: Database> Default for Storage<Db> {
}

impl<Db: Database> Storage<Db> {
pub fn debug_input_entries<T>(&self) -> impl Iterator<Item = &input::Value<T>>
where
T: input::Configuration,
{
let zalsa = self.zalsa_impl();
zalsa
.table()
.pages
.iter()
.filter_map(|page| page.cast_type::<crate::table::Page<input::Value<T>>>())
.flat_map(|page| page.slots())
}

pub fn debug_interned_entries<T>(&self) -> impl Iterator<Item = &interned::Value<T>>
where
T: interned::Configuration,
{
let zalsa = self.zalsa_impl();
zalsa
.table()
.pages
.iter()
.filter_map(|page| page.cast_type::<crate::table::Page<interned::Value<T>>>())
.flat_map(|page| page.slots())
}

pub fn debug_tracked_entries<T>(&self) -> impl Iterator<Item = &tracked_struct::Value<T>>
where
T: tracked_struct::Configuration,
{
let zalsa = self.zalsa_impl();
zalsa
.table()
.pages
.iter()
.filter_map(|page| page.cast_type::<crate::table::Page<tracked_struct::Value<T>>>())
.flat_map(|pages| pages.slots())
}

/// Access the `Arc<Zalsa>`. This should always be
/// possible as `zalsa_impl` only becomes
/// `None` once we are in the `Drop` impl.
fn zalsa_impl(&self) -> &Arc<Zalsa> {
&self.zalsa_impl
}

// ANCHOR: cancel_other_workers
/// Sets cancellation flag and blocks until all other workers with access
/// to this storage have completed.
Expand Down
14 changes: 14 additions & 0 deletions src/tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,20 @@ where

unsafe { self.to_self_ref(&data.fields) }
}

#[cfg(feature = "salsa_unstable")]
/// Returns all data corresponding to the tracked struct.
pub fn entries<'db>(
&'db self,
db: &'db dyn crate::Database,
) -> impl Iterator<Item = &'db Value<C>> {
db.zalsa()
.table()
.pages
.iter()
.filter_map(|page| page.cast_type::<crate::table::Page<Value<C>>>())
.flat_map(|page| page.slots())
}
}

impl<C> Ingredient for IngredientImpl<C>
Expand Down
16 changes: 7 additions & 9 deletions tests/debug_db_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ fn execute() {
let _ = InternedStruct::new(&db, "Salsa2".to_string());

// test interned structs
let interned = db
.storage()
.debug_interned_entries::<InternedStruct>()
let interned = InternedStruct::ingredient(&db)
.entries(&db)
.collect::<Vec<_>>();

assert_eq!(interned.len(), 2);
Expand All @@ -37,9 +36,9 @@ fn execute() {

// test input structs
let input = InputStruct::new(&db, 22);
let inputs = db
.storage()
.debug_input_entries::<InputStruct>()

let inputs = InputStruct::ingredient(&db)
.entries(&db)
.collect::<Vec<_>>();

assert_eq!(inputs.len(), 1);
Expand All @@ -48,9 +47,8 @@ fn execute() {
// test tracked structs
let computed = tracked_fn(&db, input).field(&db);
assert_eq!(computed, 44);
let tracked = db
.storage()
.debug_tracked_entries::<TrackedStruct>()
let tracked = TrackedStruct::ingredient(&db)
.entries(&db)
.collect::<Vec<_>>();

assert_eq!(tracked.len(), 1);
Expand Down