differential_dataflow/operators/
threshold.rs1use timely::order::TotalOrder;
7use timely::progress::Timestamp;
8use timely::dataflow::operators::Operator;
9use timely::dataflow::channels::pact::Pipeline;
10
11use crate::lattice::Lattice;
12use crate::{ExchangeData, VecCollection};
13use crate::difference::{Semigroup, Abelian};
14use crate::hashable::Hashable;
15use crate::collection::AsCollection;
16use crate::operators::arrange::Arranged;
17use crate::trace::{BatchCursor, BatchDiff, BatchDiffGat, BatchKey, BatchReader, Cursor, Navigable, TraceReader};
18
19pub trait ThresholdTotal<'scope, T: Timestamp + TotalOrder + Lattice, K: ExchangeData, R: ExchangeData+Semigroup> : Sized {
21 fn threshold_semigroup<R2, F>(self, thresh: F) -> VecCollection<'scope, T, K, R2>
23 where
24 R2: Semigroup+'static,
25 F: FnMut(&K,&R,Option<&R>)->Option<R2>+'static,
26 ;
27 fn threshold_total<R2: Abelian+'static, F: FnMut(&K,&R)->R2+'static>(self, mut thresh: F) -> VecCollection<'scope, T, K, R2> {
43 self.threshold_semigroup(move |key, new, old| {
44 let mut new = thresh(key, new);
45 if let Some(old) = old {
46 let mut add = thresh(key, old);
47 add.negate();
48 new.plus_equals(&add);
49 }
50 if !new.is_zero() { Some(new) } else { None }
51 })
52 }
53 fn distinct_total(self) -> VecCollection<'scope, T, K, isize> {
73 self.distinct_total_core()
74 }
75
76 fn distinct_total_core<R2: Abelian+From<i8>+'static>(self) -> VecCollection<'scope, T, K, R2> {
82 self.threshold_total(|_,_| R2::from(1i8))
83 }
84
85}
86
87impl<'scope, T, K: ExchangeData+Hashable, R: ExchangeData+Semigroup> ThresholdTotal<'scope, T, K, R> for VecCollection<'scope, T, K, R>
88where
89 T: Timestamp + TotalOrder + Lattice,
90{
91 fn threshold_semigroup<R2, F>(self, thresh: F) -> VecCollection<'scope, T, K, R2>
92 where
93 R2: Semigroup+'static,
94 F: FnMut(&K,&R,Option<&R>)->Option<R2>+'static,
95 {
96 self.arrange_by_self_named("Arrange: ThresholdTotal")
97 .threshold_semigroup(thresh)
98 }
99}
100
101impl<'scope, K, Tr> ThresholdTotal<'scope, Tr::Time, K, BatchDiff<Tr>> for Arranged<'scope, Tr>
102where
103 Tr: TraceReader<Batch: Navigable, Time: TotalOrder> + Clone + 'static,
104 for<'a> BatchCursor<Tr>: Cursor<
105 Key<'a>=&'a K,
106 Val<'a>=&'a (),
107 Time = Tr::Time,
108 Diff : ExchangeData + Semigroup<BatchDiffGat<'a, Tr>>,
109 >,
110 K: ExchangeData,
111{
112 fn threshold_semigroup<R2, F>(self, mut thresh: F) -> VecCollection<'scope, Tr::Time, K, R2>
113 where
114 R2: Semigroup+'static,
115 F: for<'a> FnMut(BatchKey<'a, Tr>,&BatchDiff<Tr>,Option<&BatchDiff<Tr>>)->Option<R2>+'static,
116 {
117
118 let mut trace = self.trace.clone();
119
120 self.stream.unary_frontier(Pipeline, "ThresholdTotal", move |_,_| {
121
122 let mut lower_limit = timely::progress::frontier::Antichain::from_elem(Tr::Time::minimum());
124 let mut upper_limit = timely::progress::frontier::Antichain::from_elem(Tr::Time::minimum());
125
126 move |(input, _frontier), output| {
127
128 let mut batch_storage = Vec::new();
129
130 lower_limit.clear();
132 lower_limit.extend(upper_limit.borrow().iter().cloned());
133
134 let mut cap = None;
135 input.for_each(|capability, batches| {
136 if cap.is_none() { cap = Some(capability.retain(0));
138 }
139 for batch in batches.drain(..) {
140 upper_limit.clone_from(batch.upper()); batch_storage.push(batch);
142 }
143 });
144
145 if let Some(capability) = cap {
146
147 let mut session = output.session(&capability);
148
149 let (mut batch_cursor, batch_storage) = crate::trace::cursor::cursor_list(batch_storage);
150 let (mut trace_cursor, trace_storage) = trace.cursor_through(lower_limit.borrow()).unwrap();
151
152 while let Some(key) = batch_cursor.get_key(&batch_storage) {
153 let mut count: Option<BatchDiff<Tr>> = None;
154
155 trace_cursor.seek_key(&trace_storage, key);
157 if trace_cursor.get_key(&trace_storage) == Some(key) {
158 trace_cursor.map_times(&trace_storage, |_, diff| {
159 count.as_mut().map(|c| c.plus_equals(&diff));
160 if count.is_none() { count = Some(<BatchCursor<Tr> as Cursor>::owned_diff(diff)); }
161 });
162 }
163
164 batch_cursor.map_times(&batch_storage, |time, diff| {
167
168 let difference =
169 match &count {
170 Some(old) => {
171 let mut temp = old.clone();
172 temp.plus_equals(&diff);
173 thresh(key, &temp, Some(old))
174 },
175 None => { thresh(key, &<BatchCursor<Tr> as Cursor>::owned_diff(diff), None) },
176 };
177
178 if let Some(count) = &mut count {
180 count.plus_equals(&diff);
181 }
182 else {
183 count = Some(<BatchCursor<Tr> as Cursor>::owned_diff(diff));
184 }
185
186 if let Some(difference) = difference {
187 if !difference.is_zero() {
188 session.give((key.clone(), <BatchCursor<Tr> as Cursor>::owned_time(time), difference));
189 }
190 }
191 });
192
193 batch_cursor.step_key(&batch_storage);
194 }
195 }
196
197 trace.advance_upper(&mut upper_limit);
199 trace.set_logical_compaction(upper_limit.borrow());
200 trace.set_physical_compaction(upper_limit.borrow());
201 }
202 })
203 .as_collection()
204 }
205}