Skip to content

Commit de5cc2d

Browse files
committed
Fix an issue with HLL serialization.
HLL#fromBytes, HLL#toBytes, and HLL#clone weren't taking into account the Postgres-compatible constructor.
1 parent 1e450d8 commit de5cc2d

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<packaging>jar</packaging>
88
<description>HyperLogLog: approximate distinct value counting algoritm</description>
99
<url>https://github.com/aggregateknowledge/java-hll</url>
10-
<version>1.5.0</version>
10+
<version>1.5.1</version>
1111
<name>HyperLogLog in Java</name>
1212
<licenses>
1313
<license>

src/main/java/net/agkn/hll/HLL.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,21 @@ public static HLL fromBytes(final byte[] bytes) {
947947
final HLLType type = metadata.HLLType();
948948
final int regwidth = metadata.registerWidth();
949949
final int log2m = metadata.registerCountLog2();
950-
final int expthresh = metadata.log2ExplicitCutoff();
951950
final boolean sparseon = metadata.sparseEnabled();
952951

952+
final int expthresh;
953+
if(metadata.explicitAuto()) {
954+
expthresh = -1;
955+
} else if(metadata.explicitOff()) {
956+
expthresh = 0;
957+
} else {
958+
// NOTE: take into account that the postgres-compatible constructor
959+
// subtracts one before taking a power of two.
960+
expthresh = metadata.log2ExplicitCutoff() + 1;
961+
}
953962

954963
final HLL hll = new HLL(log2m, regwidth, expthresh, sparseon, type);
964+
955965
// Short-circuit on empty, which needs no other deserialization.
956966
if(HLLType.EMPTY.equals(type)) {
957967
return hll;
@@ -1042,7 +1052,7 @@ public HLL clone() throws CloneNotSupportedException {
10421052
//
10431053
// Since explicitThreshold is a power of two and only has a single
10441054
// bit set, finding the LSB is the same as finding the inverse
1045-
copyExpthresh = BitUtil.leastSignificantBit(explicitThreshold);
1055+
copyExpthresh = BitUtil.leastSignificantBit(explicitThreshold) + 1;
10461056
}
10471057
final HLL copy = new HLL(log2m, regwidth, copyExpthresh, !sparseOff/*sparseOn*/, type);
10481058
switch(type) {

src/test/java/net/agkn/hll/serialization/HLLSerializationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
import net.agkn.hll.HLL;
44
import net.agkn.hll.HLLType;
5-
import org.testng.Assert;
65
import org.testng.annotations.Test;
76

87
import java.util.ArrayList;
98
import java.util.Collection;
10-
import java.util.LinkedList;
119
import java.util.List;
1210
import java.util.Random;
1311

1412
import static net.agkn.hll.HLL.MAXIMUM_EXPTHRESH_PARAM;
15-
import static net.agkn.hll.HLL.MAXIMUM_LOG2M_PARAM;
1613
import static net.agkn.hll.HLL.MAXIMUM_REGWIDTH_PARAM;
1714
import static net.agkn.hll.HLL.MINIMUM_EXPTHRESH_PARAM;
1815
import static net.agkn.hll.HLL.MINIMUM_LOG2M_PARAM;
1916
import static net.agkn.hll.HLL.MINIMUM_REGWIDTH_PARAM;
2017
import static org.testng.Assert.assertEquals;
21-
import static org.testng.Assert.fail;
2218

2319
/**
2420
* Serialization smoke-tests.
@@ -68,9 +64,13 @@ private static void assertCardinality(final HLLType hllType, final Collection<Lo
6864
}
6965
HLL copy = HLL.fromBytes(hll.toBytes());
7066
assertEquals(copy.cardinality(), hll.cardinality());
67+
assertEquals(copy.getType(), hll.getType());
68+
assertEquals(copy.toBytes(), hll.toBytes());
7169

7270
HLL clone = hll.clone();
7371
assertEquals(clone.cardinality(), hll.cardinality());
72+
assertEquals(clone.getType(), hll.getType());
73+
assertEquals(clone.toBytes(), hll.toBytes());
7474
}
7575
}
7676
}

0 commit comments

Comments
 (0)