1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
//! Create cycles in a timely dataflow graph.
use crate::Container;
use crate::container::CapacityContainerBuilder;
use crate::dataflow::channels::pact::Pipeline;
use crate::dataflow::channels::pushers::Tee;
use crate::dataflow::operators::generic::OutputWrapper;
use crate::dataflow::operators::generic::builder_rc::OperatorBuilder;
use crate::dataflow::scopes::child::Iterative;
use crate::dataflow::{StreamCore, Scope};
use crate::order::Product;
use crate::progress::frontier::Antichain;
use crate::progress::{Timestamp, PathSummary};
/// Creates a `StreamCore` and a `Handle` to later bind the source of that `StreamCore`.
pub trait Feedback<G: Scope> {
/// Creates a [StreamCore] and a [Handle] to later bind the source of that `StreamCore`.
///
/// The resulting `StreamCore` will have its data defined by a future call to `connect_loop` with
/// its `Handle` passed as an argument. Containers passed through the stream will have their
/// timestamps advanced by `summary`.
///
/// # Examples
/// ```
/// use timely::dataflow::Scope;
/// use timely::dataflow::operators::{Feedback, ConnectLoop, ToStream, Concat, Inspect, BranchWhen};
///
/// timely::example(|scope| {
/// // circulate 0..10 for 100 iterations.
/// let (handle, cycle) = scope.feedback(1);
/// (0..10).to_stream(scope)
/// .concat(&cycle)
/// .inspect(|x| println!("seen: {:?}", x))
/// .branch_when(|t| t < &100).1
/// .connect_loop(handle);
/// });
/// ```
fn feedback<C: Container>(&mut self, summary: <G::Timestamp as Timestamp>::Summary) -> (Handle<G, C>, StreamCore<G, C>);
}
/// Creates a `StreamCore` and a `Handle` to later bind the source of that `StreamCore`.
pub trait LoopVariable<'a, G: Scope, T: Timestamp> {
/// Creates a `StreamCore` and a `Handle` to later bind the source of that `StreamCore`.
///
/// The resulting `StreamCore` will have its data defined by a future call to `connect_loop` with
/// its `Handle` passed as an argument. Containers passed through the stream will have their
/// timestamps advanced by `summary`.
///
/// # Examples
/// ```
/// use timely::dataflow::Scope;
/// use timely::dataflow::operators::{LoopVariable, ConnectLoop, ToStream, Concat, Inspect, BranchWhen};
///
/// timely::example(|scope| {
/// // circulate 0..10 for 100 iterations.
/// scope.iterative::<usize,_,_>(|inner| {
/// let (handle, cycle) = inner.loop_variable(1);
/// (0..10).to_stream(inner)
/// .concat(&cycle)
/// .inspect(|x| println!("seen: {:?}", x))
/// .branch_when(|t| t.inner < 100).1
/// .connect_loop(handle);
/// });
/// });
/// ```
fn loop_variable<C: Container>(&mut self, summary: T::Summary) -> (Handle<Iterative<'a, G, T>, C>, StreamCore<Iterative<'a, G, T>, C>);
}
impl<G: Scope> Feedback<G> for G {
fn feedback<C: Container>(&mut self, summary: <G::Timestamp as Timestamp>::Summary) -> (Handle<G, C>, StreamCore<G, C>) {
let mut builder = OperatorBuilder::new("Feedback".to_owned(), self.clone());
let (output, stream) = builder.new_output();
(Handle { builder, summary, output }, stream)
}
}
impl<'a, G: Scope, T: Timestamp> LoopVariable<'a, G, T> for Iterative<'a, G, T> {
fn loop_variable<C: Container>(&mut self, summary: T::Summary) -> (Handle<Iterative<'a, G, T>, C>, StreamCore<Iterative<'a, G, T>, C>) {
self.feedback(Product::new(Default::default(), summary))
}
}
/// Connect a `Stream` to the input of a loop variable.
pub trait ConnectLoop<G: Scope, C: Container> {
/// Connect a `Stream` to be the input of a loop variable.
///
/// # Examples
/// ```
/// use timely::dataflow::Scope;
/// use timely::dataflow::operators::{Feedback, ConnectLoop, ToStream, Concat, Inspect, BranchWhen};
///
/// timely::example(|scope| {
/// // circulate 0..10 for 100 iterations.
/// let (handle, cycle) = scope.feedback(1);
/// (0..10).to_stream(scope)
/// .concat(&cycle)
/// .inspect(|x| println!("seen: {:?}", x))
/// .branch_when(|t| t < &100).1
/// .connect_loop(handle);
/// });
/// ```
fn connect_loop(&self, handle: Handle<G, C>);
}
impl<G: Scope, C: Container> ConnectLoop<G, C> for StreamCore<G, C> {
fn connect_loop(&self, handle: Handle<G, C>) {
let mut builder = handle.builder;
let summary = handle.summary;
let mut output = handle.output;
let mut input = builder.new_input_connection(self, Pipeline, vec![Antichain::from_elem(summary.clone())]);
builder.build(move |_capability| move |_frontier| {
let mut output = output.activate();
input.for_each(|cap, data| {
if let Some(new_time) = summary.results_in(cap.time()) {
let new_cap = cap.delayed(&new_time);
output
.session(&new_cap)
.give_container(data);
}
});
});
}
}
/// A handle used to bind the source of a loop variable.
#[derive(Debug)]
pub struct Handle<G: Scope, C: Container> {
builder: OperatorBuilder<G>,
summary: <G::Timestamp as Timestamp>::Summary,
output: OutputWrapper<G::Timestamp, CapacityContainerBuilder<C>, Tee<G::Timestamp, C>>,
}