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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use RefCell;
use HashMap;
use Rc;
use Hydroflow;
use GraphExt;
use ;
use multiplatform_test;
const BATCH_A: & = &;
const BATCH_B: & = &;
const BATCH_C: & = &;
/// Basic monotonic threshold: find all departments with total salary at least 20_000.
/// Uses the core API.
/// SQL: SELECT department FROM employees WHERE 20_000 <= SUM(salary) GROUP BY department
// TODO(mingwei): rewrite in surface syntax (surface *API* is gone).
// /// Basic monotonic threshold: release a value once after it has been seen three times.
// /// Uses the surface (builder) API.
// /// SQL: SELECT department FROM employees WHERE 20_000 <= SUM(salary) GROUP BY department
// #[test]
// #[multiplatform_test]
// fn fold_keyed_monotonic_surface() {
// use hydroflow::builder::prelude::*;
// let mut hf_builder = HydroflowBuilder::new();
// let (input, source_recv) = hf_builder.add_channel_input::<_, _, VecHandoff<Employee>>("source");
// let output = <Rc<RefCell<Vec<&'static str>>>>::default();
// let output_ref = output.clone();
// hf_builder.add_subgraph(
// "main",
// source_recv
// .flatten()
// .map_scan(HashMap::<&'static str, u32>::new(), |groups, employee| {
// let count = groups.entry(employee.department).or_default();
// let old_count = *count;
// *count += employee.salary;
// (employee.department, old_count, *count)
// })
// .filter_map(|(department, old_count, count)| {
// if old_count < 20_000 && 20_000 <= count {
// Some(department)
// } else {
// None
// }
// })
// .pull_to_push()
// .for_each(move |item| output_ref.borrow_mut().push(item)),
// );
// let mut hf = hf_builder.build();
// input.give(Iter(BATCH_A.iter().cloned()));
// input.flush();
// hf.run_available();
// assert_eq!(0, output.borrow().len());
// input.give(Iter(BATCH_B.iter().cloned()));
// input.flush();
// hf.run_available();
// assert_eq!(&["sales", "accounting"], &**output.borrow());
// input.give(Iter(BATCH_C.iter().cloned()));
// input.flush();
// hf.run_available();
// assert_eq!(&["sales", "accounting", "engineering"], &**output.borrow());
// }
// /// Non-monotonic barrier. Per tick, for each department find the highest paid employee.
// /// Takes in BATCH_A in the first tick, then BATCH_B *and* BATCH_C in the second tick.
// /// SQL (per batch): SELECT department, name, salary FROM employees WHERE salary = MAX(salary) GROUP BY department
// #[test]
// #[multiplatform_test]
// fn fold_keyed_nonmon_surface() {
// use hydroflow::builder::prelude::*;
// let mut hf_builder = HydroflowBuilder::new();
// let (input, source_recv) = hf_builder.add_channel_input::<_, _, VecHandoff<Employee>>("source");
// let (a_send, a_recv) = hf_builder.make_edge::<_, VecHandoff<Employee>, _>("names A-M");
// let (z_send, z_recv) = hf_builder.make_edge::<_, VecHandoff<Employee>, _>("names N-Z");
// let (stratum_boundary_send, stratum_boundary_recv) =
// hf_builder.make_edge::<_, VecHandoff<Employee>, _>("names N-Z");
// // Make output first, to mess with scheduler order.
// let output = <Rc<RefCell<BinaryHeap<_>>>>::default();
// let output_ref = output.clone();
// hf_builder.add_subgraph_stratified(
// "find median",
// 1,
// stratum_boundary_recv
// .flat_map(|batch| {
// batch
// .into_iter()
// .map(|employee| (employee.department, (employee.name, employee.salary)))
// .into_grouping_map()
// .max_by_key(|_department, (_name, salary)| *salary)
// })
// .pull_to_push()
// .map(|(department, (name, salary))| (department, name, salary))
// .for_each(move |val| output_ref.borrow_mut().push(val)),
// );
// // Partition then re-merge names to make graph more interesting.
// // Want to have multiple compiled components to test scheduler.
// hf_builder.add_subgraph_stratified(
// "split",
// 0,
// source_recv.flatten().pull_to_push().partition(
// |employee| employee.name < "N",
// hf_builder.start_tee().map(Some).push_to(a_send),
// hf_builder.start_tee().map(Some).push_to(z_send),
// ),
// );
// hf_builder.add_subgraph_stratified(
// "union",
// 0,
// a_recv
// .flatten()
// .chain(z_recv.flatten())
// .pull_to_push()
// .map(Some)
// .push_to(stratum_boundary_send),
// );
// let mut hf = hf_builder.build();
// // Give BATCH_A and cross barrier to run next stratum.
// input.give(Iter(BATCH_A.iter().cloned()));
// input.flush();
// hf.run_stratum();
// assert_eq!((0, 0), (hf.current_tick(), hf.current_stratum()));
// assert_eq!(0, output.borrow().len());
// hf.run_available();
// assert_eq!((1, 1), (hf.current_tick(), hf.current_stratum()));
// assert_eq!(
// &[
// ("accounting", "Pierce", 7100),
// ("hr", "Davis", 7000),
// ("sales", "Alexa", 9000),
// ],
// &*std::mem::take(&mut *output.borrow_mut()).into_sorted_vec(),
// );
// // Give BATCH_B but only run this stratum.
// input.give(Iter(BATCH_B.iter().cloned()));
// input.flush();
// hf.run_stratum();
// assert_eq!((1, 1), (hf.current_tick(), hf.current_stratum()));
// // Give BATCH_C and run all to completion.
// input.give(Iter(BATCH_C.iter().cloned()));
// input.flush();
// hf.run_available();
// assert_eq!((3, 1), (hf.current_tick(), hf.current_stratum()));
// // Second batch has 7+3 = 10 items.
// assert_eq!(
// &[
// ("accounting", "Chaitali", 6900),
// ("engineering", "Raul", 12000),
// ("hr", "Miles", 6100),
// ("sales", "Buffy", 8800),
// ],
// &*std::mem::take(&mut *output.borrow_mut()).into_sorted_vec(),
// );
// assert!(!hf.next_stratum());
// }