@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("### ").append(this.getClass().getSimpleName()).append(" SUMMARY: ").append(LS); sb.append(" Estimate : ").append(getEstimate()).append(LS); sb.append(" Upper Bound, 95% conf : ").append(getUpperBound(2)).append(LS); sb.append(" Lower Bound, 95% conf : ").append(getLowerBound(2)).append(LS); sb.append(" Theta (double) : ").append(this.getTheta()).append(LS); sb.append(" Theta (long) : ").append(this.getThetaLong()).append(LS); sb.append(" EstMode? : ").append(isEstimationMode()).append(LS); sb.append(" Empty? : ").append(isEmpty()).append(LS); sb.append(" Retained Entries : ").append(this.getRetainedEntries()).append(LS); if (this instanceof UpdatableSketch) { @SuppressWarnings("rawtypes") final UpdatableSketch updatable = (UpdatableSketch) this; sb.append(" Nominal Entries (k) : ").append(updatable.getNominalEntries()).append(LS); sb.append(" Current Capacity : ").append(updatable.getCurrentCapacity()).append(LS); sb.append(" Resize Factor : ").append(updatable.getResizeFactor().getValue()).append(LS); sb.append(" Sampling Probability (p): ").append(updatable.getSamplingProbability()).append(LS); } sb.append("### END SKETCH SUMMARY").append(LS); return sb.toString(); }
public void update(final Sketch<S> a, final Sketch<S> b) { if (a != null) { isEmpty_ = a.isEmpty(); } //stays this way even if we end up with no result entries final long thetaA = a == null ? Long.MAX_VALUE : a.getThetaLong(); final long thetaB = b == null ? Long.MAX_VALUE : b.getThetaLong(); theta_ = Math.min(thetaA, thetaB); if (a == null || a.getRetainedEntries() == 0) { return; }
@Test public void emptySketch() { Sketch<DoubleSummary> sketch = Sketches.createEmptySketch(); Filter<DoubleSummary> filter = new Filter<>(o -> true); Sketch<DoubleSummary> filteredSketch = filter.filter(sketch); Assert.assertEquals(filteredSketch.getEstimate(), 0.0); Assert.assertEquals(filteredSketch.getThetaLong(), sketch.getThetaLong()); Assert.assertTrue(filteredSketch.isEmpty()); Assert.assertEquals(filteredSketch.getLowerBound(1), 0.0); Assert.assertEquals(filteredSketch.getUpperBound(1), 0.0); }
@Test public void nullSketch() { Filter<DoubleSummary> filter = new Filter<>(o -> true); Sketch<DoubleSummary> filteredSketch = filter.filter(null); Assert.assertEquals(filteredSketch.getEstimate(), 0.0); Assert.assertEquals(filteredSketch.getThetaLong(), Long.MAX_VALUE); Assert.assertTrue(filteredSketch.isEmpty()); Assert.assertEquals(filteredSketch.getLowerBound(1), 0.0); Assert.assertEquals(filteredSketch.getUpperBound(1), 0.0); }
@Test public void filledSketchShouldBehaveTheSame() { UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build(); fillSketch(sketch, numberOfElements, 0.0); Filter<DoubleSummary> filter = new Filter<>(o -> true); Sketch<DoubleSummary> filteredSketch = filter.filter(sketch); Assert.assertEquals(filteredSketch.getEstimate(), sketch.getEstimate()); Assert.assertEquals(filteredSketch.getThetaLong(), sketch.getThetaLong()); Assert.assertFalse(filteredSketch.isEmpty()); Assert.assertEquals(filteredSketch.getLowerBound(1), sketch.getLowerBound(1)); Assert.assertEquals(filteredSketch.getUpperBound(1), sketch.getUpperBound(1)); }
@Test public void nonEmptySketchWithNoEntries() { UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>( new DoubleSummaryFactory()).setSamplingProbability(0.0001f).build(); sketch.update(0, 0.0); Assert.assertFalse(sketch.isEmpty()); Assert.assertEquals(sketch.getRetainedEntries(), 0); Filter<DoubleSummary> filter = new Filter<>(o -> true); Sketch<DoubleSummary> filteredSketch = filter.filter(sketch); Assert.assertFalse(filteredSketch.isEmpty()); Assert.assertEquals(filteredSketch.getEstimate(), sketch.getEstimate()); Assert.assertEquals(filteredSketch.getThetaLong(), sketch.getThetaLong()); Assert.assertEquals(filteredSketch.getLowerBound(1), sketch.getLowerBound(1)); Assert.assertEquals(filteredSketch.getUpperBound(1), sketch.getUpperBound(1)); }
@Test public void filteringInEstimationMode() { UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build(); int n = 10000; fillSketch(sketch, n, 0.0); fillSketch(sketch, 2 * n, 1.0); Filter<DoubleSummary> filter = new Filter<>(o -> o.getValue() < 0.5); Sketch<DoubleSummary> filteredSketch = filter.filter(sketch); Assert.assertEquals(filteredSketch.getEstimate(), n, n * 0.05); Assert.assertEquals(filteredSketch.getThetaLong(), sketch.getThetaLong()); Assert.assertFalse(filteredSketch.isEmpty()); Assert.assertTrue(filteredSketch.getLowerBound(1) <= filteredSketch.getEstimate()); Assert.assertTrue(filteredSketch.getUpperBound(1) >= filteredSketch.getEstimate()); }
@Test public void filledSketchShouldFilterOutElements() { UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()).build(); fillSketch(sketch, numberOfElements, 0.0); fillSketch(sketch, 2 * numberOfElements, 1.0); Filter<DoubleSummary> filter = new Filter<>(o -> o.getValue() < 0.5); Sketch<DoubleSummary> filteredSketch = filter.filter(sketch); Assert.assertEquals(filteredSketch.getEstimate(), (double) numberOfElements); Assert.assertEquals(filteredSketch.getThetaLong(), sketch.getThetaLong()); Assert.assertFalse(filteredSketch.isEmpty()); Assert.assertTrue(filteredSketch.getLowerBound(1) <= filteredSketch.getEstimate()); Assert.assertTrue(filteredSketch.getUpperBound(1) >= filteredSketch.getEstimate()); }
@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 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); }
return; theta_ = min(theta_, sketchIn.getThetaLong()); isEmpty_ |= sketchIn.isEmpty(); if (isEmpty_ || (sketchIn.getRetainedEntries() == 0)) {
@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("### ").append(this.getClass().getSimpleName()).append(" SUMMARY: ").append(LS); sb.append(" Estimate : ").append(getEstimate()).append(LS); sb.append(" Upper Bound, 95% conf : ").append(getUpperBound(2)).append(LS); sb.append(" Lower Bound, 95% conf : ").append(getLowerBound(2)).append(LS); sb.append(" Theta (double) : ").append(this.getTheta()).append(LS); sb.append(" Theta (long) : ").append(this.getThetaLong()).append(LS); sb.append(" EstMode? : ").append(isEstimationMode()).append(LS); sb.append(" Empty? : ").append(isEmpty()).append(LS); sb.append(" Retained Entries : ").append(this.getRetainedEntries()).append(LS); if (this instanceof UpdatableSketch) { @SuppressWarnings("rawtypes") final UpdatableSketch updatable = (UpdatableSketch) this; sb.append(" Nominal Entries (k) : ").append(updatable.getNominalEntries()).append(LS); sb.append(" Current Capacity : ").append(updatable.getCurrentCapacity()).append(LS); sb.append(" Resize Factor : ").append(updatable.getResizeFactor().getValue()).append(LS); sb.append(" Sampling Probability (p): ").append(updatable.getSamplingProbability()).append(LS); } sb.append("### END SKETCH SUMMARY").append(LS); return sb.toString(); }
public void update(final Sketch<S> a, final Sketch<S> b) { if (a != null) { isEmpty_ = a.isEmpty(); } //stays this way even if we end up with no result entries final long thetaA = a == null ? Long.MAX_VALUE : a.getThetaLong(); final long thetaB = b == null ? Long.MAX_VALUE : b.getThetaLong(); theta_ = Math.min(thetaA, thetaB); if (a == null || a.getRetainedEntries() == 0) { return; }
/** * 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(); } }
return; theta_ = min(theta_, sketchIn.getThetaLong()); isEmpty_ |= sketchIn.isEmpty(); if (isEmpty_ || (sketchIn.getRetainedEntries() == 0)) {