Skip to content

Commit 64f250a

Browse files
Merge pull request #196 from DataSketches/FixSerDeIssue165
Fix SerDe Issue 195
2 parents 99fd7fb + 276d673 commit 64f250a

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/main/java/com/yahoo/sketches/quantiles/DoublesUnionImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ static DoublesUnionImpl heapifyInstance(final DoublesSketch sketch) {
7777
* @return a DoublesUnion object
7878
*/
7979
static DoublesUnionImpl heapifyInstance(final Memory srcMem) {
80-
final long n = srcMem.getLong(PreambleUtil.N_LONG);
80+
final int preLongs = srcMem.getByte(PreambleUtil.PREAMBLE_LONGS_BYTE) & 0xFF;
8181
final int k = srcMem.getShort(PreambleUtil.K_SHORT) & 0xFFFF;
82-
final HeapUpdateDoublesSketch sketch = (n == 0)
82+
final HeapUpdateDoublesSketch sketch = (preLongs == 1)
8383
? HeapUpdateDoublesSketch.newInstance(k)
8484
: HeapUpdateDoublesSketch.heapifyInstance(srcMem);
8585
final DoublesUnionImpl union = new DoublesUnionImpl(k);

src/test/java/com/yahoo/sketches/quantiles/DoublesUnionImplTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,17 @@ public void isSameResourceDirect() {
693693
Assert.assertFalse(union.isSameResource(mem2));
694694
}
695695

696+
@SuppressWarnings("unused")
697+
@Test
698+
public void emptyUnionSerDeIssue195() {
699+
DoublesUnion union = DoublesUnion.builder().build();
700+
byte[] byteArr = union.toByteArray();
701+
Memory mem = Memory.wrap(byteArr);
702+
DoublesUnion union2 = DoublesUnionBuilder.heapify(mem);
703+
Assert.assertEquals(mem.getCapacity(), 8L);
704+
Assert.assertTrue(union2.isEmpty());
705+
}
706+
696707
@Test
697708
public void printlnTest() {
698709
println("PRINTING: " + this.getClass().getName());

src/test/java/com/yahoo/sketches/tuple/FilterTest.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.yahoo.sketches.tuple;
22

3+
import java.util.Random;
4+
35
import org.testng.Assert;
46
import org.testng.annotations.Test;
57

6-
import java.util.Random;
7-
88
public class FilterTest {
99

10-
private final int numberOfElements = 100;
10+
private static final int numberOfElements = 100;
11+
private static final Random random = new Random(1);//deterministic for this class
1112

1213
@Test
1314
public void emptySketch() {
@@ -39,7 +40,8 @@ public void nullSketch() {
3940

4041
@Test
4142
public void filledSketchShouldBehaveTheSame() {
42-
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
43+
UpdatableSketch<Double, DoubleSummary> sketch =
44+
new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
4345

4446
fillSketch(sketch, numberOfElements, 0.0);
4547

@@ -56,7 +58,8 @@ public void filledSketchShouldBehaveTheSame() {
5658

5759
@Test
5860
public void filledSketchShouldFilterOutElements() {
59-
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
61+
UpdatableSketch<Double, DoubleSummary> sketch =
62+
new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
6063

6164
fillSketch(sketch, numberOfElements, 0.0);
6265
fillSketch(sketch, 2 * numberOfElements, 1.0);
@@ -74,7 +77,8 @@ public void filledSketchShouldFilterOutElements() {
7477

7578
@Test
7679
public void filteringInEstimationMode() {
77-
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
80+
UpdatableSketch<Double, DoubleSummary> sketch =
81+
new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build();
7882

7983
int n = 10000;
8084
fillSketch(sketch, n, 0.0);
@@ -93,7 +97,9 @@ public void filteringInEstimationMode() {
9397

9498
@Test
9599
public void nonEmptySketchWithNoEntries() {
96-
UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).setSamplingProbability(0.0001f).build();
100+
UpdatableSketch<Double, DoubleSummary> sketch =
101+
new UpdatableSketchBuilder<>(
102+
new DoubleSummaryFactory()).setSamplingProbability(0.0001f).build();
97103
sketch.update(0, 0.0);
98104

99105
Assert.assertFalse(sketch.isEmpty());
@@ -110,11 +116,12 @@ public void nonEmptySketchWithNoEntries() {
110116
Assert.assertEquals(filteredSketch.getUpperBound(1), sketch.getUpperBound(1));
111117
}
112118

113-
private static void fillSketch(UpdatableSketch<Double, DoubleSummary> sketch, int numberOfElements, Double sketchValue) {
114-
Random random = new Random();
119+
private static void fillSketch(UpdatableSketch<Double, DoubleSummary> sketch,
120+
int numberOfElements, Double sketchValue) {
121+
115122

116-
for (int cont = 0; cont < numberOfElements; cont++) {
117-
sketch.update(random.nextLong(), sketchValue);
118-
}
123+
for (int cont = 0; cont < numberOfElements; cont++) {
124+
sketch.update(random.nextLong(), sketchValue);
125+
}
119126
}
120127
}

0 commit comments

Comments
 (0)