extra: clean up workcache to use & in place of @ most places.
This commit is contained in:
parent
1d9181bd76
commit
31c180e5f5
@ -124,6 +124,15 @@ struct Database {
|
||||
}
|
||||
|
||||
impl Database {
|
||||
|
||||
pub fn new(p: Path) -> Database {
|
||||
Database {
|
||||
db_filename: p,
|
||||
db_cache: TreeMap::new(),
|
||||
db_dirty: false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare(&self,
|
||||
fn_name: &str,
|
||||
declared_inputs: &WorkMap)
|
||||
@ -156,6 +165,11 @@ struct Logger {
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
|
||||
pub fn new() -> Logger {
|
||||
Logger { a: () }
|
||||
}
|
||||
|
||||
pub fn info(&self, i: &str) {
|
||||
io::println(~"workcache: " + i);
|
||||
}
|
||||
@ -163,15 +177,14 @@ impl Logger {
|
||||
|
||||
struct Context {
|
||||
db: RWARC<Database>,
|
||||
logger: @mut Logger,
|
||||
logger: Logger,
|
||||
cfg: json::Object,
|
||||
freshness: TreeMap<~str,@fn(&str,&str)->bool>
|
||||
}
|
||||
|
||||
#[deriving(Clone)]
|
||||
struct Prep {
|
||||
ctxt: @Context,
|
||||
fn_name: ~str,
|
||||
struct Prep<'self> {
|
||||
ctxt: &'self Context,
|
||||
fn_name: &'self str,
|
||||
declared_inputs: WorkMap,
|
||||
}
|
||||
|
||||
@ -180,8 +193,8 @@ struct Exec {
|
||||
discovered_outputs: WorkMap
|
||||
}
|
||||
|
||||
struct Work<T> {
|
||||
prep: @mut Prep,
|
||||
struct Work<'self, T> {
|
||||
prep: &'self Prep<'self>,
|
||||
res: Option<Either<T,PortOne<(Exec,T)>>>
|
||||
}
|
||||
|
||||
@ -215,8 +228,8 @@ fn digest_file(path: &Path) -> ~str {
|
||||
}
|
||||
|
||||
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 {
|
||||
db: db,
|
||||
logger: lg,
|
||||
@ -225,33 +238,28 @@ impl Context {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prep<T:Send +
|
||||
Encodable<json::Encoder> +
|
||||
Decodable<json::Decoder>>(@self, // FIXME(#5121)
|
||||
fn_name:&str,
|
||||
blk: &fn(@mut Prep)->Work<T>)
|
||||
-> Work<T> {
|
||||
let p = @mut Prep {
|
||||
ctxt: self,
|
||||
fn_name: fn_name.to_owned(),
|
||||
pub fn prep<'a>(&'a self, fn_name: &'a str) -> Prep<'a> {
|
||||
Prep::new(self, fn_name)
|
||||
}
|
||||
|
||||
pub fn with_prep<'a, T>(&'a self, fn_name: &'a str, blk: &fn(p: &mut Prep) -> T) -> T {
|
||||
let mut p = self.prep(fn_name);
|
||||
blk(&mut p)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
};
|
||||
blk(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 {
|
||||
impl<'self> Prep<'self> {
|
||||
fn declare_input(&mut self, kind:&str, name:&str, val:&str) {
|
||||
self.declared_inputs.insert(WorkKey::new(kind, name),
|
||||
val.to_owned());
|
||||
@ -286,22 +294,28 @@ impl TPrep for Prep {
|
||||
}
|
||||
|
||||
fn exec<T:Send +
|
||||
Encodable<json::Encoder> +
|
||||
Decodable<json::Decoder>>( // FIXME(#5121)
|
||||
&self, blk: ~fn(&Exec) -> T) -> Work<T> {
|
||||
Encodable<json::Encoder> +
|
||||
Decodable<json::Decoder>>(
|
||||
&'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 cached = do self.ctxt.db.read |db| {
|
||||
db.prepare(self.fn_name, &self.declared_inputs)
|
||||
};
|
||||
|
||||
match cached {
|
||||
let res = match cached {
|
||||
Some((ref disc_in, ref disc_out, ref res))
|
||||
if self.all_fresh("declared input",
|
||||
&self.declared_inputs) &&
|
||||
self.all_fresh("discovered input", disc_in) &&
|
||||
self.all_fresh("discovered output", disc_out) => {
|
||||
Work::new(@mut (*self).clone(), Left(json_decode(*res)))
|
||||
if self.all_fresh("declared input",&self.declared_inputs) &&
|
||||
self.all_fresh("discovered input", disc_in) &&
|
||||
self.all_fresh("discovered output", disc_out) => {
|
||||
Left(json_decode(*res))
|
||||
}
|
||||
|
||||
_ => {
|
||||
@ -318,16 +332,19 @@ impl TPrep for Prep {
|
||||
let v = blk(&exe);
|
||||
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> +
|
||||
Decodable<json::Decoder>> Work<T> { // FIXME(#5121)
|
||||
pub fn new(p: @mut Prep, e: Either<T,PortOne<(Exec,T)>>) -> Work<T> {
|
||||
Decodable<json::Decoder>>
|
||||
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) }
|
||||
}
|
||||
|
||||
@ -357,19 +374,16 @@ impl<T:Send +
|
||||
fn test() {
|
||||
use std::io::WriterUtil;
|
||||
|
||||
let db = RWARC(Database { db_filename: Path("db.json"),
|
||||
db_cache: TreeMap::new(),
|
||||
db_dirty: false });
|
||||
let lg = @mut Logger { a: () };
|
||||
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 pth = Path("foo.c");
|
||||
{
|
||||
let r = io::file_writer(&pth, [io::Create]);
|
||||
r.get_ref().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));
|
||||
do prep.exec |_exe| {
|
||||
let out = Path("foo.o");
|
||||
@ -377,6 +391,5 @@ fn test() {
|
||||
out.to_str()
|
||||
}
|
||||
};
|
||||
let s = w.unwrap();
|
||||
io::println(s);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user