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
170
171
172
173
174
175
176
177
use std::rc::Rc;
use std::cell::RefCell;
use progress::{Timestamp, Operate, Antichain};
use progress::frontier::MutableAntichain;
use progress::nested::subgraph::{Source, Target};
use progress::count_map::CountMap;
use dataflow::channels::pushers::Tee;
use dataflow::channels::pushers::Counter as PushCounter;
use dataflow::channels::pushers::buffer::Buffer as PushBuffer;
use dataflow::channels::pact::{ParallelizationContract, Pipeline};
use dataflow::channels::pullers::Counter as PullCounter;
use Data;
use dataflow::{Stream, Scope};
pub trait Probe<G: Scope, D: Data> {
fn probe(&self) -> (Handle<G::Timestamp>, Stream<G, D>);
}
impl<G: Scope, D: Data> Probe<G, D> for Stream<G, D> {
fn probe(&self) -> (Handle<G::Timestamp>, Stream<G, D>) {
let frontier = Rc::new(RefCell::new(MutableAntichain::new()));
let handle = Handle { frontier: frontier.clone() };
let mut scope = self.scope();
let channel_id = scope.new_identifier();
let (sender, receiver) = Pipeline.connect(&mut scope, channel_id);
let (targets, registrar) = Tee::<G::Timestamp,D>::new();
let operator = Operator {
input: PullCounter::new(receiver),
output: PushBuffer::new(PushCounter::new(targets, Rc::new(RefCell::new(CountMap::new())))),
frontier: frontier,
};
let index = scope.add_operator(operator);
self.connect_to(Target { index: index, port: 0 }, sender, channel_id);
(handle, Stream::new(Source { index: index, port: 0 }, registrar, scope))
}
}
pub struct Handle<T:Timestamp> {
frontier: Rc<RefCell<MutableAntichain<T>>>
}
impl<T: Timestamp> Handle<T> {
#[inline] pub fn lt(&self, time: &T) -> bool { self.frontier.borrow().lt(time) }
#[inline] pub fn le(&self, time: &T) -> bool { self.frontier.borrow().le(time) }
#[inline] pub fn done(&self) -> bool { self.frontier.borrow().elements().len() == 0 }
}
struct Operator<T:Timestamp, D: Data> {
input: PullCounter<T, D>,
output: PushBuffer<T, D, PushCounter<T, D, Tee<T, D>>>,
frontier: Rc<RefCell<MutableAntichain<T>>>
}
impl<T:Timestamp, D: Data> Operate<T> for Operator<T, D> {
fn name(&self) -> String { "Probe".to_owned() }
fn inputs(&self) -> usize { 1 }
fn outputs(&self) -> usize { 1 }
fn set_external_summary(&mut self, _: Vec<Vec<Antichain<T::Summary>>>, counts: &mut [CountMap<T>]) {
let mut borrow = self.frontier.borrow_mut();
while let Some((time, delta)) = counts[0].pop() {
borrow.update(&time, delta);
}
}
fn push_external_progress(&mut self, counts: &mut [CountMap<T>]) {
let mut borrow = self.frontier.borrow_mut();
while let Some((time, delta)) = counts[0].pop() {
borrow.update(&time, delta);
}
}
fn pull_internal_progress(&mut self, consumed: &mut [CountMap<T>], _: &mut [CountMap<T>], produced: &mut [CountMap<T>]) -> bool {
while let Some((time, data)) = self.input.next() {
self.output.session(time).give_content(data);
}
self.output.cease();
self.input.pull_progress(&mut consumed[0]);
self.output.inner().pull_progress(&mut produced[0]);
false
}
}
#[cfg(test)]
mod tests {
use ::Configuration;
use ::progress::timestamp::RootTimestamp;
use dataflow::*;
use dataflow::operators::{Input, Probe};
#[test]
fn probe() {
::execute(Configuration::Thread, |computation| {
let (mut input, probe) = computation.scoped(move |builder| {
let (input, stream) = builder.new_input::<String>();
(input, stream.probe().0)
});
for round in 0..10 {
assert!(!probe.done());
assert!(probe.le(&RootTimestamp::new(round)));
assert!(!probe.lt(&RootTimestamp::new(round)));
assert!(probe.lt(&RootTimestamp::new(round + 1)));
input.advance_to(round + 1);
computation.step();
}
input.close();
computation.step();
assert!(probe.done());
}).unwrap();
}
}