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
214
215
216
use std::default::Default;
use ::Data;
use progress::nested::subgraph::{Source, Target};
use progress::ChangeBatch;
use progress::{Timestamp, Operate, Antichain};
use dataflow::{Stream, Scope};
use dataflow::channels::pushers::Tee;
use dataflow::channels::pact::ParallelizationContract;
pub struct OperatorShape {
name: String,
notify: bool,
peers: usize,
inputs: usize,
outputs: usize,
}
impl OperatorShape {
fn new(name: String, peers: usize) -> Self {
OperatorShape {
name: name,
notify: true,
peers: peers,
inputs: 0,
outputs: 0,
}
}
pub fn inputs(&self) -> usize {
self.inputs
}
pub fn outputs(&self) -> usize {
self.outputs
}
}
pub struct OperatorBuilder<G: Scope> {
scope: G,
index: usize,
shape: OperatorShape,
summary: Vec<Vec<Antichain<<G::Timestamp as Timestamp>::Summary>>>,
}
impl<G: Scope> OperatorBuilder<G> {
pub fn new(name: String, mut scope: G) -> Self {
let index = scope.allocate_operator_index();
let peers = scope.peers();
OperatorBuilder {
scope: scope,
index: index,
shape: OperatorShape::new(name, peers),
summary: vec![],
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn shape(&self) -> &OperatorShape {
&self.shape
}
pub fn set_notify(&mut self, notify: bool) {
self.shape.notify = notify;
}
pub fn new_input<D: Data, P>(&mut self, stream: &Stream<G, D>, pact: P) -> P::Puller
where
P: ParallelizationContract<G::Timestamp, D> {
let connection = vec![Antichain::from_elem(Default::default()); self.shape.outputs];
self.new_input_connection(stream, pact, connection)
}
pub fn new_input_connection<D: Data, P>(&mut self, stream: &Stream<G, D>, pact: P, connection: Vec<Antichain<<G::Timestamp as Timestamp>::Summary>>) -> P::Puller
where
P: ParallelizationContract<G::Timestamp, D> {
let channel_id = self.scope.new_identifier();
let logging = self.scope.logging();
let (sender, receiver) = pact.connect(&mut self.scope, channel_id, logging);
let target = Target { index: self.index, port: self.shape.inputs };
stream.connect_to(target, sender, channel_id);
self.shape.inputs += 1;
assert_eq!(self.shape.outputs, connection.len());
self.summary.push(connection);
receiver
}
pub fn new_output<D: Data>(&mut self) -> (Tee<G::Timestamp, D>, Stream<G, D>) {
let connection = vec![Antichain::from_elem(Default::default()); self.shape.inputs];
self.new_output_connection(connection)
}
pub fn new_output_connection<D: Data>(&mut self, connection: Vec<Antichain<<G::Timestamp as Timestamp>::Summary>>) -> (Tee<G::Timestamp, D>, Stream<G, D>) {
let (targets, registrar) = Tee::<G::Timestamp,D>::new();
let source = Source { index: self.index, port: self.shape.outputs };
let stream = Stream::new(source, registrar, self.scope.clone());
self.shape.outputs += 1;
assert_eq!(self.shape.inputs, connection.len());
for (summary, entry) in self.summary.iter_mut().zip(connection.into_iter()) {
summary.push(entry);
}
(targets, stream)
}
pub fn build<PEP, PIP>(mut self, push_external: PEP, pull_internal: PIP)
where
PEP: FnMut(&mut [ChangeBatch<G::Timestamp>])+'static,
PIP: FnMut(
&mut [ChangeBatch<G::Timestamp>],
&mut [ChangeBatch<G::Timestamp>],
&mut [ChangeBatch<G::Timestamp>],
)->bool+'static
{
let operator = OperatorCore {
shape: self.shape,
push_external: push_external,
pull_internal: pull_internal,
summary: self.summary,
phantom: ::std::marker::PhantomData,
};
self.scope.add_operator_with_index(operator, self.index);
}
}
struct OperatorCore<T, PEP, PIP>
where
T: Timestamp,
PEP: FnMut(&mut [ChangeBatch<T>])+'static,
PIP: FnMut(&mut [ChangeBatch<T>], &mut [ChangeBatch<T>], &mut [ChangeBatch<T>])->bool+'static
{
shape: OperatorShape,
push_external: PEP,
pull_internal: PIP,
summary: Vec<Vec<Antichain<T::Summary>>>,
phantom: ::std::marker::PhantomData<T>,
}
impl<T, PEP, PIP> Operate<T> for OperatorCore<T, PEP, PIP>
where
T: Timestamp,
PEP: FnMut(&mut [ChangeBatch<T>])+'static,
PIP: FnMut(&mut [ChangeBatch<T>], &mut [ChangeBatch<T>], &mut [ChangeBatch<T>])->bool+'static
{
fn inputs(&self) -> usize { self.shape.inputs }
fn outputs(&self) -> usize { self.shape.outputs }
fn get_internal_summary(&mut self) -> (Vec<Vec<Antichain<T::Summary>>>, Vec<ChangeBatch<T>>) {
let mut internal = Vec::new();
for _ in 0 .. self.shape.outputs {
internal.push(ChangeBatch::new_from(Default::default(), self.shape.peers as i64));
}
(self.summary.clone(), internal)
}
fn set_external_summary(&mut self, _summaries: Vec<Vec<Antichain<T::Summary>>>,
count_map: &mut [ChangeBatch<T>]) {
(self.push_external)(count_map);
}
fn push_external_progress(&mut self, count_map: &mut [ChangeBatch<T>]) {
(self.push_external)(count_map);
}
fn pull_internal_progress(&mut self, consumed: &mut [ChangeBatch<T>],
internal: &mut [ChangeBatch<T>],
produced: &mut [ChangeBatch<T>]) -> bool
{
(self.pull_internal)(consumed, internal, produced)
}
fn name(&self) -> String { self.shape.name.clone() }
fn notify_me(&self) -> bool { self.shape.notify }
}