2022-08-15 10:13:54 +00:00
|
|
|
//! Delete cascade:
|
|
|
|
//!
|
|
|
|
//! * when we delete memoized data, also delete outputs from that data
|
|
|
|
|
2024-06-18 07:40:21 +00:00
|
|
|
mod common;
|
|
|
|
use common::{HasLogger, Logger};
|
2022-08-15 10:13:54 +00:00
|
|
|
|
|
|
|
use expect_test::expect;
|
2024-07-15 11:46:23 +00:00
|
|
|
use salsa::Setter;
|
2022-08-15 10:13:54 +00:00
|
|
|
use test_log::test;
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::db]
|
|
|
|
trait Db: salsa::Database + HasLogger {}
|
2022-08-15 10:13:54 +00:00
|
|
|
|
2022-09-02 01:54:21 +00:00
|
|
|
#[salsa::input(singleton)]
|
2022-08-15 10:13:54 +00:00
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
|
|
|
fn final_result(db: &dyn Db, input: MyInput) -> u32 {
|
|
|
|
db.push_log(format!("final_result({:?})", input));
|
|
|
|
let mut sum = 0;
|
|
|
|
for tracked_struct in create_tracked_structs(db, input) {
|
|
|
|
sum += contribution_from_struct(db, tracked_struct);
|
|
|
|
}
|
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-05-18 10:56:04 +00:00
|
|
|
struct MyTracked<'db> {
|
2022-08-15 10:13:54 +00:00
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-19 13:13:48 +00:00
|
|
|
fn create_tracked_structs(db: &dyn Db, input: MyInput) -> Vec<MyTracked<'_>> {
|
2022-08-15 10:13:54 +00:00
|
|
|
db.push_log(format!("intermediate_result({:?})", input));
|
|
|
|
(0..input.field(db))
|
|
|
|
.map(|i| MyTracked::new(db, i))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-05-18 10:56:04 +00:00
|
|
|
fn contribution_from_struct<'db>(db: &'db dyn Db, tracked: MyTracked<'db>) -> u32 {
|
2022-08-15 10:13:54 +00:00
|
|
|
let m = MyTracked::new(db, tracked.field(db));
|
|
|
|
copy_field(db, m) * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-05-18 10:56:04 +00:00
|
|
|
fn copy_field<'db>(db: &'db dyn Db, tracked: MyTracked<'db>) -> u32 {
|
2022-08-15 10:13:54 +00:00
|
|
|
tracked.field(db)
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::db]
|
2022-08-15 10:13:54 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
storage: salsa::Storage<Self>,
|
|
|
|
logger: Logger,
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::db]
|
2022-08-15 10:13:54 +00:00
|
|
|
impl salsa::Database for Database {
|
|
|
|
fn salsa_event(&self, event: salsa::Event) {
|
|
|
|
match event.kind {
|
|
|
|
salsa::EventKind::WillDiscardStaleOutput { .. }
|
|
|
|
| salsa::EventKind::DidDiscard { .. } => {
|
2024-07-15 11:46:23 +00:00
|
|
|
self.push_log(format!("salsa_event({:?})", event.kind));
|
2022-08-15 10:13:54 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::db]
|
2022-08-15 10:13:54 +00:00
|
|
|
impl Db for Database {}
|
|
|
|
|
|
|
|
impl HasLogger for Database {
|
|
|
|
fn logger(&self) -> &Logger {
|
|
|
|
&self.logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
let mut db = Database::default();
|
|
|
|
|
|
|
|
// Creates 3 tracked structs
|
2022-09-25 01:23:36 +00:00
|
|
|
let input = MyInput::new(&db, 3);
|
2022-08-24 16:43:29 +00:00
|
|
|
assert_eq!(final_result(&db, input), 2 * 2 + 2);
|
2022-08-15 10:13:54 +00:00
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-07-15 11:46:23 +00:00
|
|
|
"final_result(MyInput { [salsa id]: 0, field: 3 })",
|
|
|
|
"intermediate_result(MyInput { [salsa id]: 0, field: 3 })",
|
2022-08-15 10:13:54 +00:00
|
|
|
]"#]]);
|
|
|
|
|
|
|
|
// Creates only 2 tracked structs in this revision, should delete 1
|
|
|
|
//
|
|
|
|
// Expect to see 6 DidDiscard events. Three from the primary struct:
|
|
|
|
//
|
|
|
|
// * the struct itself
|
|
|
|
// * the struct's field
|
|
|
|
// * the `contribution_from_struct` result
|
|
|
|
//
|
|
|
|
// and then 3 more from the struct created by `contribution_from_struct`:
|
|
|
|
//
|
|
|
|
// * the struct itself
|
|
|
|
// * the struct's field
|
|
|
|
// * the `copy_field` result
|
|
|
|
|
2022-08-22 10:32:04 +00:00
|
|
|
input.set_field(&mut db).to(2);
|
2022-08-24 16:43:29 +00:00
|
|
|
assert_eq!(final_result(&db, input), 2);
|
2022-08-15 10:13:54 +00:00
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-07-15 11:46:23 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: 0, field: 2 })",
|
2022-08-22 07:23:58 +00:00
|
|
|
"salsa_event(WillDiscardStaleOutput { execute_key: create_tracked_structs(0), output_key: MyTracked(2) })",
|
|
|
|
"salsa_event(DidDiscard { key: MyTracked(2) })",
|
|
|
|
"salsa_event(DidDiscard { key: contribution_from_struct(2) })",
|
|
|
|
"salsa_event(DidDiscard { key: MyTracked(5) })",
|
|
|
|
"salsa_event(DidDiscard { key: copy_field(5) })",
|
2024-07-15 11:46:23 +00:00
|
|
|
"final_result(MyInput { [salsa id]: 0, field: 2 })",
|
2022-08-15 10:13:54 +00:00
|
|
|
]"#]]);
|
|
|
|
}
|