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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::rc::Rc;
use std::cell::RefCell;
use progress::Timestamp;
use progress::frontier::MutableAntichain;
use dataflow::channels::pushers::Counter as PushCounter;
use dataflow::channels::pushers::buffer::Buffer as PushBuffer;
use dataflow::channels::pact::Pipeline;
use dataflow::channels::pullers::Counter as PullCounter;
use dataflow::operators::generic::builder_raw::OperatorBuilder;
use Data;
use dataflow::{Stream, Scope};
pub trait Probe<G: Scope, D: Data> {
fn probe(&self) -> Handle<G::Timestamp>;
fn probe_with(&self, handle: &mut Handle<G::Timestamp>) -> Stream<G, D>;
}
impl<G: Scope, D: Data> Probe<G, D> for Stream<G, D> {
fn probe(&self) -> Handle<G::Timestamp> {
let mut handle = Handle::<G::Timestamp>::new();
self.probe_with(&mut handle);
handle
}
fn probe_with(&self, handle: &mut Handle<G::Timestamp>) -> Stream<G, D> {
let mut builder = OperatorBuilder::new("Probe".to_owned(), self.scope());
let mut input = PullCounter::new(builder.new_input(self, Pipeline));
let (tee, stream) = builder.new_output();
let mut output = PushBuffer::new(PushCounter::new(tee));
let frontier = handle.frontier.clone();
let mut started = false;
builder.build(
move |changes| {
frontier.borrow_mut().update_iter(changes[0].drain());
},
move |consumed, internal, produced| {
if !started {
internal[0].update(Default::default(), -1);
started = true;
}
while let Some((time, data)) = input.next() {
output.session(time).give_content(data);
}
output.cease();
input.consumed().borrow_mut().drain_into(&mut consumed[0]);
output.inner().produced().borrow_mut().drain_into(&mut produced[0]);
false
},
);
stream
}
}
pub struct Handle<T:Timestamp> {
frontier: Rc<RefCell<MutableAntichain<T>>>
}
impl<T: Timestamp> Handle<T> {
#[inline] pub fn less_than(&self, time: &T) -> bool { self.frontier.borrow().less_than(time) }
#[inline] pub fn less_equal(&self, time: &T) -> bool { self.frontier.borrow().less_equal(time) }
#[inline] pub fn done(&self) -> bool { self.frontier.borrow().is_empty() }
#[inline] pub fn new() -> Self { Handle { frontier: Rc::new(RefCell::new(MutableAntichain::new())) } }
#[inline]
pub fn with_frontier<R, F: FnMut(&[T])->R>(&self, mut function: F) -> R {
function(self.frontier.borrow().frontier())
}
}
impl<T: Timestamp> Clone for Handle<T> {
fn clone(&self) -> Self {
Handle {
frontier: self.frontier.clone()
}
}
}
#[cfg(test)]
mod tests {
use ::Configuration;
use ::progress::timestamp::RootTimestamp;
use dataflow::operators::{Input, Probe};
#[test]
fn probe() {
::execute(Configuration::Thread, |worker| {
let (mut input, probe) = worker.dataflow(move |scope| {
let (input, stream) = scope.new_input::<String>();
(input, stream.probe())
});
for round in 0..10 {
assert!(!probe.done());
assert!(probe.less_equal(&RootTimestamp::new(round)));
assert!(probe.less_than(&RootTimestamp::new(round + 1)));
input.advance_to(round + 1);
worker.step();
}
input.close();
worker.step();
worker.step();
worker.step();
worker.step();
assert!(probe.done());
}).unwrap();
}
}