-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathHLL.java
More file actions
1091 lines (1004 loc) · 46.3 KB
/
HLL.java
File metadata and controls
1091 lines (1004 loc) · 46.3 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.agkn.hll;
/*
* Copyright 2013 Aggregate Knowledge, Inc.
*
* 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.
*/
import java.util.Arrays;
import com.carrotsearch.hppc.IntByteHashMap;
import com.carrotsearch.hppc.LongHashSet;
import com.carrotsearch.hppc.cursors.IntByteCursor;
import com.carrotsearch.hppc.cursors.LongCursor;
import net.agkn.hll.serialization.HLLMetadata;
import net.agkn.hll.serialization.IHLLMetadata;
import net.agkn.hll.serialization.ISchemaVersion;
import net.agkn.hll.serialization.IWordDeserializer;
import net.agkn.hll.serialization.IWordSerializer;
import net.agkn.hll.serialization.SerializationUtil;
import net.agkn.hll.util.BitVector;
import net.agkn.hll.util.BitUtil;
import net.agkn.hll.util.HLLUtil;
import net.agkn.hll.util.LongIterator;
import net.agkn.hll.util.NumberUtil;
/**
* A probabilistic set of hashed <code>long</code> elements. Useful for computing
* the approximate cardinality of a stream of data in very small storage.<p/>
*
* A modified version of the <a href="http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf">
* 'HyperLogLog' data structure and algorithm</a> is used, which combines both
* probabilistic and non-probabilistic techniques to improve the accuracy and
* storage requirements of the original algorithm.<p/>
*
* More specifically, initializing and storing a new {@link HLL} will
* allocate a sentinel value symbolizing the empty set ({@link HLLType#EMPTY}).
* After adding the first few values, a sorted list of unique integers is
* stored in a {@link HLLType#EXPLICIT} hash set. When configured, accuracy can
* be sacrificed for memory footprint: the values in the sorted list are
* "promoted" to a "{@link HLLType#SPARSE}" map-based HyperLogLog structure.
* Finally, when enough registers are set, the map-based HLL will be converted
* to a bit-packed "{@link HLLType#FULL}" HyperLogLog structure.<p/>
*
* This data structure is interoperable with the implementations found at:
* <ul>
* <li><a href="https://github.com/aggregateknowledge/postgresql-hll">postgresql-hll</a>, and</li>
* <li><a href="https://github.com/aggregateknowledge/js-hll">js-hll</a></li>
* </ul>
* when <a href="https://github.com/aggregateknowledge/postgresql-hll/blob/master/STORAGE.markdown">properly serialized</a>.
*
* @author timon
*/
public class HLL implements Cloneable {
// minimum and maximum values for the log-base-2 of the number of registers
// in the HLL
public static final int MINIMUM_LOG2M_PARAM = 4;
public static final int MAXIMUM_LOG2M_PARAM = 30;
// minimum and maximum values for the register width of the HLL
public static final int MINIMUM_REGWIDTH_PARAM = 1;
public static final int MAXIMUM_REGWIDTH_PARAM = 8;
// minimum and maximum values for the 'expthresh' parameter of the
// constructor that is meant to match the PostgreSQL implementation's
// constructor and parameter names
public static final int MINIMUM_EXPTHRESH_PARAM = -1;
public static final int MAXIMUM_EXPTHRESH_PARAM = 18;
public static final int MAXIMUM_EXPLICIT_THRESHOLD = (1 << (MAXIMUM_EXPTHRESH_PARAM - 1)/*per storage spec*/);
// ************************************************************************
// Storage
// storage used when #type is EXPLICIT, null otherwise
LongHashSet explicitStorage;
// storage used when #type is SPARSE, null otherwise
IntByteHashMap sparseProbabilisticStorage;
// storage used when #type is FULL, null otherwise
BitVector probabilisticStorage;
// current type of this HLL instance, if this changes then so should the
// storage used (see above)
private HLLType type;
// ------------------------------------------------------------------------
// Characteristic parameters
// NOTE: These members are named to match the PostgreSQL implementation's
// parameters.
// log2(the number of probabilistic HLL registers)
private final int log2m;
// the size (width) each register in bits
private final int regwidth;
// ------------------------------------------------------------------------
// Computed constants
// ........................................................................
// EXPLICIT-specific constants
// flag indicating if the EXPLICIT representation should NOT be used
private final boolean explicitOff;
// flag indicating that the promotion threshold from EXPLICIT should be
// computed automatically
// NOTE: this only has meaning when 'explicitOff' is false
private final boolean explicitAuto;
// threshold (in element count) at which a EXPLICIT HLL is converted to a
// SPARSE or FULL HLL, always greater than or equal to zero and always a
// power of two OR simply zero
// NOTE: this only has meaning when 'explicitOff' is false
private final int explicitThreshold;
// ........................................................................
// SPARSE-specific constants
// the computed width of the short words
private final int shortWordLength;
// flag indicating if the SPARSE representation should not be used
private final boolean sparseOff;
// threshold (in register count) at which a SPARSE HLL is converted to a
// FULL HLL, always greater than zero
private final int sparseThreshold;
// ........................................................................
// Probabilistic algorithm constants
// the number of registers, will always be a power of 2
private final int m;
// a mask of the log2m bits set to one and the rest to zero
private final int mBitsMask;
// a mask as wide as a register (see #fromBytes())
private final int valueMask;
// mask used to ensure that p(w) does not overflow register (see #Constructor() and #addRaw())
private final long pwMaxMask;
// alpha * m^2 (the constant in the "'raw' HyperLogLog estimator")
private final double alphaMSquared;
// the cutoff value of the estimator for using the "small" range cardinality
// correction formula
private final double smallEstimatorCutoff;
// the cutoff value of the estimator for using the "large" range cardinality
// correction formula
private final double largeEstimatorCutoff;
// ========================================================================
/**
* NOTE: Arguments here are named and structured identically to those in the
* PostgreSQL implementation, which can be found
* <a href="https://github.com/aggregateknowledge/postgresql-hll/blob/master/README.markdown#explanation-of-parameters-and-tuning">here</a>.
*
* @param log2m log-base-2 of the number of registers used in the HyperLogLog
* algorithm. Must be at least 4 and at most 30.
* @param regwidth number of bits used per register in the HyperLogLog
* algorithm. Must be at least 1 and at most 8.
* @param expthresh tunes when the {@link HLLType#EXPLICIT} to
* {@link HLLType#SPARSE} promotion occurs,
* based on the set's cardinality. Must be at least -1 and at most 18.
* <table>
* <thead><tr><th><code>expthresh</code> value</th><th>Meaning</th></tr></thead>
* <tbody>
* <tr>
* <td>-1</td>
* <td>Promote at whatever cutoff makes sense for optimal memory usage. ('auto' mode)</td>
* </tr>
* <tr>
* <td>0</td>
* <td>Skip <code>EXPLICIT</code> representation in hierarchy.</td>
* </tr>
* <tr>
* <td>1-18</td>
* <td>Promote at 2<sup>expthresh - 1</sup> cardinality</td>
* </tr>
* </tbody>
* </table>
* @param sparseon Flag indicating if the {@link HLLType#SPARSE}
* representation should be used.
* @param type the type in the promotion hierarchy which this instance should
* start at. This cannot be <code>null</code>.
*/
public HLL(final int log2m, final int regwidth, final int expthresh, final boolean sparseon, final HLLType type) {
this.log2m = log2m;
if((log2m < MINIMUM_LOG2M_PARAM) || (log2m > MAXIMUM_LOG2M_PARAM)) {
throw new IllegalArgumentException("'log2m' must be at least " + MINIMUM_LOG2M_PARAM + " and at most " + MAXIMUM_LOG2M_PARAM + " (was: " + log2m + ")");
}
this.regwidth = regwidth;
if((regwidth < MINIMUM_REGWIDTH_PARAM) || (regwidth > MAXIMUM_REGWIDTH_PARAM)) {
throw new IllegalArgumentException("'regwidth' must be at least " + MINIMUM_REGWIDTH_PARAM + " and at most " + MAXIMUM_REGWIDTH_PARAM + " (was: " + regwidth + ")");
}
this.m = (1 << log2m);
this.mBitsMask = m - 1;
this.valueMask = (1 << regwidth) - 1;
this.pwMaxMask = HLLUtil.pwMaxMask(regwidth);
this.alphaMSquared = HLLUtil.alphaMSquared(m);
this.smallEstimatorCutoff = HLLUtil.smallEstimatorCutoff(m);
this.largeEstimatorCutoff = HLLUtil.largeEstimatorCutoff(log2m, regwidth);
if(expthresh == -1) {
this.explicitAuto = true;
this.explicitOff = false;
// NOTE: This math matches the size calculation in the PostgreSQL impl.
final long fullRepresentationSize = (this.regwidth * (long)this.m + 7/*round up to next whole byte*/)/Byte.SIZE;
final int numLongs = (int)(fullRepresentationSize / 8/*integer division to round down*/);
if(numLongs > MAXIMUM_EXPLICIT_THRESHOLD) {
this.explicitThreshold = MAXIMUM_EXPLICIT_THRESHOLD;
} else {
this.explicitThreshold = numLongs;
}
} else if(expthresh == 0) {
this.explicitAuto = false;
this.explicitOff = true;
this.explicitThreshold = 0;
} else if((expthresh > 0) && (expthresh <= MAXIMUM_EXPTHRESH_PARAM)){
this.explicitAuto = false;
this.explicitOff = false;
this.explicitThreshold = (1 << (expthresh - 1));
} else {
throw new IllegalArgumentException("'expthresh' must be at least " + MINIMUM_EXPTHRESH_PARAM + " and at most " + MAXIMUM_EXPTHRESH_PARAM + " (was: " + expthresh + ")");
}
this.shortWordLength = (regwidth + log2m);
this.sparseOff = !sparseon;
if(this.sparseOff) {
this.sparseThreshold = 0;
} else {
// TODO improve this cutoff to include the cost overhead of Java
// members/objects
final int largestPow2LessThanCutoff =
(int)NumberUtil.log2((this.m * this.regwidth) / this.shortWordLength);
this.sparseThreshold = (1 << largestPow2LessThanCutoff);
}
initializeStorage(type);
}
/**
* Construct an empty HLL with the given {@code log2m} and {@code regwidth}.<p/>
*
* This is equivalent to calling <code>HLL(log2m, regwidth, -1, true, HLLType.EMPTY)</code>.
*
* @param log2m log-base-2 of the number of registers used in the HyperLogLog
* algorithm. Must be at least 4 and at most 30.
* @param regwidth number of bits used per register in the HyperLogLog
* algorithm. Must be at least 1 and at most 8.
*
* @see #HLL(int, int, int, boolean, HLLType)
*/
public HLL(final int log2m, final int regwidth) {
this(log2m, regwidth, -1, true, HLLType.EMPTY);
}
// -------------------------------------------------------------------------
/**
* Convenience constructor for testing. Assumes that both {@link HLLType#EXPLICIT}
* and {@link HLLType#SPARSE} representations should be enabled.
*
* @param log2m log-base-2 of the number of registers used in the HyperLogLog
* algorithm. Must be at least 4 and at most 30.
* @param regwidth number of bits used per register in the HyperLogLog
* algorithm. Must be at least 1 and at most 8.
* @param explicitThreshold cardinality threshold at which the {@link HLLType#EXPLICIT}
* representation should be promoted to {@link HLLType#SPARSE}.
* This must be greater than zero and less than or equal to {@value #MAXIMUM_EXPLICIT_THRESHOLD}.
* @param sparseThreshold register count threshold at which the {@link HLLType#SPARSE}
* representation should be promoted to {@link HLLType#FULL}.
* This must be greater than zero.
* @param type the type in the promotion hierarchy which this instance should
* start at. This cannot be <code>null</code>.
*/
/*package, for testing*/ HLL(final int log2m, final int regwidth, final int explicitThreshold, final int sparseThreshold, final HLLType type) {
this.log2m = log2m;
if((log2m < MINIMUM_LOG2M_PARAM) || (log2m > MAXIMUM_LOG2M_PARAM)) {
throw new IllegalArgumentException("'log2m' must be at least " + MINIMUM_LOG2M_PARAM + " and at most " + MAXIMUM_LOG2M_PARAM + " (was: " + log2m + ")");
}
this.regwidth = regwidth;
if((regwidth < MINIMUM_REGWIDTH_PARAM) || (regwidth > MAXIMUM_REGWIDTH_PARAM)) {
throw new IllegalArgumentException("'regwidth' must be at least " + MINIMUM_REGWIDTH_PARAM + " and at most " + MAXIMUM_REGWIDTH_PARAM + " (was: " + regwidth + ")");
}
this.m = (1 << log2m);
this.mBitsMask = m - 1;
this.valueMask = (1 << regwidth) - 1;
this.pwMaxMask = HLLUtil.pwMaxMask(regwidth);
this.alphaMSquared = HLLUtil.alphaMSquared(m);
this.smallEstimatorCutoff = HLLUtil.smallEstimatorCutoff(m);
this.largeEstimatorCutoff = HLLUtil.largeEstimatorCutoff(log2m, regwidth);
this.explicitAuto = false;
this.explicitOff = false;
this.explicitThreshold = explicitThreshold;
if((explicitThreshold < 1) || (explicitThreshold > MAXIMUM_EXPLICIT_THRESHOLD)) {
throw new IllegalArgumentException("'explicitThreshold' must be at least 1 and at most " + MAXIMUM_EXPLICIT_THRESHOLD + " (was: " + explicitThreshold + ")");
}
this.shortWordLength = (regwidth + log2m);
this.sparseOff = false;
this.sparseThreshold = sparseThreshold;
initializeStorage(type);
}
/**
* @return the type in the promotion hierarchy of this instance. This will
* never be <code>null</code>.
*/
public HLLType getType() { return type; }
// ========================================================================
// Add
/**
* Adds <code>rawValue</code> directly to the HLL.
*
* @param rawValue the value to be added. It is very important that this
* value <em>already be hashed</em> with a strong (but not
* necessarily cryptographic) hash function. For instance, the
* Murmur3 implementation in
* <a href="http://guava-libraries.googlecode.com/git/guava/src/com/google/common/hash/Murmur3_128HashFunction.java">
* Google's Guava</a> library is an excellent hash function for this
* purpose and, for seeds greater than zero, matches the output
* of the hash provided in the PostgreSQL implementation.
*/
public void addRaw(final long rawValue) {
switch(type) {
case EMPTY: {
// NOTE: EMPTY type is always promoted on #addRaw()
if(explicitThreshold > 0) {
initializeStorage(HLLType.EXPLICIT);
explicitStorage.add(rawValue);
} else if(!sparseOff) {
initializeStorage(HLLType.SPARSE);
addRawSparseProbabilistic(rawValue);
} else {
initializeStorage(HLLType.FULL);
addRawProbabilistic(rawValue);
}
return;
}
case EXPLICIT: {
explicitStorage.add(rawValue);
// promotion, if necessary
if(explicitStorage.size() > explicitThreshold) {
if(!sparseOff) {
initializeStorage(HLLType.SPARSE);
for (LongCursor c : explicitStorage) {
addRawSparseProbabilistic(c.value);
}
} else {
initializeStorage(HLLType.FULL);
for (LongCursor c : explicitStorage) {
addRawProbabilistic(c.value);
}
}
explicitStorage = null;
}
return;
}
case SPARSE: {
addRawSparseProbabilistic(rawValue);
// promotion, if necessary
if(sparseProbabilisticStorage.size() > sparseThreshold) {
initializeStorage(HLLType.FULL);
for(IntByteCursor c : sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
probabilisticStorage.setMaxRegister(registerIndex, registerValue);
}
sparseProbabilisticStorage = null;
}
return;
}
case FULL:
addRawProbabilistic(rawValue);
return;
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
}
// ------------------------------------------------------------------------
// #addRaw(..) helpers
/**
* Adds the raw value to the {@link #sparseProbabilisticStorage}.
* {@link #type} must be {@link HLLType#SPARSE}.
*
* @param rawValue the raw value to add to the sparse storage.
*/
private void addRawSparseProbabilistic(final long rawValue) {
// p(w): position of the least significant set bit (one-indexed)
// By contract: p(w) <= 2^(registerValueInBits) - 1 (the max register value)
//
// By construction of pwMaxMask (see #Constructor()),
// lsb(pwMaxMask) = 2^(registerValueInBits) - 2,
// thus lsb(any_long | pwMaxMask) <= 2^(registerValueInBits) - 2,
// thus 1 + lsb(any_long | pwMaxMask) <= 2^(registerValueInBits) -1.
final long substreamValue = (rawValue >>> log2m);
final byte p_w;
if(substreamValue == 0L) {
// The paper does not cover p(0x0), so the special value 0 is used.
// 0 is the original initialization value of the registers, so by
// doing this the multiset simply ignores it. This is acceptable
// because the probability is 1/(2^(2^registerSizeInBits)).
p_w = 0;
} else {
p_w = (byte)(1 + BitUtil.leastSignificantBit(substreamValue| pwMaxMask));
}
// Short-circuit if the register is being set to zero, since algorithmically
// this corresponds to an "unset" register, and "unset" registers aren't
// stored to save memory. (The very reason this sparse implementation
// exists.) If a register is set to zero it will break the #algorithmCardinality
// code.
if(p_w == 0) {
return;
}
// NOTE: no +1 as in paper since 0-based indexing
final int j = (int)(rawValue & mBitsMask);
final byte currentValue = sparseProbabilisticStorage.getOrDefault(j, (byte) 0);
if(p_w > currentValue) {
sparseProbabilisticStorage.put(j, p_w);
}
}
/**
* Adds the raw value to the {@link #probabilisticStorage}.
* {@link #type} must be {@link HLLType#FULL}.
*
* @param rawValue the raw value to add to the full probabilistic storage.
*/
private void addRawProbabilistic(final long rawValue) {
// p(w): position of the least significant set bit (one-indexed)
// By contract: p(w) <= 2^(registerValueInBits) - 1 (the max register value)
//
// By construction of pwMaxMask (see #Constructor()),
// lsb(pwMaxMask) = 2^(registerValueInBits) - 2,
// thus lsb(any_long | pwMaxMask) <= 2^(registerValueInBits) - 2,
// thus 1 + lsb(any_long | pwMaxMask) <= 2^(registerValueInBits) -1.
final long substreamValue = (rawValue >>> log2m);
final byte p_w;
if (substreamValue == 0L) {
// The paper does not cover p(0x0), so the special value 0 is used.
// 0 is the original initialization value of the registers, so by
// doing this the multiset simply ignores it. This is acceptable
// because the probability is 1/(2^(2^registerSizeInBits)).
p_w = 0;
} else {
p_w = (byte)(1 + BitUtil.leastSignificantBit(substreamValue| pwMaxMask));
}
// Short-circuit if the register is being set to zero, since algorithmically
// this corresponds to an "unset" register, and "unset" registers aren't
// stored to save memory. (The very reason this sparse implementation
// exists.) If a register is set to zero it will break the #algorithmCardinality
// code.
if(p_w == 0) {
return;
}
// NOTE: no +1 as in paper since 0-based indexing
final int j = (int)(rawValue & mBitsMask);
probabilisticStorage.setMaxRegister(j, p_w);
}
// ------------------------------------------------------------------------
// Storage helper
/**
* Initializes storage for the specified {@link HLLType} and changes the
* instance's {@link #type}.
*
* @param type the {@link HLLType} to initialize storage for. This cannot be
* <code>null</code> and must be an instantiable type. (For instance,
* it cannot be {@link HLLType#UNDEFINED}.)
*/
private void initializeStorage(final HLLType type) {
this.type = type;
switch(type) {
case EMPTY:
// nothing to be done
break;
case EXPLICIT:
this.explicitStorage = new LongHashSet();
break;
case SPARSE:
this.sparseProbabilisticStorage = new IntByteHashMap();
break;
case FULL:
this.probabilisticStorage = new BitVector(regwidth, m);
break;
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
}
// ========================================================================
// Cardinality
/**
* Computes the cardinality of the HLL.
*
* @return the cardinality of HLL. This will never be negative.
*/
public long cardinality() {
switch(type) {
case EMPTY:
return 0/*by definition*/;
case EXPLICIT:
return explicitStorage.size();
case SPARSE:
return (long)Math.ceil(sparseProbabilisticAlgorithmCardinality());
case FULL:
return (long)Math.ceil(fullProbabilisticAlgorithmCardinality());
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
}
// ------------------------------------------------------------------------
// Cardinality helpers
/**
* Computes the exact cardinality value returned by the HLL algorithm when
* represented as a {@link HLLType#SPARSE} HLL. Kept
* separate from {@link #cardinality()} for testing purposes. {@link #type}
* must be {@link HLLType#SPARSE}.
*
* @return the exact, unrounded cardinality given by the HLL algorithm
*/
/*package, for testing*/ double sparseProbabilisticAlgorithmCardinality() {
final int m = this.m/*for performance*/;
// compute the "indicator function" -- sum(2^(-M[j])) where M[j] is the
// 'j'th register value
double sum = 0;
int numberOfZeroes = 0/*"V" in the paper*/;
for(int j=0; j<m; j++) {
final long register = sparseProbabilisticStorage.getOrDefault(j, (byte) 0);
sum += 1.0 / (1L << register);
if(register == 0L) numberOfZeroes++;
}
// apply the estimate and correction to the indicator function
final double estimator = alphaMSquared / sum;
if((numberOfZeroes != 0) && (estimator < smallEstimatorCutoff)) {
return HLLUtil.smallEstimator(m, numberOfZeroes);
} else if(estimator <= largeEstimatorCutoff) {
return estimator;
} else {
return HLLUtil.largeEstimator(log2m, regwidth, estimator);
}
}
/**
* Computes the exact cardinality value returned by the HLL algorithm when
* represented as a {@link HLLType#FULL} HLL. Kept
* separate from {@link #cardinality()} for testing purposes. {@link #type}
* must be {@link HLLType#FULL}.
*
* @return the exact, unrounded cardinality given by the HLL algorithm
*/
/*package, for testing*/ double fullProbabilisticAlgorithmCardinality() {
final int m = this.m/*for performance*/;
// compute the "indicator function" -- sum(2^(-M[j])) where M[j] is the
// 'j'th register value
double sum = 0;
int numberOfZeroes = 0/*"V" in the paper*/;
final LongIterator iterator = probabilisticStorage.registerIterator();
while(iterator.hasNext()) {
final long register = iterator.next();
sum += 1.0 / (1L << register);
if(register == 0L) numberOfZeroes++;
}
// apply the estimate and correction to the indicator function
final double estimator = alphaMSquared / sum;
if((numberOfZeroes != 0) && (estimator < smallEstimatorCutoff)) {
return HLLUtil.smallEstimator(m, numberOfZeroes);
} else if(estimator <= largeEstimatorCutoff) {
return estimator;
} else {
return HLLUtil.largeEstimator(log2m, regwidth, estimator);
}
}
// ========================================================================
// Clear
/**
* Clears the HLL. The HLL will have cardinality zero and will act as if no
* elements have been added.<p/>
*
* NOTE: Unlike {@link #addRaw(long)}, <code>clear</code> does NOT handle
* transitions between {@link HLLType}s - a probabilistic type will remain
* probabilistic after being cleared.
*/
public void clear() {
switch(type) {
case EMPTY:
return /*do nothing*/;
case EXPLICIT:
explicitStorage.release();
return;
case SPARSE:
sparseProbabilisticStorage.release();
return;
case FULL:
probabilisticStorage.fill(0);
return;
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
}
// ========================================================================
// Union
/**
* Computes the union of HLLs and stores the result in this instance.
*
* @param other the other {@link HLL} instance to union into this one. This
* cannot be <code>null</code>.
*/
public void union(final HLL other) {
// TODO: verify HLLs are compatible
final HLLType otherType = other.getType();
if(type.equals(otherType)) {
homogeneousUnion(other);
return;
} else {
heterogenousUnion(other);
return;
}
}
// ------------------------------------------------------------------------
// Union helpers
/**
* Computes the union of two HLLs, of different types, and stores the
* result in this instance.
*
* @param other the other {@link HLL} instance to union into this one. This
* cannot be <code>null</code>.
*/
/*package, for testing*/ void heterogenousUnion(final HLL other) {
/*
* The logic here is divided into two sections: unions with an EMPTY
* HLL, and unions between EXPLICIT/SPARSE/FULL
* HLL.
*
* Between those two sections, all possible heterogeneous unions are
* covered. Should another type be added to HLLType whose unions
* are not easily reduced (say, as EMPTY's are below) this may be more
* easily implemented as Strategies. However, that is unnecessary as it
* stands.
*/
// ....................................................................
// Union with an EMPTY
if(HLLType.EMPTY.equals(type)) {
// NOTE: The union of empty with non-empty HLL is just a
// clone of the non-empty.
switch(other.getType()) {
case EXPLICIT: {
// src: EXPLICIT
// dest: EMPTY
if(other.explicitStorage.size() <= explicitThreshold) {
type = HLLType.EXPLICIT;
explicitStorage = other.explicitStorage.clone();
} else {
if(!sparseOff) {
initializeStorage(HLLType.SPARSE);
} else {
initializeStorage(HLLType.FULL);
}
for(LongCursor c : other.explicitStorage) {
addRaw(c.value);
}
}
return;
}
case SPARSE: {
// src: SPARSE
// dest: EMPTY
if(!sparseOff) {
type = HLLType.SPARSE;
sparseProbabilisticStorage = other.sparseProbabilisticStorage.clone();
} else {
initializeStorage(HLLType.FULL);
for(IntByteCursor c : other.sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
probabilisticStorage.setMaxRegister(registerIndex, registerValue);
}
}
return;
}
default/*case FULL*/: {
// src: FULL
// dest: EMPTY
type = HLLType.FULL;
probabilisticStorage = other.probabilisticStorage.clone();
return;
}
}
} else if (HLLType.EMPTY.equals(other.getType())) {
// source is empty, so just return destination since it is unchanged
return;
} /* else -- both of the sets are not empty */
// ....................................................................
// NOTE: Since EMPTY is handled above, the HLLs are non-EMPTY below
switch(type) {
case EXPLICIT: {
// src: FULL/SPARSE
// dest: EXPLICIT
// "Storing into destination" cannot be done (since destination
// is by definition of smaller capacity than source), so a clone
// of source is made and values from destination are inserted
// into that.
// Determine source and destination storage.
// NOTE: destination storage may change through promotion if
// source is SPARSE.
if(HLLType.SPARSE.equals(other.getType())) {
if(!sparseOff) {
type = HLLType.SPARSE;
sparseProbabilisticStorage = other.sparseProbabilisticStorage.clone();
} else {
initializeStorage(HLLType.FULL);
for(IntByteCursor c : other.sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
probabilisticStorage.setMaxRegister(registerIndex, registerValue);
}
}
} else /*source is HLLType.FULL*/ {
type = HLLType.FULL;
probabilisticStorage = other.probabilisticStorage.clone();
}
for(LongCursor c : explicitStorage) {
addRaw(c.value);
}
explicitStorage = null;
return;
}
case SPARSE: {
if(HLLType.EXPLICIT.equals(other.getType())) {
// src: EXPLICIT
// dest: SPARSE
// Add the raw values from the source to the destination.
for(LongCursor c : other.explicitStorage) {
addRaw(c.value);
}
// NOTE: addRaw will handle promotion cleanup
} else /*source is HLLType.FULL*/ {
// src: FULL
// dest: SPARSE
// "Storing into destination" cannot be done (since destination
// is by definition of smaller capacity than source), so a
// clone of source is made and registers from the destination
// are merged into the clone.
type = HLLType.FULL;
probabilisticStorage = other.probabilisticStorage.clone();
for(IntByteCursor c : sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
probabilisticStorage.setMaxRegister(registerIndex, registerValue);
}
sparseProbabilisticStorage = null;
}
return;
}
default/*destination is HLLType.FULL*/: {
if(HLLType.EXPLICIT.equals(other.getType())) {
// src: EXPLICIT
// dest: FULL
// Add the raw values from the source to the destination.
// Promotion is not possible, so don't bother checking.
for(LongCursor c : other.explicitStorage) {
addRaw(c.value);
}
} else /*source is HLLType.SPARSE*/ {
// src: SPARSE
// dest: FULL
// Merge the registers from the source into the destination.
// Promotion is not possible, so don't bother checking.
for(IntByteCursor c : other.sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
probabilisticStorage.setMaxRegister(registerIndex, registerValue);
}
}
}
}
}
/**
* Computes the union of two HLLs of the same type, and stores the
* result in this instance.
*
* @param other the other {@link HLL} instance to union into this one. This
* cannot be <code>null</code>.
*/
private void homogeneousUnion(final HLL other) {
switch(type) {
case EMPTY:
// union of empty and empty is empty
return;
case EXPLICIT:
for(LongCursor c : other.explicitStorage) {
addRaw(c.value);
}
// NOTE: #addRaw() will handle promotion, if necessary
return;
case SPARSE:
for(IntByteCursor c : other.sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
final byte currentRegisterValue = sparseProbabilisticStorage.get(registerIndex);
if(registerValue > currentRegisterValue) {
sparseProbabilisticStorage.put(registerIndex, registerValue);
}
}
// promotion, if necessary
if(sparseProbabilisticStorage.size() > sparseThreshold) {
initializeStorage(HLLType.FULL);
for(IntByteCursor c : sparseProbabilisticStorage) {
final int registerIndex = c.key;
final byte registerValue = c.value;
probabilisticStorage.setMaxRegister(registerIndex, registerValue);
}
sparseProbabilisticStorage = null;
}
return;
case FULL:
for(int i=0; i<m; i++) {
final long registerValue = other.probabilisticStorage.getRegister(i);
probabilisticStorage.setMaxRegister(i, registerValue);
}
return;
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
}
// ========================================================================
// Serialization
/**
* Serializes the HLL to an array of bytes in correspondence with the format
* of the default schema version, {@link SerializationUtil#DEFAULT_SCHEMA_VERSION}.
*
* @return the array of bytes representing the HLL. This will never be
* <code>null</code> or empty.
*/
public byte[] toBytes() {
return toBytes(SerializationUtil.DEFAULT_SCHEMA_VERSION);
}
/**
* Serializes the HLL to an array of bytes in correspondence with the format
* of the specified schema version.
*
* @param schemaVersion the schema version dictating the serialization format
* @return the array of bytes representing the HLL. This will never be
* <code>null</code> or empty.
*/
public byte[] toBytes(final ISchemaVersion schemaVersion) {
final byte[] bytes;
switch(type) {
case EMPTY:
bytes = new byte[schemaVersion.paddingBytes(type)];
break;
case EXPLICIT: {
final IWordSerializer serializer =
schemaVersion.getSerializer(type, Long.SIZE, explicitStorage.size());
final long[] values = explicitStorage.toArray();
Arrays.sort(values);
for(final long value : values) {
serializer.writeWord(value);
}
bytes = serializer.getBytes();
break;
}
case SPARSE: {
final IWordSerializer serializer =
schemaVersion.getSerializer(type, shortWordLength, sparseProbabilisticStorage.size());
final int[] indices = sparseProbabilisticStorage.keys().toArray();
Arrays.sort(indices);
for(final int registerIndex : indices) {
assert sparseProbabilisticStorage.containsKey(registerIndex);
final long registerValue = sparseProbabilisticStorage.get(registerIndex);
// pack index and value into "short word"
final long shortWord = ((registerIndex << regwidth) | registerValue);
serializer.writeWord(shortWord);
}
bytes = serializer.getBytes();
break;
}
case FULL: {
final IWordSerializer serializer = schemaVersion.getSerializer(type, regwidth, m);
probabilisticStorage.getRegisterContents(serializer);
bytes = serializer.getBytes();
break;
}
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
final IHLLMetadata metadata = new HLLMetadata(schemaVersion.schemaVersionNumber(),
type,
log2m,
regwidth,
(int)NumberUtil.log2(explicitThreshold),
explicitOff,
explicitAuto,
!sparseOff);
schemaVersion.writeMetadata(bytes, metadata);
return bytes;
}
/**
* Deserializes the HLL (in {@link #toBytes(ISchemaVersion)} format) serialized
* into <code>bytes</code>.<p/>
*
* @param bytes the serialized bytes of new HLL
* @return the deserialized HLL. This will never be <code>null</code>.
*
* @see #toBytes(ISchemaVersion)
*/
public static HLL fromBytes(final byte[] bytes) {
final ISchemaVersion schemaVersion = SerializationUtil.getSchemaVersion(bytes);
final IHLLMetadata metadata = schemaVersion.readMetadata(bytes);
final HLLType type = metadata.HLLType();
final int regwidth = metadata.registerWidth();
final int log2m = metadata.registerCountLog2();
final boolean sparseon = metadata.sparseEnabled();
final int expthresh;
if(metadata.explicitAuto()) {
expthresh = -1;
} else if(metadata.explicitOff()) {
expthresh = 0;
} else {
// NOTE: take into account that the postgres-compatible constructor
// subtracts one before taking a power of two.
expthresh = metadata.log2ExplicitCutoff() + 1;
}
final HLL hll = new HLL(log2m, regwidth, expthresh, sparseon, type);
// Short-circuit on empty, which needs no other deserialization.
if(HLLType.EMPTY.equals(type)) {
return hll;
}
final int wordLength;
switch(type) {
case EXPLICIT:
wordLength = Long.SIZE;
break;
case SPARSE:
wordLength = hll.shortWordLength;
break;
case FULL:
wordLength = hll.regwidth;
break;
default:
throw new RuntimeException("Unsupported HLL type " + type);
}
final IWordDeserializer deserializer =