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
use std::default::Default;
use timely::order::TotalOrder;
use timely::dataflow::*;
use timely::dataflow::operators::Unary;
use timely::dataflow::channels::pact::Pipeline;
use timely_sort::Unsigned;
use lattice::Lattice;
use ::{Data, Collection, Diff};
use hashable::{Hashable, UnsignedWrapper};
use collection::AsCollection;
use operators::arrange::{Arrange, Arranged, ArrangeBySelf};
use trace::{BatchReader, Cursor, Trace, TraceReader};
use trace::implementations::ord::OrdKeySpine as DefaultKeyTrace;
pub trait DistinctTotal<G: Scope, K: Data, R: Diff> where G::Timestamp: TotalOrder+Lattice+Ord {
fn distinct_total(&self) -> Collection<G, K, isize>;
fn distinct_total_u(&self) -> Collection<G, K, isize> where K: Unsigned+Copy;
}
impl<G: Scope, K: Data+Default+Hashable, R: Diff> DistinctTotal<G, K, R> for Collection<G, K, R>
where G::Timestamp: TotalOrder+Lattice+Ord {
fn distinct_total(&self) -> Collection<G, K, isize> {
self.arrange_by_self()
.distinct_total_core()
.map(|k| k.item)
}
fn distinct_total_u(&self) -> Collection<G, K, isize> where K: Unsigned+Copy {
self.map(|k| (UnsignedWrapper::from(k), ()))
.arrange(DefaultKeyTrace::new())
.distinct_total_core()
.map(|k| k.item)
}
}
pub trait DistinctTotalCore<G: Scope, K: Data, R: Diff> where G::Timestamp: TotalOrder+Lattice+Ord {
fn distinct_total_core(&self) -> Collection<G, K, isize>;
}
impl<G: Scope, K: Data, R: Diff, T1> DistinctTotalCore<G, K, R> for Arranged<G, K, (), R, T1>
where
G::Timestamp: TotalOrder+Lattice+Ord,
T1: TraceReader<K, (), G::Timestamp, R>+Clone+'static,
T1::Batch: BatchReader<K, (), G::Timestamp, R> {
fn distinct_total_core(&self) -> Collection<G, K, isize> {
let mut trace = self.trace.clone();
self.stream.unary_stream(Pipeline, "DistinctTotal", move |input, output| {
input.for_each(|capability, batches| {
let mut session = output.session(&capability);
for batch in batches.drain(..).map(|x| x.item) {
let (mut batch_cursor, batch_storage) = batch.cursor();
let (mut trace_cursor, trace_storage) = trace.cursor_through(batch.lower()).unwrap();
while batch_cursor.key_valid(&batch_storage) {
let key = batch_cursor.key(&batch_storage);
let mut count = R::zero();
trace_cursor.seek_key(&trace_storage, key);
if trace_cursor.key_valid(&trace_storage) && trace_cursor.key(&trace_storage) == key {
trace_cursor.map_times(&trace_storage, |_, diff| count = count + diff);
}
batch_cursor.map_times(&batch_storage, |time, diff| {
let old_distinct = !count.is_zero();
count = count + diff;
let new_distinct = !count.is_zero();
if old_distinct != new_distinct {
session.give((key.clone(), time.clone(), if old_distinct { -1 } else { 1 }));
}
});
batch_cursor.step_key(&batch_storage);
}
trace.advance_by(batch.upper());
trace.distinguish_since(batch.upper());
}
});
})
.as_collection()
}
}