Skip to content

Commit 8620698

Browse files
committed
CountingBloom: counters can overflow #20
1 parent 9b6c6d8 commit 8620698

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

fastfilter/src/main/java/org/fastfilter/bloom/count/CountingBloom.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public long getBitCount() {
3636
entryCount = Math.max(1, entryCount);
3737
this.k = k;
3838
this.seed = Hash.randomSeed();
39-
this.bits = (long) (4 * entryCount * bitsPerKey);
39+
// if the entryCount is very small, then there is a relatively high
40+
// probability that one of the counter overflows, so we add
41+
// a fixed number of bits (64 in this case) to reduce the probability of this
42+
// (this is a workaround only)
43+
this.bits = (long) (4 * entryCount * bitsPerKey) + 64;
4044
arraySize = (int) ((bits + 63) / 64);
4145
counts = new long[arraySize];
4246
}
@@ -53,6 +57,11 @@ public void add(long key) {
5357
int b = (int) hash;
5458
for (int i = 0; i < k; i++) {
5559
int index = Hash.reduce(a, arraySize << 4);
60+
int oldCount = (int) (counts[index >>> 4] >>> (index << 2)) & 0xf;
61+
if (oldCount >= 15) {
62+
// TODO we should also undo what was added so far
63+
throw new UnsupportedOperationException("Counter overflow");
64+
}
5665
counts[index >>> 4] += getBit(index);
5766
a += b;
5867
}

fastfilter/src/main/java/org/fastfilter/gcs/GolombCompressedSet.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public static GolombCompressedSet construct(long[] keys, int setting) {
2626
return new GolombCompressedSet(keys, keys.length, setting);
2727
}
2828

29+
// TODO rearrange Rice codes so that buckets have all variable parts first, then fixed part
30+
// this is to speed up lookup with large bucket sizes
31+
2932
GolombCompressedSet(long[] keys, int len, int fingerprintBits) {
3033
if (fingerprintBits < 4 || fingerprintBits > 50) {
3134
throw new IllegalArgumentException();

0 commit comments

Comments
 (0)