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
use std::rc::Rc;
use std::cell::RefCell;
use std::default::Default;
use ::Data;
use progress::{ChangeBatch, Timestamp};
use progress::frontier::{Antichain, MutableAntichain};
use dataflow::{Stream, Scope};
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;
use dataflow::channels::pullers::Counter as PullCounter;
use dataflow::operators::capability::Capability;
use dataflow::operators::capability::mint as mint_capability;
use dataflow::operators::generic::handles::{InputHandle, new_input_handle, OutputWrapper};
use logging::Logger;
use super::builder_raw::OperatorBuilder as OperatorBuilderRaw;
pub struct OperatorBuilder<G: Scope> {
builder: OperatorBuilderRaw<G>,
frontier: Vec<MutableAntichain<G::Timestamp>>,
consumed: Vec<Rc<RefCell<ChangeBatch<G::Timestamp>>>>,
internal: Rc<RefCell<ChangeBatch<G::Timestamp>>>,
produced: Vec<Rc<RefCell<ChangeBatch<G::Timestamp>>>>,
logging: Logger,
}
impl<G: Scope> OperatorBuilder<G> {
pub fn new(name: String, scope: G) -> Self {
let logging = scope.logging();
OperatorBuilder {
builder: OperatorBuilderRaw::new(name, scope),
frontier: Vec::new(),
consumed: Vec::new(),
internal: Rc::new(RefCell::new(ChangeBatch::new())),
produced: Vec::new(),
logging: logging,
}
}
pub fn set_notify(&mut self, notify: bool) {
self.builder.set_notify(notify);
}
pub fn new_input<D: Data, P>(&mut self, stream: &Stream<G, D>, pact: P) -> InputHandle<G::Timestamp, D, P::Puller>
where
P: ParallelizationContract<G::Timestamp, D> {
let connection = vec![Antichain::from_elem(Default::default()); self.builder.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>>) -> InputHandle<G::Timestamp, D, P::Puller>
where
P: ParallelizationContract<G::Timestamp, D> {
let puller = self.builder.new_input_connection(stream, pact, connection);
let input = PullCounter::new(puller);
self.frontier.push(MutableAntichain::new());
self.consumed.push(input.consumed().clone());
new_input_handle(input, self.internal.clone(), self.logging.clone())
}
pub fn new_output<D: Data>(&mut self) -> (OutputWrapper<G::Timestamp, D, Tee<G::Timestamp, D>>, Stream<G, D>) {
let connection = vec![Antichain::from_elem(Default::default()); self.builder.shape().inputs()];
self.new_output_connection(connection)
}
pub fn new_output_connection<D: Data>(&mut self, connection: Vec<Antichain<<G::Timestamp as Timestamp>::Summary>>) -> (OutputWrapper<G::Timestamp, D, Tee<G::Timestamp, D>>, Stream<G, D>) {
let (tee, stream) = self.builder.new_output_connection(connection);
let mut buffer = PushBuffer::new(PushCounter::new(tee));
self.produced.push(buffer.inner().produced().clone());
(OutputWrapper::new(buffer), stream)
}
pub fn build<B, L>(self, constructor: B)
where
B: FnOnce(Capability<G::Timestamp>) -> L,
L: FnMut(&[MutableAntichain<G::Timestamp>])+'static
{
let cap = mint_capability(Default::default(), self.internal.clone());
self.internal.borrow_mut().clear();
let mut logic = constructor(cap);
let self_frontier1 = Rc::new(RefCell::new(self.frontier));
let self_frontier2 = self_frontier1.clone();
let self_consumed = self.consumed;
let self_internal = self.internal;
let self_produced = self.produced;
let pep = move |changes: &mut [ChangeBatch<G::Timestamp>]| {
let mut borrow = self_frontier1.borrow_mut();
for index in 0 .. changes.len() {
borrow[index].update_iter(changes[index].drain());
}
};
let pip = move |consumed: &mut [ChangeBatch<G::Timestamp>],
internal: &mut [ChangeBatch<G::Timestamp>],
produced: &mut [ChangeBatch<G::Timestamp>]| {
let borrow = self_frontier2.borrow();
logic(&*borrow);
for index in 0 .. consumed.len() {
self_consumed[index].borrow_mut().drain_into(&mut consumed[index]);
}
for index in 0 .. internal.len() {
let mut borrow = self_internal.borrow_mut();
internal[index].extend(borrow.iter().cloned());
}
self_internal.borrow_mut().clear();
for index in 0 .. produced.len() {
self_produced[index].borrow_mut().drain_into(&mut produced[index]);
}
false
};
self.builder.build(pep, pip);
}
}