2018-09-12 13:50:15 -05:00
|
|
|
extern crate salsa;
|
|
|
|
use std::{
|
2018-09-12 14:30:48 -05:00
|
|
|
iter::once,
|
2018-09-12 13:50:15 -05:00
|
|
|
sync::Arc,
|
|
|
|
collections::hash_map::{HashMap, DefaultHasher},
|
|
|
|
any::Any,
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
};
|
|
|
|
|
|
|
|
type State = HashMap<u32, String>;
|
2018-09-12 14:11:26 -05:00
|
|
|
type Data = Arc<Any + Send + Sync + 'static>;
|
2018-09-12 13:50:15 -05:00
|
|
|
const GET_TEXT: salsa::QueryTypeId = salsa::QueryTypeId(1);
|
|
|
|
const GET_FILES: salsa::QueryTypeId = salsa::QueryTypeId(2);
|
|
|
|
const FILE_NEWLINES: salsa::QueryTypeId = salsa::QueryTypeId(3);
|
|
|
|
const TOTAL_NEWLINES: salsa::QueryTypeId = salsa::QueryTypeId(4);
|
|
|
|
|
|
|
|
fn mk_ground_query<T, R>(
|
|
|
|
state: &State,
|
2018-09-12 14:11:26 -05:00
|
|
|
params: &Data,
|
2018-09-12 13:50:15 -05:00
|
|
|
f: fn(&State, &T) -> R,
|
2018-09-12 14:11:26 -05:00
|
|
|
) -> (Data, salsa::OutputFingerprint)
|
2018-09-12 13:50:15 -05:00
|
|
|
where
|
|
|
|
T: 'static,
|
|
|
|
R: Hash + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
let params = params.downcast_ref().unwrap();
|
|
|
|
let result = f(state, params);
|
|
|
|
let fingerprint = o_print(&result);
|
2018-09-12 14:11:26 -05:00
|
|
|
(Arc::new(result), fingerprint)
|
2018-09-12 13:50:15 -05:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:11:26 -05:00
|
|
|
fn get<T, R>(db: &salsa::Db<State, Data>, query_type: salsa::QueryTypeId, param: T) -> (Arc<R>, Vec<salsa::QueryTypeId>)
|
2018-09-12 13:50:15 -05:00
|
|
|
where
|
|
|
|
T: Hash + Send + Sync + 'static,
|
|
|
|
R: Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
let i_print = i_print(¶m);
|
2018-09-12 14:11:26 -05:00
|
|
|
let param = Arc::new(param);
|
2018-09-12 13:50:15 -05:00
|
|
|
let (res, trace) = db.get(salsa::QueryId(query_type, i_print), param);
|
|
|
|
(res.downcast().unwrap(), trace)
|
|
|
|
}
|
|
|
|
|
2018-09-12 14:11:26 -05:00
|
|
|
struct QueryCtx<'a>(&'a salsa::QueryCtx<State, Data>);
|
2018-09-12 13:50:15 -05:00
|
|
|
|
|
|
|
impl<'a> QueryCtx<'a> {
|
|
|
|
fn get_text(&self, id: u32) -> Arc<String> {
|
|
|
|
let i_print = i_print(&id);
|
|
|
|
let text = self.0.get(salsa::QueryId(GET_TEXT, i_print), Arc::new(id));
|
|
|
|
text.downcast().unwrap()
|
|
|
|
}
|
|
|
|
fn get_files(&self) -> Arc<Vec<u32>> {
|
|
|
|
let i_print = i_print(&());
|
|
|
|
let files = self.0.get(salsa::QueryId(GET_FILES, i_print), Arc::new(()));
|
|
|
|
let res = files.downcast().unwrap();
|
|
|
|
res
|
|
|
|
}
|
|
|
|
fn get_n_lines(&self, id: u32) -> usize {
|
|
|
|
let i_print = i_print(&id);
|
|
|
|
let n_lines = self.0.get(salsa::QueryId(FILE_NEWLINES, i_print), Arc::new(id));
|
|
|
|
*n_lines.downcast().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_query<T, R>(
|
2018-09-12 14:11:26 -05:00
|
|
|
query_ctx: &salsa::QueryCtx<State, Data>,
|
|
|
|
params: &Data,
|
2018-09-12 13:50:15 -05:00
|
|
|
f: fn(QueryCtx, &T) -> R,
|
2018-09-12 14:11:26 -05:00
|
|
|
) -> (Data, salsa::OutputFingerprint)
|
2018-09-12 13:50:15 -05:00
|
|
|
where
|
|
|
|
T: 'static,
|
|
|
|
R: Hash + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
let params: &T = params.downcast_ref().unwrap();
|
|
|
|
let query_ctx = QueryCtx(query_ctx);
|
|
|
|
let result = f(query_ctx, params);
|
|
|
|
let fingerprint = o_print(&result);
|
2018-09-12 14:11:26 -05:00
|
|
|
(Arc::new(result), fingerprint)
|
2018-09-12 13:50:15 -05:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:11:26 -05:00
|
|
|
fn mk_queries() -> salsa::QueryConfig<State, Data> {
|
|
|
|
salsa::QueryConfig::<State, Data>::new()
|
2018-09-13 14:58:36 -05:00
|
|
|
.with_ground_query(GET_TEXT, Box::new(|state, id| {
|
2018-09-12 13:50:15 -05:00
|
|
|
mk_ground_query::<u32, String>(state, id, |state, id| state[id].clone())
|
2018-09-13 14:58:36 -05:00
|
|
|
}))
|
|
|
|
.with_ground_query(GET_FILES, Box::new(|state, id| {
|
2018-09-12 13:50:15 -05:00
|
|
|
mk_ground_query::<(), Vec<u32>>(state, id, |state, &()| state.keys().cloned().collect())
|
2018-09-13 14:58:36 -05:00
|
|
|
}))
|
|
|
|
.with_query(FILE_NEWLINES, Box::new(|query_ctx, id| {
|
2018-09-12 13:50:15 -05:00
|
|
|
mk_query(query_ctx, id, |query_ctx, &id| {
|
|
|
|
let text = query_ctx.get_text(id);
|
|
|
|
text.lines().count()
|
|
|
|
})
|
2018-09-13 14:58:36 -05:00
|
|
|
}))
|
|
|
|
.with_query(TOTAL_NEWLINES, Box::new(|query_ctx, id| {
|
2018-09-12 13:50:15 -05:00
|
|
|
mk_query(query_ctx, id, |query_ctx, &()| {
|
|
|
|
let mut total = 0;
|
|
|
|
for &id in query_ctx.get_files().iter() {
|
|
|
|
total += query_ctx.get_n_lines(id)
|
|
|
|
}
|
|
|
|
total
|
|
|
|
})
|
2018-09-13 14:58:36 -05:00
|
|
|
}))
|
2018-09-12 13:50:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_number_of_lines() {
|
|
|
|
let mut state = State::new();
|
|
|
|
let db = salsa::Db::new(mk_queries(), state.clone());
|
|
|
|
let (newlines, trace) = get::<(), usize>(&db, TOTAL_NEWLINES, ());
|
|
|
|
assert_eq!(*newlines, 0);
|
|
|
|
assert_eq!(trace.len(), 2);
|
|
|
|
let (newlines, trace) = get::<(), usize>(&db, TOTAL_NEWLINES, ());
|
|
|
|
assert_eq!(*newlines, 0);
|
|
|
|
assert_eq!(trace.len(), 0);
|
|
|
|
|
|
|
|
state.insert(1, "hello\nworld".to_string());
|
2018-09-12 14:30:48 -05:00
|
|
|
let mut inv = salsa::Invalidations::new();
|
|
|
|
inv.invalidate(GET_TEXT, once(i_print(&1u32)));
|
|
|
|
inv.invalidate(GET_FILES, once(i_print(&())));
|
|
|
|
let db = db.with_ground_data(state.clone(), inv);
|
2018-09-12 13:50:15 -05:00
|
|
|
let (newlines, trace) = get::<(), usize>(&db, TOTAL_NEWLINES, ());
|
|
|
|
assert_eq!(*newlines, 2);
|
|
|
|
assert_eq!(trace.len(), 4);
|
|
|
|
|
|
|
|
state.insert(2, "spam\neggs".to_string());
|
2018-09-12 14:30:48 -05:00
|
|
|
let mut inv = salsa::Invalidations::new();
|
|
|
|
inv.invalidate(GET_TEXT, once(i_print(&2u32)));
|
|
|
|
inv.invalidate(GET_FILES, once(i_print(&())));
|
|
|
|
let db = db.with_ground_data(state.clone(), inv);
|
2018-09-12 13:50:15 -05:00
|
|
|
let (newlines, trace) = get::<(), usize>(&db, TOTAL_NEWLINES, ());
|
|
|
|
assert_eq!(*newlines, 4);
|
2018-09-12 14:30:48 -05:00
|
|
|
assert_eq!(trace.len(), 4);
|
2018-09-12 13:50:15 -05:00
|
|
|
|
2018-09-12 14:30:48 -05:00
|
|
|
let mut invs = vec![];
|
2018-09-12 13:50:15 -05:00
|
|
|
for i in 0..10 {
|
2018-09-12 14:30:48 -05:00
|
|
|
let id = i + 10;
|
|
|
|
invs.push(i_print(&id));
|
|
|
|
state.insert(id, "spam".to_string());
|
2018-09-12 13:50:15 -05:00
|
|
|
}
|
2018-09-12 14:30:48 -05:00
|
|
|
let mut inv = salsa::Invalidations::new();
|
|
|
|
inv.invalidate(GET_TEXT, invs.into_iter());
|
|
|
|
inv.invalidate(GET_FILES, once(i_print(&())));
|
|
|
|
let db = db.with_ground_data(state.clone(), inv);
|
2018-09-12 13:50:15 -05:00
|
|
|
let (newlines, trace) = get::<(), usize>(&db, TOTAL_NEWLINES, ());
|
|
|
|
assert_eq!(*newlines, 14);
|
2018-09-12 14:30:48 -05:00
|
|
|
assert_eq!(trace.len(), 22);
|
2018-09-12 13:50:15 -05:00
|
|
|
|
|
|
|
state.insert(15, String::new());
|
2018-09-12 14:30:48 -05:00
|
|
|
let mut inv = salsa::Invalidations::new();
|
|
|
|
inv.invalidate(GET_TEXT, once(i_print(&15u32)));
|
|
|
|
inv.invalidate(GET_FILES, once(i_print(&())));
|
|
|
|
let db = db.with_ground_data(state.clone(), inv);
|
2018-09-12 13:50:15 -05:00
|
|
|
let (newlines, trace) = get::<(), usize>(&db, TOTAL_NEWLINES, ());
|
|
|
|
assert_eq!(*newlines, 13);
|
2018-09-12 14:30:48 -05:00
|
|
|
assert_eq!(trace.len(), 4);
|
2018-09-12 13:50:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn o_print<T: Hash>(x: &T) -> salsa::OutputFingerprint {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
x.hash(&mut hasher);
|
|
|
|
let hash = hasher.finish();
|
|
|
|
salsa::OutputFingerprint(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn i_print<T: Hash>(x: &T) -> salsa::InputFingerprint {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
x.hash(&mut hasher);
|
|
|
|
let hash = hasher.finish();
|
|
|
|
salsa::InputFingerprint(hash)
|
|
|
|
}
|