extra::workcache: Remodel the (internal) struct Work
Using an enum with two cases for `Work` reveals simpler code than the previous `Option<Either<X, Y>>` representation.
This commit is contained in:
parent
830ac37ca2
commit
15c9dc7a86
1 changed files with 16 additions and 16 deletions
|
@ -19,7 +19,6 @@ use arc::{Arc,RWArc};
|
||||||
use treemap::TreeMap;
|
use treemap::TreeMap;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::comm::{PortOne, oneshot};
|
use std::comm::{PortOne, oneshot};
|
||||||
use std::either::{Either, Left, Right};
|
|
||||||
use std::{io, os, task};
|
use std::{io, os, task};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -252,9 +251,9 @@ struct Exec {
|
||||||
discovered_outputs: WorkMap
|
discovered_outputs: WorkMap
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Work<'self, T> {
|
enum Work<'self, T> {
|
||||||
prep: &'self Prep<'self>,
|
WorkValue(T),
|
||||||
res: Option<Either<T,PortOne<(Exec,T)>>>
|
WorkFromTask(&'self Prep<'self>, PortOne<(Exec, T)>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
|
fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str {
|
||||||
|
@ -426,7 +425,7 @@ impl<'self> Prep<'self> {
|
||||||
db.prepare(self.fn_name, &self.declared_inputs)
|
db.prepare(self.fn_name, &self.declared_inputs)
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = match cached {
|
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",&self.declared_inputs) &&
|
if self.all_fresh("declared input",&self.declared_inputs) &&
|
||||||
self.all_fresh("discovered input", disc_in) &&
|
self.all_fresh("discovered input", disc_in) &&
|
||||||
|
@ -434,7 +433,7 @@ impl<'self> Prep<'self> {
|
||||||
debug!("Cache hit!");
|
debug!("Cache hit!");
|
||||||
debug!("Trying to decode: %? / %? / %?",
|
debug!("Trying to decode: %? / %? / %?",
|
||||||
disc_in, disc_out, *res);
|
disc_in, disc_out, *res);
|
||||||
Left(json_decode(*res))
|
Work::from_value(json_decode(*res))
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -453,10 +452,9 @@ impl<'self> Prep<'self> {
|
||||||
let v = blk(&mut exe);
|
let v = blk(&mut exe);
|
||||||
chan.send((exe, v));
|
chan.send((exe, v));
|
||||||
}
|
}
|
||||||
Right(port)
|
Work::from_task(self, port)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
Work::new(self, res)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -465,16 +463,18 @@ impl<'self, T:Send +
|
||||||
Decodable<json::Decoder>>
|
Decodable<json::Decoder>>
|
||||||
Work<'self, T> { // FIXME(#5121)
|
Work<'self, T> { // FIXME(#5121)
|
||||||
|
|
||||||
pub fn new(p: &'self Prep<'self>, e: Either<T,PortOne<(Exec,T)>>) -> Work<'self, T> {
|
pub fn from_value(elt: T) -> Work<'self, T> {
|
||||||
Work { prep: p, res: Some(e) }
|
WorkValue(elt)
|
||||||
|
}
|
||||||
|
pub fn from_task(prep: &'self Prep<'self>, port: PortOne<(Exec, T)>)
|
||||||
|
-> Work<'self, T> {
|
||||||
|
WorkFromTask(prep, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unwrap(self) -> T {
|
pub fn unwrap(self) -> T {
|
||||||
let Work { prep, res } = self;
|
match self {
|
||||||
match res {
|
WorkValue(v) => v,
|
||||||
None => fail!(),
|
WorkFromTask(prep, port) => {
|
||||||
Some(Left(v)) => v,
|
|
||||||
Some(Right(port)) => {
|
|
||||||
let (exe, v) = port.recv();
|
let (exe, v) = port.recv();
|
||||||
let s = json_encode(&v);
|
let s = json_encode(&v);
|
||||||
do prep.ctxt.db.write |db| {
|
do prep.ctxt.db.write |db| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue