1
Fork 0

extra: clean up workcache to use & in place of @ most places.

This commit is contained in:
Graydon Hoare 2013-07-17 21:12:36 -07:00
parent 1d9181bd76
commit 31c180e5f5

View file

@ -124,6 +124,15 @@ struct Database {
} }
impl Database { impl Database {
pub fn new(p: Path) -> Database {
Database {
db_filename: p,
db_cache: TreeMap::new(),
db_dirty: false
}
}
pub fn prepare(&self, pub fn prepare(&self,
fn_name: &str, fn_name: &str,
declared_inputs: &WorkMap) declared_inputs: &WorkMap)
@ -156,6 +165,11 @@ struct Logger {
} }
impl Logger { impl Logger {
pub fn new() -> Logger {
Logger { a: () }
}
pub fn info(&self, i: &str) { pub fn info(&self, i: &str) {
io::println(~"workcache: " + i); io::println(~"workcache: " + i);
} }
@ -163,15 +177,14 @@ impl Logger {
struct Context { struct Context {
db: RWARC<Database>, db: RWARC<Database>,
logger: @mut Logger, logger: Logger,
cfg: json::Object, cfg: json::Object,
freshness: TreeMap<~str,@fn(&str,&str)->bool> freshness: TreeMap<~str,@fn(&str,&str)->bool>
} }
#[deriving(Clone)] struct Prep<'self> {
struct Prep { ctxt: &'self Context,
ctxt: @Context, fn_name: &'self str,
fn_name: ~str,
declared_inputs: WorkMap, declared_inputs: WorkMap,
} }
@ -180,8 +193,8 @@ struct Exec {
discovered_outputs: WorkMap discovered_outputs: WorkMap
} }
struct Work<T> { struct Work<'self, T> {
prep: @mut Prep, prep: &'self Prep<'self>,
res: Option<Either<T,PortOne<(Exec,T)>>> res: Option<Either<T,PortOne<(Exec,T)>>>
} }
@ -215,8 +228,8 @@ fn digest_file(path: &Path) -> ~str {
} }
impl Context { impl Context {
pub fn new(db: RWARC<Database>, lg: @mut Logger, cfg: json::Object)
-> Context { pub fn new(db: RWARC<Database>, lg: Logger, cfg: json::Object) -> Context {
Context { Context {
db: db, db: db,
logger: lg, logger: lg,
@ -225,33 +238,28 @@ impl Context {
} }
} }
pub fn prep<T:Send + pub fn prep<'a>(&'a self, fn_name: &'a str) -> Prep<'a> {
Encodable<json::Encoder> + Prep::new(self, fn_name)
Decodable<json::Decoder>>(@self, // FIXME(#5121) }
fn_name:&str,
blk: &fn(@mut Prep)->Work<T>) pub fn with_prep<'a, T>(&'a self, fn_name: &'a str, blk: &fn(p: &mut Prep) -> T) -> T {
-> Work<T> { let mut p = self.prep(fn_name);
let p = @mut Prep { blk(&mut p)
ctxt: self, }
fn_name: fn_name.to_owned(),
}
impl<'self> Prep<'self> {
fn new(ctxt: &'self Context, fn_name: &'self str) -> Prep<'self> {
Prep {
ctxt: ctxt,
fn_name: fn_name,
declared_inputs: WorkMap::new() declared_inputs: WorkMap::new()
}; }
blk(p)
} }
} }
impl<'self> Prep<'self> {
trait TPrep {
fn declare_input(&mut self, kind:&str, name:&str, val:&str);
fn is_fresh(&self, cat:&str, kind:&str, name:&str, val:&str) -> bool;
fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool;
fn exec<T:Send +
Encodable<json::Encoder> +
Decodable<json::Decoder>>( // FIXME(#5121)
&self, blk: ~fn(&Exec) -> T) -> Work<T>;
}
impl TPrep for Prep {
fn declare_input(&mut self, kind:&str, name:&str, val:&str) { fn declare_input(&mut self, kind:&str, name:&str, val:&str) {
self.declared_inputs.insert(WorkKey::new(kind, name), self.declared_inputs.insert(WorkKey::new(kind, name),
val.to_owned()); val.to_owned());
@ -286,22 +294,28 @@ impl TPrep for Prep {
} }
fn exec<T:Send + fn exec<T:Send +
Encodable<json::Encoder> + Encodable<json::Encoder> +
Decodable<json::Decoder>>( // FIXME(#5121) Decodable<json::Decoder>>(
&self, blk: ~fn(&Exec) -> T) -> Work<T> { &'self self, blk: ~fn(&Exec) -> T) -> T {
self.exec_work(blk).unwrap()
}
fn exec_work<T:Send +
Encodable<json::Encoder> +
Decodable<json::Decoder>>( // FIXME(#5121)
&'self self, blk: ~fn(&Exec) -> T) -> Work<'self, T> {
let mut bo = Some(blk); let mut bo = Some(blk);
let cached = do self.ctxt.db.read |db| { let cached = do self.ctxt.db.read |db| {
db.prepare(self.fn_name, &self.declared_inputs) db.prepare(self.fn_name, &self.declared_inputs)
}; };
match cached { let res = match cached {
Some((ref disc_in, ref disc_out, ref res)) Some((ref disc_in, ref disc_out, ref res))
if self.all_fresh("declared input", if self.all_fresh("declared input",&self.declared_inputs) &&
&self.declared_inputs) && self.all_fresh("discovered input", disc_in) &&
self.all_fresh("discovered input", disc_in) && self.all_fresh("discovered output", disc_out) => {
self.all_fresh("discovered output", disc_out) => { Left(json_decode(*res))
Work::new(@mut (*self).clone(), Left(json_decode(*res)))
} }
_ => { _ => {
@ -318,16 +332,19 @@ impl TPrep for Prep {
let v = blk(&exe); let v = blk(&exe);
send_one(chan, (exe, v)); send_one(chan, (exe, v));
} }
Work::new(@mut (*self).clone(), Right(port)) Right(port)
} }
} };
Work::new(self, res)
} }
} }
impl<T:Send + impl<'self, T:Send +
Encodable<json::Encoder> + Encodable<json::Encoder> +
Decodable<json::Decoder>> Work<T> { // FIXME(#5121) Decodable<json::Decoder>>
pub fn new(p: @mut Prep, e: Either<T,PortOne<(Exec,T)>>) -> Work<T> { Work<'self, T> { // FIXME(#5121)
pub fn new(p: &'self Prep<'self>, e: Either<T,PortOne<(Exec,T)>>) -> Work<'self, T> {
Work { prep: p, res: Some(e) } Work { prep: p, res: Some(e) }
} }
@ -357,19 +374,16 @@ impl<T:Send +
fn test() { fn test() {
use std::io::WriterUtil; use std::io::WriterUtil;
let db = RWARC(Database { db_filename: Path("db.json"), let pth = Path("foo.c");
db_cache: TreeMap::new(), {
db_dirty: false }); let r = io::file_writer(&pth, [io::Create]);
let lg = @mut Logger { a: () }; r.get_ref().write_str("int main() { return 0; }");
let cfg = HashMap::new(); }
let cx = @Context::new(db, lg, cfg);
let w:Work<~str> = do cx.prep("test1") |prep| {
let pth = Path("foo.c");
{
let file = io::file_writer(&pth, [io::Create]).unwrap();
file.write_str("int main() { return 0; }");
}
let cx = Context::new(RWARC(Database::new(Path("db.json"))),
Logger::new(), HashMap::new());
let s = do cx.with_prep("test1") |prep| {
prep.declare_input("file", pth.to_str(), digest_file(&pth)); prep.declare_input("file", pth.to_str(), digest_file(&pth));
do prep.exec |_exe| { do prep.exec |_exe| {
let out = Path("foo.o"); let out = Path("foo.o");
@ -377,6 +391,5 @@ fn test() {
out.to_str() out.to_str()
} }
}; };
let s = w.unwrap();
io::println(s); io::println(s);
} }