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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use differential_dataflow::lattice::Lattice;
use timely::progress::frontier::{Antichain, AntichainRef, MutableAntichain};
use timely::progress::Timestamp;
use expr::GlobalId;
use crate::coord::antichain::AntichainToken;
pub struct ArrangementFrontiers<T: Timestamp> {
index: HashMap<GlobalId, Frontiers<T>>,
}
impl<T: Timestamp> Default for ArrangementFrontiers<T> {
fn default() -> Self {
Self {
index: HashMap::new(),
}
}
}
impl<T: Timestamp> ArrangementFrontiers<T> {
pub fn get(&self, id: &GlobalId) -> Option<&Frontiers<T>> {
self.index.get(id)
}
pub fn get_mut(&mut self, id: &GlobalId) -> Option<&mut Frontiers<T>> {
self.index.get_mut(id)
}
pub fn contains_key(&self, id: GlobalId) -> bool {
self.index.contains_key(&id)
}
pub fn intersection(&self, ids: impl IntoIterator<Item = GlobalId>) -> Vec<GlobalId> {
ids.into_iter()
.filter(|id| self.contains_key(*id))
.collect()
}
pub fn insert(&mut self, id: GlobalId, state: Frontiers<T>) -> Option<Frontiers<T>> {
self.index.insert(id, state)
}
pub fn remove(&mut self, id: &GlobalId) -> Option<Frontiers<T>> {
self.index.remove(id)
}
pub fn upper_of(&self, name: &GlobalId) -> Option<AntichainRef<T>> {
if let Some(index_state) = self.get(name) {
Some(index_state.upper.frontier())
} else {
None
}
}
pub fn since_of(&self, name: &GlobalId) -> Option<Antichain<T>> {
if let Some(index_state) = self.get(name) {
Some(index_state.since.borrow().frontier().to_owned())
} else {
None
}
}
pub fn greatest_open_upper<I>(&self, identifiers: I) -> Antichain<T>
where
I: IntoIterator<Item = GlobalId>,
T: Lattice,
{
let mut min_upper = Antichain::new();
for id in identifiers {
min_upper.extend(self.upper_of(&id).unwrap().iter().cloned());
}
min_upper
}
pub fn least_valid_since<I>(&self, identifiers: I) -> Antichain<T>
where
I: IntoIterator<Item = GlobalId>,
T: Lattice,
{
let mut max_since = Antichain::from_elem(T::minimum());
for id in identifiers {
max_since.join_assign(&self.since_of(&id).expect("Since missing at coordinator"));
}
max_since
}
}
pub struct Frontiers<T: Timestamp> {
pub upper: MutableAntichain<T>,
pub since: Rc<RefCell<MutableAntichain<T>>>,
since_action: Rc<RefCell<dyn FnMut(Antichain<T>)>>,
pub compaction_window_ms: Option<T>,
}
impl<T: Timestamp + Copy> Frontiers<T> {
pub fn new<I, F>(
initial: I,
compaction_window_ms: Option<T>,
since_action: F,
) -> (Self, AntichainToken<T>)
where
I: IntoIterator<Item = T>,
F: FnMut(Antichain<T>) + 'static,
{
let mut upper = MutableAntichain::new();
upper.update_iter(Some((T::minimum(), 1)));
let frontier = Self {
upper,
since: Rc::new(RefCell::new(MutableAntichain::new())),
compaction_window_ms,
since_action: Rc::new(RefCell::new(since_action)),
};
let handle = frontier.since_handle(initial);
(frontier, handle)
}
pub fn since_handle<I>(&self, values: I) -> AntichainToken<T>
where
I: IntoIterator<Item = T>,
{
let since = Rc::clone(&self.since);
let since_action = Rc::clone(&self.since_action);
AntichainToken::new(values, move |changes| {
let changed = since.borrow_mut().update_iter(changes).next().is_some();
if changed {
(since_action.borrow_mut())(since.borrow().frontier().to_owned());
}
})
}
pub fn set_compaction_window_ms(&mut self, window_ms: Option<T>) {
self.compaction_window_ms = window_ms;
}
}
pub struct SinkWrites<T: Timestamp> {
pub frontier: MutableAntichain<T>,
pub source_handles: Vec<AntichainToken<T>>,
}
impl<T: Timestamp> SinkWrites<T> {
pub fn new(source_handles: Vec<AntichainToken<T>>) -> Self {
let mut frontier = MutableAntichain::new();
frontier.update_iter(Some((T::minimum(), 1)));
SinkWrites {
frontier,
source_handles,
}
}
pub fn advance_source_handles(&mut self) {
let frontier: Vec<_> = self.frontier.frontier().to_owned().elements().to_vec();
for handle in self.source_handles.iter_mut() {
handle.maybe_advance(frontier.iter().cloned());
}
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use timely::progress::frontier::Antichain;
use super::Frontiers;
#[test]
fn test_frontiers_action() {
let expect = Rc::new(RefCell::new(Antichain::from_elem(0)));
let inner = Rc::clone(&expect);
let (f, initial) = Frontiers::new(Some(0), None, move |since| {
assert_eq!(*inner.borrow(), since);
});
let h1 = f.since_handle(vec![5u64]);
assert_eq!(f.since.borrow().frontier().to_owned(), *expect.borrow());
expect.replace(Antichain::from_elem(5));
drop(initial);
assert_eq!(f.since.borrow().frontier().to_owned(), *expect.borrow());
expect.replace(Antichain::new());
drop(h1);
assert_eq!(f.since.borrow().frontier().to_owned(), *expect.borrow());
}
}