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
use std::rc::Rc;
use std::cell::RefCell;
use crate::scheduling::{Schedule, ActivateOnDrop};
use crate::progress::frontier::Antichain;
use crate::progress::{Operate, operate::SharedProgress, Timestamp};
use crate::progress::Source;
use crate::progress::ChangeBatch;
use crate::Data;
use crate::dataflow::channels::pushers::{Tee, Counter as PushCounter};
use crate::dataflow::channels::pushers::buffer::{Buffer as PushBuffer, AutoflushSession};
use crate::dataflow::operators::ActivateCapability;
use crate::dataflow::operators::capability::mint as mint_capability;
use crate::dataflow::{Stream, Scope};
pub trait UnorderedInput<G: Scope> {
fn new_unordered_input<D:Data>(&mut self) -> ((UnorderedHandle<G::Timestamp, D>, ActivateCapability<G::Timestamp>), Stream<G, D>);
}
impl<G: Scope> UnorderedInput<G> for G {
fn new_unordered_input<D:Data>(&mut self) -> ((UnorderedHandle<G::Timestamp, D>, ActivateCapability<G::Timestamp>), Stream<G, D>) {
let (output, registrar) = Tee::<G::Timestamp, D>::new();
let internal = Rc::new(RefCell::new(ChangeBatch::new()));
let cap = mint_capability(G::Timestamp::minimum(), internal.clone());
let counter = PushCounter::new(output);
let produced = counter.produced().clone();
let peers = self.peers();
let index = self.allocate_operator_index();
let mut address = self.addr();
address.push(index);
let cap = ActivateCapability::new(cap, &address[..], self.activations().clone());
let helper = UnorderedHandle::new(counter);
self.add_operator_with_index(Box::new(UnorderedOperator {
name: "UnorderedInput".to_owned(),
address,
shared_progress: Rc::new(RefCell::new(SharedProgress::new(0, 1))),
internal,
produced,
peers,
}), index);
((helper, cap), Stream::new(Source::new(index, 0), registrar, self.clone()))
}
}
struct UnorderedOperator<T:Timestamp> {
name: String,
address: Vec<usize>,
shared_progress: Rc<RefCell<SharedProgress<T>>>,
internal: Rc<RefCell<ChangeBatch<T>>>,
produced: Rc<RefCell<ChangeBatch<T>>>,
peers: usize,
}
impl<T:Timestamp> Schedule for UnorderedOperator<T> {
fn name(&self) -> &str { &self.name }
fn path(&self) -> &[usize] { &self.address[..] }
fn schedule(&mut self) -> bool {
let shared_progress = &mut *self.shared_progress.borrow_mut();
self.internal.borrow_mut().drain_into(&mut shared_progress.internals[0]);
self.produced.borrow_mut().drain_into(&mut shared_progress.produceds[0]);
false
}
}
impl<T:Timestamp> Operate<T> for UnorderedOperator<T> {
fn inputs(&self) -> usize { 0 }
fn outputs(&self) -> usize { 1 }
fn get_internal_summary(&mut self) -> (Vec<Vec<Antichain<<T as Timestamp>::Summary>>>, Rc<RefCell<SharedProgress<T>>>) {
let mut borrow = self.internal.borrow_mut();
for (time, count) in borrow.drain() {
self.shared_progress.borrow_mut().internals[0].update(time, count * (self.peers as i64));
}
(Vec::new(), self.shared_progress.clone())
}
fn notify_me(&self) -> bool { false }
}
pub struct UnorderedHandle<T: Timestamp, D: Data> {
buffer: PushBuffer<T, D, PushCounter<T, D, Tee<T, D>>>,
}
impl<T: Timestamp, D: Data> UnorderedHandle<T, D> {
fn new(pusher: PushCounter<T, D, Tee<T, D>>) -> UnorderedHandle<T, D> {
UnorderedHandle {
buffer: PushBuffer::new(pusher),
}
}
pub fn session<'b>(&'b mut self, cap: ActivateCapability<T>) -> ActivateOnDrop<AutoflushSession<'b, T, D, PushCounter<T, D, Tee<T, D>>>> {
ActivateOnDrop::new(self.buffer.autoflush_session(cap.capability.clone()), cap.address.clone(), cap.activations.clone())
}
}