Skip to content

Commit

Permalink
Merge pull request #145 from DataSketches/reservoir_reset_test
Browse files Browse the repository at this point in the history
Reservoir reset test
  • Loading branch information
jmalkin committed Apr 11, 2017
2 parents 6254e77 + e2414e4 commit c6ed8d5
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ jarsIn/
*.jar
build.xml
.idea
*.iml
*.properties
*.releaseBackup
*.next
*.tag

# IntelliJ project files
*.iml
*.ipr
*.iws

# Jekyll
Gemfile
Gemfile.lock
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@
<plugin>
<groupId>com.versioneye</groupId>
<artifactId>versioneye-maven-plugin</artifactId>
<version>3.11.1</version>
<version>3.11.2</version>
</plugin>

</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright 2017, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/

package com.yahoo.sketches.quantiles;

import com.yahoo.memory.Memory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright 2017, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/

package com.yahoo.sketches.quantiles;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright 2017, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/

package com.yahoo.sketches.quantiles;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright 2017, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/

package com.yahoo.sketches.quantiles;

import static com.yahoo.sketches.quantiles.PreambleUtil.COMBINED_BUFFER;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright 2017, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/

package com.yahoo.sketches.quantiles;

import com.yahoo.memory.Memory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author Jon Malkin
*/
public class VarOptItemsSamples<T> implements Iterable<VarOptItemsSamples<T>.WeightedSample> {
class VarOptItemsSamples<T> implements Iterable<VarOptItemsSamples<T>.WeightedSample> {

private final VarOptItemsSketch<T> sketch_;
private VarOptItemsSketch<T>.Result sampleLists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @author Jon Malkin
* @author Kevin Lang
*/
public final class VarOptItemsSketch<T> {
final class VarOptItemsSketch<T> {
/**
* The smallest sampling array allocated: 16
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,29 @@ public void checkDownsampledUpdate() {
assertEquals(riu.getResult().getNumSamples(), smallK);
}

@Test
public void checkUnionResetWithInitialSmallK() {
final int maxK = 25;
final int sketchK = 10;
final ReservoirItemsUnion<Long> riu = ReservoirItemsUnion.getInstance(maxK);

ReservoirItemsSketch<Long> ris = getBasicSketch(2 * sketchK, sketchK); // in sampling mode
riu.update(ris);
assertEquals(riu.getMaxK(), maxK);
assertNotNull(riu.getResult());
assertEquals(riu.getResult().getK(), sketchK);

riu.reset();
assertNotNull(riu.getResult());

// feed in sketch in sampling mode, with larger k than old gadget
ris = getBasicSketch(2 * maxK, maxK + 1);
riu.update(ris);
assertEquals(riu.getMaxK(), maxK);
assertNotNull(riu.getResult());
assertEquals(riu.getResult().getK(), maxK);
}

@Test
public void checkNewGadget() {
final int maxK = 1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ public void checkDownsampledUpdate() {
assertEquals(rlu.getResult().getNumSamples(), smallK);
}

@Test
public void checkUnionResetWithInitialSmallK() {
final int maxK = 25;
final int sketchK = 10;
final ReservoirLongsUnion rlu = ReservoirLongsUnion.getInstance(maxK);

ReservoirLongsSketch rls = getBasicSketch(2 * sketchK, sketchK); // in sampling mode
rlu.update(rls);
assertEquals(rlu.getMaxK(), maxK);
assertNotNull(rlu.getResult());
assertEquals(rlu.getResult().getK(), sketchK);

rlu.reset();
assertNotNull(rlu.getResult());

// feed in sketch in sampling mode, with larger k than old gadget
rls = getBasicSketch(2 * maxK, maxK + 1);
rlu.update(rls);
assertEquals(rlu.getMaxK(), maxK);
assertNotNull(rlu.getResult());
assertEquals(rlu.getResult().getK(), maxK);
}

@Test
public void checkNewGadget() {
final int maxK = 1024;
Expand All @@ -219,34 +242,34 @@ public void checkNewGadget() {
final byte[] bigKBytes = bigKSketch.toByteArray();
final Memory bigKMem = new NativeMemory(bigKBytes);

ReservoirLongsUnion riu = ReservoirLongsUnion.getInstance(maxK);
riu.update(bigKMem);
assertNotNull(riu.getResult());
assertEquals(riu.getResult().getK(), maxK);
assertEquals(riu.getResult().getN(), maxK / 2);
ReservoirLongsUnion rlu = ReservoirLongsUnion.getInstance(maxK);
rlu.update(bigKMem);
assertNotNull(rlu.getResult());
assertEquals(rlu.getResult().getK(), maxK);
assertEquals(rlu.getResult().getN(), maxK / 2);

// sketch k < maxK but in sampling mode
final ReservoirLongsSketch smallKSketch = getBasicSketch(maxK, smallK);
final byte[] smallKBytes = smallKSketch.toByteArray();
final Memory smallKMem = new NativeMemory(smallKBytes);

riu = ReservoirLongsUnion.getInstance(maxK);
riu.update(smallKMem);
assertNotNull(riu.getResult());
assertTrue(riu.getResult().getK() < maxK);
assertEquals(riu.getResult().getK(), smallK);
assertEquals(riu.getResult().getN(), maxK);
rlu = ReservoirLongsUnion.getInstance(maxK);
rlu.update(smallKMem);
assertNotNull(rlu.getResult());
assertTrue(rlu.getResult().getK() < maxK);
assertEquals(rlu.getResult().getK(), smallK);
assertEquals(rlu.getResult().getN(), maxK);

// sketch k < maxK and in exact mode
final ReservoirLongsSketch smallKExactSketch = getBasicSketch(smallK, smallK);
final byte[] smallKExactBytes = smallKExactSketch.toByteArray();
final Memory smallKExactMem = new NativeMemory(smallKExactBytes);

riu = ReservoirLongsUnion.getInstance(maxK);
riu.update(smallKExactMem);
assertNotNull(riu.getResult());
assertEquals(riu.getResult().getK(), maxK);
assertEquals(riu.getResult().getN(), smallK);
rlu = ReservoirLongsUnion.getInstance(maxK);
rlu.update(smallKExactMem);
assertNotNull(rlu.getResult());
assertEquals(rlu.getResult().getK(), maxK);
assertEquals(rlu.getResult().getN(), smallK);
}

@Test
Expand Down

0 comments on commit c6ed8d5

Please sign in to comment.