/** * Updates the internal set by adding entries from the given sketch * @param sketchIn input sketch to add to the internal set */ public void update(final Sketch<S> sketchIn) { if (sketchIn == null || sketchIn.isEmpty()) { return; } if (sketchIn.theta_ < theta_) { theta_ = sketchIn.theta_; } final SketchIterator<S> it = sketchIn.iterator(); while (it.next()) { sketch_.merge(it.getKey(), it.getSummary(), summarySetOps_); } }
@Test public void serialVersion1Compatibility() throws Exception { byte[] bytes = TestUtil.readBytesFromFile(getClass().getClassLoader() .getResource("CompactSketchWithDoubleSummary4K_serialVersion1.bin").getFile()); Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes), new DoubleSummaryDeserializer()); Assert.assertTrue(sketch.isEstimationMode()); Assert.assertEquals(sketch.getEstimate(), 8192, 8192 * 0.99); Assert.assertEquals(sketch.getRetainedEntries(), 4096); int count = 0; SketchIterator<DoubleSummary> it = sketch.iterator(); while (it.next()) { Assert.assertEquals(it.getSummary().getValue(), 1.0); count++; } Assert.assertEquals(count, 4096); }
@Test public void serializeDeserializeSmallExact() { UpdatableSketch<Double, DoubleSummary> us = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build(); us.update("a", 1.0); us.update("b", 1.0); us.update("c", 1.0); CompactSketch<DoubleSummary> sketch1 = us.compact(); Sketch<DoubleSummary> sketch2 = Sketches.heapifySketch(Memory.wrap(sketch1.toByteArray()), new DoubleSummaryDeserializer()); Assert.assertFalse(sketch2.isEmpty()); Assert.assertFalse(sketch2.isEstimationMode()); Assert.assertEquals(sketch2.getEstimate(), 3.0); Assert.assertEquals(sketch2.getLowerBound(1), 3.0); Assert.assertEquals(sketch2.getUpperBound(1), 3.0); Assert.assertEquals(sketch2.getRetainedEntries(), 3); Assert.assertEquals(sketch2.getThetaLong(), Long.MAX_VALUE); Assert.assertEquals(sketch2.getTheta(), 1.0); SketchIterator<DoubleSummary> it = sketch2.iterator(); int count = 0; while (it.next()) { Assert.assertEquals(it.getSummary().getValue(), 1.0); count++; } Assert.assertEquals(count, 3); }
/** * Filters elements on the provided {@link Sketch} * * @param sketchIn The sketch against which apply the {@link Predicate} * @return A new Sketch with some of the entries filtered out based on the {@link Predicate} */ public CompactSketch<T> filter(final Sketch<T> sketchIn) { if (sketchIn == null) { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } final QuickSelectSketch<T> sketch = new QuickSelectSketch<>(sketchIn.getRetainedEntries(), ResizeFactor.X1.lg(), null); final SketchIterator<T> it = sketchIn.iterator(); while (it.next()) { final T summary = it.getSummary(); if (predicate.test(summary)) { sketch.insert(it.getKey(), summary.copy()); } } sketch.setThetaLong(sketchIn.getThetaLong()); if (!sketchIn.isEmpty()) { sketch.setNotEmpty(); } return sketch.compact(); } }
@Test public void serializeDeserializeEstimationNoResizing() throws Exception { UpdatableSketch<Double, DoubleSummary> sketch1 = new UpdatableSketchBuilder<>( new DoubleSummaryFactory()).setResizeFactor(ResizeFactor.X1).build(); for (int j = 0; j < 10; j++) { for (int i = 0; i < 8192; i++) { sketch1.update(i, 1.0); } } sketch1.trim(); byte[] bytes = sketch1.toByteArray(); //for visual testing //TestUtil.writeBytesToFile(bytes, "UpdatableSketchWithDoubleSummary4K.bin"); Sketch<DoubleSummary> sketch2 = Sketches.heapifySketch(Memory.wrap(bytes), new DoubleSummaryDeserializer()); Assert.assertTrue(sketch2.isEstimationMode()); Assert.assertEquals(sketch2.getEstimate(), 8192, 8192 * 0.99); Assert.assertEquals(sketch1.getTheta(), sketch2.getTheta()); SketchIterator<DoubleSummary> it = sketch2.iterator(); int count = 0; while (it.next()) { Assert.assertEquals(it.getSummary().getValue(), 10.0); count++; } Assert.assertEquals(count, 4096); }
@Test public void serializeDeserializeEstimation() throws Exception { UpdatableSketch<Double, DoubleSummary> us = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build(); for (int i = 0; i < 8192; i++) us.update(i, 1.0); us.trim(); CompactSketch<DoubleSummary> sketch1 = us.compact(); byte[] bytes = sketch1.toByteArray(); // for visual testing //TestUtil.writeBytesToFile(bytes, "CompactSketchWithDoubleSummary4K.bin"); Sketch<DoubleSummary> sketch2 = Sketches.heapifySketch(Memory.wrap(bytes), new DoubleSummaryDeserializer()); Assert.assertFalse(sketch2.isEmpty()); Assert.assertTrue(sketch2.isEstimationMode()); Assert.assertEquals(sketch2.getEstimate(), sketch1.getEstimate()); Assert.assertEquals(sketch2.getThetaLong(), sketch1.getThetaLong()); SketchIterator<DoubleSummary> it = sketch2.iterator(); int count = 0; while (it.next()) { Assert.assertEquals(it.getSummary().getValue(), 1.0); count++; } Assert.assertEquals(count, 4096); }
@Override public List<BulletRecord> getRecords() { merge(); List<BulletRecord> result = new ArrayList<>(); SketchIterator<GroupDataSummary> iterator = this.result.iterator(); for (int count = 0; iterator.next() && count < maxSize; count++) { GroupData data = iterator.getSummary().getData(); result.add(data.getAsBulletRecord(provider)); } return result; }
/** * Updates the internal set by adding entries from the given sketch * @param sketchIn input sketch to add to the internal set */ public void update(final Sketch<S> sketchIn) { if (sketchIn == null || sketchIn.isEmpty()) { return; } if (sketchIn.theta_ < theta_) { theta_ = sketchIn.theta_; } final SketchIterator<S> it = sketchIn.iterator(); while (it.next()) { sketch_.merge(it.getKey(), it.getSummary(), summarySetOps_); } }
/** * Filters elements on the provided {@link Sketch} * * @param sketchIn The sketch against which apply the {@link Predicate} * @return A new Sketch with some of the entries filtered out based on the {@link Predicate} */ public CompactSketch<T> filter(final Sketch<T> sketchIn) { if (sketchIn == null) { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } final QuickSelectSketch<T> sketch = new QuickSelectSketch<>(sketchIn.getRetainedEntries(), ResizeFactor.X1.lg(), null); final SketchIterator<T> it = sketchIn.iterator(); while (it.next()) { final T summary = it.getSummary(); if (predicate.test(summary)) { sketch.insert(it.getKey(), summary.copy()); } } sketch.setThetaLong(sketchIn.getThetaLong()); if (!sketchIn.isEmpty()) { sketch.setNotEmpty(); } return sketch.compact(); } }