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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
//! Create cycles in a timely dataflow graph.
use crate::{Container, Data};
use crate::progress::{Timestamp, PathSummary};
use crate::progress::frontier::Antichain;
use crate::order::Product;
use crate::dataflow::channels::pushers::TeeCore;
use crate::dataflow::channels::pact::Pipeline;
use crate::dataflow::{StreamCore, Scope, Stream};
use crate::dataflow::scopes::child::Iterative;
use crate::dataflow::operators::generic::builder_rc::OperatorBuilder;
use crate::dataflow::operators::generic::OutputWrapper;
/// Creates a `Stream` and a `Handle` to later bind the source of that `Stream`.
pub trait Feedback<G: Scope> {
/// Creates a `Stream` and a `Handle` to later bind the source of that `Stream`.
///
/// The resulting `Stream` will have its data defined by a future call to `connect_loop` with
/// its `Handle` passed as an argument. Data 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<D: Data>(&mut self, summary: <G::Timestamp as Timestamp>::Summary) -> (Handle<G, D>, Stream<G, D>);
/// Creates a [StreamCore] and a [HandleCore] to later bind the source of that `Stream`.
///
/// The resulting `Stream` will have its data defined by a future call to `connect_loop` with
/// its `Handle` passed as an argument. Data passed through the stream will have their
/// timestamps advanced by `summary`, and will be dropped if the result exceeds `limit`.
///
/// # 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_core::<Vec<_>>(1);
/// (0..10).to_stream(scope)
/// .concat(&cycle)
/// .inspect(|x| println!("seen: {:?}", x))
/// .branch_when(|t| t < &100).1
/// .connect_loop(handle);
/// });
/// ```
fn feedback_core<D: Container>(&mut self, summary: <G::Timestamp as Timestamp>::Summary) -> (HandleCore<G, D>, StreamCore<G, D>);
}
/// Creates a `Stream` and a `Handle` to later bind the source of that `Stream`.
pub trait LoopVariable<'a, G: Scope, T: Timestamp> {
/// Creates a `Stream` and a `Handle` to later bind the source of that `Stream`.
///
/// The resulting `Stream` will have its data defined by a future call to `connect_loop` with
/// its `Handle` passed as an argument. Data 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<D: Container>(&mut self, summary: T::Summary) -> (HandleCore<Iterative<'a, G, T>, D>, StreamCore<Iterative<'a, G, T>, D>);
}
impl<G: Scope> Feedback<G> for G {
fn feedback<D: Data>(&mut self, summary: <G::Timestamp as Timestamp>::Summary) -> (Handle<G, D>, Stream<G, D>) {
self.feedback_core(summary)
}
fn feedback_core<D: Container>(&mut self, summary: <G::Timestamp as Timestamp>::Summary) -> (HandleCore<G, D>, StreamCore<G, D>) {
let mut builder = OperatorBuilder::new("Feedback".to_owned(), self.clone());
let (output, stream) = builder.new_output();
(HandleCore { builder, summary, output }, stream)
}
}
impl<'a, G: Scope, T: Timestamp> LoopVariable<'a, G, T> for Iterative<'a, G, T> {
fn loop_variable<D: Container>(&mut self, summary: T::Summary) -> (HandleCore<Iterative<'a, G, T>, D>, StreamCore<Iterative<'a, G, T>, D>) {
self.feedback_core(Product::new(Default::default(), summary))
}
}
/// Connect a `Stream` to the input of a loop variable.
pub trait ConnectLoop<G: Scope, D: 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, _: HandleCore<G, D>);
}
impl<G: Scope, D: Container> ConnectLoop<G, D> for StreamCore<G, D> {
fn connect_loop(&self, helper: HandleCore<G, D>) {
let mut builder = helper.builder;
let summary = helper.summary;
let mut output = helper.output;
let mut input = builder.new_input_connection(self, Pipeline, vec![Antichain::from_elem(summary.clone())]);
let mut vector = Default::default();
builder.build(move |_capability| move |_frontier| {
let mut output = output.activate();
input.for_each(|cap, data| {
data.swap(&mut vector);
if let Some(new_time) = summary.results_in(cap.time()) {
let new_cap = cap.delayed(&new_time);
output
.session(&new_cap)
.give_container(&mut vector);
}
});
});
}
}
/// A handle used to bind the source of a loop variable.
#[derive(Debug)]
pub struct HandleCore<G: Scope, D: Container> {
builder: OperatorBuilder<G>,
summary: <G::Timestamp as Timestamp>::Summary,
output: OutputWrapper<G::Timestamp, D, TeeCore<G::Timestamp, D>>,
}
/// A `HandleCore` specialized for using `Vec` as container
pub type Handle<G, D> = HandleCore<G, Vec<D>>;