mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-23 05:07:27 +00:00
9df075b63c
Accumulators don't currently work across revisions due to a few bugs. This commit adds 2 tests to show the problems and reworks the implementation strategy. We keep track of when the values in an accumulator were pushed and reset the vector to empty when the push occurs in a new revision. We also ignore stale values from old revisions (but update the revision when it is marked as validated). Finally, we treat an accumulator as an untracked read, which is quite conservative but correct. To get better reuse, we would need to (a) somehow determine when different values were pushed, e.g. by hashing or tracked the old values; and (b) have some `DatabaseKeyIndex` we can use to identify "the values pushed by this query". Both of these would add overhead to accumulators and I didn'τ feel like doing it, particularly since the main use case for them is communicating errors and things which are not typically used from within queries.
180 lines
4.4 KiB
Rust
180 lines
4.4 KiB
Rust
//! Basic deletion test:
|
|
//!
|
|
//! * entities not created in a revision are deleted, as is any memoized data keyed on them.
|
|
|
|
use salsa::DebugWithDb;
|
|
use salsa_2022_tests::{HasLogger, Logger};
|
|
|
|
use expect_test::expect;
|
|
use test_log::test;
|
|
|
|
#[salsa::jar(db = Db)]
|
|
struct Jar(MyInput, Logs, push_logs, push_a_logs, push_b_logs);
|
|
|
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
|
|
|
#[salsa::input]
|
|
struct MyInput {
|
|
field_a: u32,
|
|
field_b: u32,
|
|
}
|
|
|
|
#[salsa::accumulator]
|
|
struct Logs(String);
|
|
|
|
#[salsa::tracked]
|
|
#[allow(dead_code)]
|
|
fn push_logs(db: &dyn Db, input: MyInput) {
|
|
db.push_log(format!(
|
|
"push_logs(a = {}, b = {})",
|
|
input.field_a(db),
|
|
input.field_b(db)
|
|
));
|
|
|
|
// We don't invoke `push_a_logs` (or `push_b_logs`) with a value of 1 or less.
|
|
// This allows us to test what happens a change in inputs causes a function not to be called at all.
|
|
if input.field_a(db) > 1 {
|
|
push_a_logs(db, input);
|
|
}
|
|
|
|
if input.field_b(db) > 1 {
|
|
push_b_logs(db, input);
|
|
}
|
|
}
|
|
|
|
#[salsa::tracked]
|
|
fn push_a_logs(db: &dyn Db, input: MyInput) {
|
|
let field_a = input.field_a(db);
|
|
db.push_log(format!("push_a_logs({})", field_a));
|
|
|
|
for i in 0..field_a {
|
|
Logs::push(db, format!("log_a({} of {})", i, field_a));
|
|
}
|
|
}
|
|
|
|
#[salsa::tracked]
|
|
fn push_b_logs(db: &dyn Db, input: MyInput) {
|
|
let field_a = input.field_b(db);
|
|
db.push_log(format!("push_b_logs({})", field_a));
|
|
|
|
for i in 0..field_a {
|
|
Logs::push(db, format!("log_b({} of {})", i, field_a));
|
|
}
|
|
}
|
|
|
|
#[salsa::db(Jar)]
|
|
#[derive(Default)]
|
|
struct Database {
|
|
storage: salsa::Storage<Self>,
|
|
logger: Logger,
|
|
}
|
|
|
|
impl salsa::Database for Database {
|
|
fn salsa_event(&self, event: salsa::Event) {}
|
|
|
|
fn salsa_runtime(&self) -> &salsa::Runtime {
|
|
self.storage.runtime()
|
|
}
|
|
}
|
|
|
|
impl Db for Database {}
|
|
|
|
impl HasLogger for Database {
|
|
fn logger(&self) -> &Logger {
|
|
&self.logger
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn accumulate_once() {
|
|
let mut db = Database::default();
|
|
|
|
// Just call accumulate on a base input to see what happens.
|
|
let input = MyInput::new(&mut db, 2, 3);
|
|
let logs = push_logs::accumulated::<Logs>(&db, input);
|
|
expect![[r#"
|
|
[
|
|
"log_b(0 of 3)",
|
|
"log_b(1 of 3)",
|
|
"log_b(2 of 3)",
|
|
"log_a(0 of 2)",
|
|
"log_a(1 of 2)",
|
|
]"#]]
|
|
.assert_eq(&format!("{:#?}", logs));
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"push_logs(a = 2, b = 3)",
|
|
"push_a_logs(2)",
|
|
"push_b_logs(3)",
|
|
]"#]])
|
|
}
|
|
|
|
#[test]
|
|
fn change_a_and_reaccumulate() {
|
|
let mut db = Database::default();
|
|
|
|
// Accumulate logs for `a = 2` and `b = 3`
|
|
let input = MyInput::new(&mut db, 2, 3);
|
|
let logs = push_logs::accumulated::<Logs>(&db, input);
|
|
expect![[r#"
|
|
[
|
|
"log_b(0 of 3)",
|
|
"log_b(1 of 3)",
|
|
"log_b(2 of 3)",
|
|
"log_a(0 of 2)",
|
|
"log_a(1 of 2)",
|
|
]"#]]
|
|
.assert_eq(&format!("{:#?}", logs));
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"push_logs(a = 2, b = 3)",
|
|
"push_a_logs(2)",
|
|
"push_b_logs(3)",
|
|
]"#]]);
|
|
|
|
// Change to `a = 1`, which means `push_logs` does not call `push_a_logs` at all
|
|
input.set_field_a(&mut db, 1);
|
|
let logs = push_logs::accumulated::<Logs>(&db, input);
|
|
expect![[r#"
|
|
[
|
|
"log_b(0 of 3)",
|
|
"log_b(1 of 3)",
|
|
"log_b(2 of 3)",
|
|
]"#]]
|
|
.assert_eq(&format!("{:#?}", logs));
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"push_logs(a = 1, b = 3)",
|
|
]"#]]);
|
|
}
|
|
|
|
#[test]
|
|
fn get_a_logs_after_changing_b() {
|
|
let mut db = Database::default();
|
|
|
|
// Invoke `push_a_logs` with `a = 2` and `b = 3` (but `b` doesn't matter)
|
|
let input = MyInput::new(&mut db, 2, 3);
|
|
let logs = push_a_logs::accumulated::<Logs>(&db, input);
|
|
expect![[r#"
|
|
[
|
|
"log_a(0 of 2)",
|
|
"log_a(1 of 2)",
|
|
]"#]]
|
|
.assert_eq(&format!("{:#?}", logs));
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"push_a_logs(2)",
|
|
]"#]]);
|
|
|
|
// Changing `b` does not cause `push_a_logs` to re-execute
|
|
// and we still get the same result
|
|
input.set_field_b(&mut db, 5);
|
|
let logs = push_a_logs::accumulated::<Logs>(&db, input);
|
|
expect![[r#"
|
|
[
|
|
"log_a(0 of 2)",
|
|
"log_a(1 of 2)",
|
|
]"#]]
|
|
.assert_debug_eq(&logs);
|
|
db.assert_logs(expect!["[]"]);
|
|
}
|