/
package.scala
330 lines (301 loc) · 10.8 KB
/
package.scala
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.github.garyaiki.dendrites.algebird
import akka.NotUsed
import akka.stream.scaladsl.Flow
import com.twitter.algebird.{Approximate, AveragedValue, HLL, QTree}
import scala.reflect.runtime.universe.TypeTag
import com.github.garyaiki.dendrites.algebird.typeclasses.QTreeLike
/** Akka Stream Flows for Algebird hashing and aggregating functions
*
* CreateCMSFlow class initializes a CMS monoid then creates Count Mean Sketch for value
*
* CreateHLLFLow class initializes a HyperLogLogAggregator then HyperLogLog for value
*
* ZipTimeFlow class creates a tuple of received value and current time in milliseconds
* DecayedValue uses these value/time pairs
*
* == AveragedValue ==
* Average sequence of Numerics Flow `com.github.garyaiki.dendrites.algebird.stream.AveragedFlowSpec`
* {{{
* val avg: FlowShape[Seq[A], AveragedValue] = builder.add(avgFlow)
* }}}
* Sum AveragedValues sequences to AveragedValue `com.github.garyaiki.dendrites.algebird.stream.SumAveragedFlowSpec`
* {{{
* val (pub, sub) = TestSource.probe[Seq[BigDecimal]]
* .via(avgBDFlow.grouped(2))
* .via(sumAvgFlow)
* .toMat(TestSink.probe[AveragedValue])(Keep.both)
* .run()
* }}}
* == Hyper Log Log == `com.github.garyaiki.dendrites.algebird.stream.HyperLogLogFlowSpec`
* Estimate size of Hyper Log Log
* {{{
* val (pub, sub) = TestSource.probe[HLL]
* .via(estSizeFlow)
* .toMat(TestSink.probe[Double])(Keep.both)
* .run()
* }}}
* Map Hyper Log Log to Approximate[Long]
* {{{
* val (pub, sub) = TestSource.probe[HLL]
* .via(toApproximate)
* .toMat(TestSink.probe[Approximate[Long]])(Keep.both)
* .run()
* }}}
* Map Hyper Log Log sequence to Approximate[Long] sequence
* {{{
* val (pub, sub) = TestSource.probe[Seq[HLL]]
* .via(toApproximates)
* .toMat(TestSink.probe[Seq[Approximate[Long]]])(Keep.both)
* .run()
* }}}
* Sum Hyper Log Log sequence to Approximate[Long]
* {{{
* val (pub, sub) = TestSource.probe[Seq[HLL]]
* .via(sumHLLs)
* .toMat(TestSink.probe[Approximate[Long]])(Keep.both)
* .run()
* }}}
* == Max value in a sequence == `com.github.garyaiki.dendrites.algebird.stream.MaxFlowSpec`
* {{{
* val (pub, sub) = TestSource.probe[Seq[BigDecimal]]
* .via(maxFlow)
* .toMat(TestSink.probe[BigDecimal])(Keep.both)
* .run()
}}}
* == Min value in a sequence == `com.github.garyaiki.dendrites.algebird.stream.MinFlowSpec`
* {{{
* val (pub, sub) = TestSource.probe[Seq[BigInt]]
* .via(minFlow)
* .toMat(TestSink.probe[BigInt])(Keep.both)
* .run()
* }}}
* == QTree == `com.github.garyaiki.dendrites.algebird.stream.QTreeFlowSpec`
* Find max value in a QTree
* {{{
* def sourceBI = TestSource.probe[QTree[BigInt]]
* def sinkD = TestSink.probe[Double]
* val (pub, sub) = sourceBI
* .via(qTreeMaxFlow)
* .toMat(sinkD)(Keep.both)
* .run()
* }}}
* Find min value in a QTree
* {{{
* def sourceBD = TestSource.probe[QTree[BigDecimal]]
* def sinkD = TestSink.probe[Double]
* val (pub, sub) = sourceBD
* .via(qTreeMinFlow)
* .toMat(sinkD)(Keep.both)
* .run()
* }}}
* Find first quartile bounds in a QTree
* {{{
* def sourceD = TestSource.probe[QTree[Double]]
* def sinkDD = TestSink.probe[(Double, Double)]
* val (pub, sub) = sourceD
* .via(firstQuartileFlow)
* .toMat(sinkDD)(Keep.both)
* .run()
* }}}
* Find second quartile bounds in a QTree
* {{{
* def sourceF = TestSource.probe[QTree[Float]]
* def sinkDD = TestSink.probe[(Double, Double)]
* val (pub, sub) = sourceF
* .via(secondQuartileFlow)
* .toMat(sinkDD)(Keep.both)
* .run()
* }}}
* Find third quartile bounds in a QTree
* {{{
* def sourceI = TestSource.probe[QTree[Int]]
* def sinkDD = TestSink.probe[(Double, Double)]
* val (pub, sub) = sourceI
* .via(thirdQuartileFlow)
* .toMat(sinkDD)(Keep.both)
* .run()
* }}}
* Find inter quartile mean in a QTree[Long]
* {{{
* def sourceL = TestSource.probe[QTree[Long]]
* def sinkDD = TestSink.probe[(Double, Double)]
* val (pub, sub) = sourceL
* .via(interQuartileMeanLFlow)
* .toMat(sinkDD)(Keep.both)
* .run()
* }}}
* @see [[com.github.garyaiki.dendrites.algebird]] package object for the functions these Flows wrap
* @author Gary Struthers
*/
package object stream {
/** Flow to average sequence of Numerics. When Numerics are ambiguous use typed avg Flows below
*
* @tparam A can be generic by including TypeTage at runtime & if it's sole Numeric in scope
* @return AveragedValue
*
* @see [[http://twitter.github.io/algebird/datatypes/averaged_value.html AveragedValue]]
* @see [[http://doc.akka.io/api/akka/current/akka/stream/scaladsl/Flow.html Flow]]
*/
def avgFlow[A: TypeTag: Numeric]: Flow[Seq[A], AveragedValue, NotUsed] = Flow[Seq[A]].map(avg[A])
/** Flow to find average of sequence of BigDecimals
*
* @return AveragedValue
*/
def avgBDFlow: Flow[Seq[BigDecimal], AveragedValue, NotUsed] = Flow[Seq[BigDecimal]].map(avg[BigDecimal])
/** Flow to find average of sequence of BigInts
*
* @return AveragedValue
*/
def avgBIFlow: Flow[Seq[BigInt], AveragedValue, NotUsed] = Flow[Seq[BigInt]].map(avg[BigInt])
/** Flow to find average of sequence of Doubles
*
* @return AveragedValue
*/
def avgDFlow: Flow[Seq[Double], AveragedValue, NotUsed] = Flow[Seq[Double]].map(avg[Double])
/** Flow to find average of sequence of Floats
*
* @return AveragedValue
*/
def avgFFlow: Flow[Seq[Float], AveragedValue, NotUsed] = Flow[Seq[Float]].map(avg[Float])
/** Flow to find average of sequence of Ints
*
* @return AveragedValue
*/
def avgIFlow: Flow[Seq[Int], AveragedValue, NotUsed] = Flow[Seq[Int]].map(avg[Int])
/** Flow to find average of sequence of Longs
*
* @return AveragedValue
*/
def avgLFlow: Flow[Seq[Long], AveragedValue, NotUsed] = Flow[Seq[Long]].map(avg[Long])
/** Flow to sum sequences of AveragedValues to a single AveragedValue
*
* @return AveragedValue
*/
def sumAvgFlow: Flow[Seq[AveragedValue], AveragedValue, NotUsed] = Flow[Seq[AveragedValue]].map(sumAverageValues)
/** Flow to estimate size of HLL
*
* @return estimated size of HLL
* @see [[http://twitter.github.io/algebird/datatypes/approx/hyperloglog.html HyperLogLog]]
*/
def estSizeFlow: Flow[HLL, Double, NotUsed] = Flow[HLL].map(x => x.estimatedSize)
/** Flow to map Hyper Log Log to Approximate[Long]
*
* @return Approximate[Long]
* @see [[http://twitter.github.io/algebird/datatypes/approx/approximate.html Approximate]]
*/
def toApproximate: Flow[HLL, Approximate[Long], NotUsed] = Flow[HLL].map(x => x.approximateSize)
/** Flow to map sequence of HLL to sequence of Approximate[Long]
*
* @return Approximate[Long]
*/
def toApproximates: Flow[Seq[HLL], Seq[Approximate[Long]], NotUsed] = Flow[Seq[HLL]].map(mapHLL2Approximate)
/** Flow to sum sequence of HLL to an Approximate[Long]
*
* @return Approximate[Long]
*/
def sumHLLs: Flow[Seq[HLL], Approximate[Long], NotUsed] = Flow[Seq[HLL]].map(sumHLLApproximateSizes)
/** Flow to find max value in a sequence
*
* @tparam A: Ordering
* @return max
* @see [[http://www.scala-lang.org/api/current/scala/math/Ordering.html Ordering]]
*/
def maxFlow[A: Ordering]: Flow[Seq[A], A, NotUsed] = Flow[Seq[A]].map(max[A])
/** Flow to find min value in a sequence
*
* @tparam A: Ordering
* @return min
*/
def minFlow[A: Ordering]: Flow[Seq[A], A, NotUsed] = Flow[Seq[A]].map(min[A])
/** Flow to find max value in a QTree
*
* @tparam A QTreeLike typeclass that construct QTree[A]
* @return max
* @see [[http://twitter.github.io/algebird/datatypes/approx/q_tree.html QTree]]
*/
def qTreeMaxFlow[A: QTreeLike]: Flow[QTree[A], Double, NotUsed] = Flow[QTree[A]].map(_.upperBound)
/** Flow to find min value in a QTree
*
* @tparam A QTreeLike typeclass that construct QTree[A]
* @return min
*
*/
def qTreeMinFlow[A: QTreeLike]: Flow[QTree[A], Double, NotUsed] = Flow[QTree[A]].map(_.lowerBound)
/** Flow to find first quartile bounds in a QTree
*
* @tparam A QTreeLike typeclass that construct QTree[A]
* @return lower, upper bounds
*/
def firstQuartileFlow[A: QTreeLike]: Flow[QTree[A], (Double, Double), NotUsed] = {
Flow[QTree[A]].map(_.quantileBounds(0.25))
}
/** Flow to find second quartile bounds in a QTree
*
* @tparam A QTreeLike typeclass that construct QTree[A]
* @return lower, upper bounds
*/
def secondQuartileFlow[A: QTreeLike]: Flow[QTree[A], (Double, Double), NotUsed] = {
Flow[QTree[A]].map(_.quantileBounds(0.5))
}
/** Flow to find third quartile bounds in a QTree
*
* @tparam A QTreeLike typeclass that construct QTree[A]
* @return lower, upper bounds
*/
def thirdQuartileFlow[A: QTreeLike]: Flow[QTree[A], (Double, Double), NotUsed] = {
Flow[QTree[A]].map(_.quantileBounds(0.75))
}
/** Flow to find inter quartile mean in a QTree[BigDecimal]
*
* @return mean of middle 50th percentile
*/
def interQuartileMeanBDFlow: Flow[QTree[BigDecimal], (Double, Double), NotUsed] = {
Flow[QTree[BigDecimal]].map(_.interQuartileMean)
}
/** Flow to find inter quartile mean in a QTree[BigInt]
*
* @return mean of middle 50th percentile
*/
def interQuartileMeanBIFlow: Flow[QTree[BigInt], (Double, Double), NotUsed] = {
Flow[QTree[BigInt]].map(_.interQuartileMean)
}
/** Flow to find inter quartile mean in a QTree[Double]
*
* @return mean of middle 50th percentile
*/
def interQuartileMeanDFlow: Flow[QTree[Double], (Double, Double), NotUsed] = {
Flow[QTree[Double]].map(_.interQuartileMean)
}
/** Flow to find inter quartile mean in a QTree[Float]
*
* @return mean of middle 50th percentile
*/
def interQuartileMeanFFlow: Flow[QTree[Float], (Double, Double), NotUsed] = {
Flow[QTree[Float]].map(_.interQuartileMean)
}
/** Flow to find inter quartile mean in a QTree[Int]
*
* @return mean of middle 50th percentile
*/
def interQuartileMeanIFlow: Flow[QTree[Int], (Double, Double), NotUsed] = {
Flow[QTree[Int]].map(_.interQuartileMean)
}
/** Flow to find inter quartile mean in a QTree[Long]
*
* @return mean of middle 50th percentile
*/
def interQuartileMeanLFlow: Flow[QTree[Long], (Double, Double), NotUsed] = {
Flow[QTree[Long]].map(_.interQuartileMean)
}
}