@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(); }
/** * 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_); } }
/** * Estimates the cardinality of the set (number of unique values presented to the sketch) * @return best estimate of the number of unique values */ public double getEstimate() { if (!isEstimationMode()) { return getRetainedEntries(); } return getRetainedEntries() / getTheta(); }
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; } if (b == null || b.getRetainedEntries() == 0) { getNoMatchSetFromSketch(a); } else { final int noMatchSize = a.getRetainedEntries(); keys_ = new long[noMatchSize]; summaries_ = (S[]) Array.newInstance(a.summaries_.getClass().getComponentType(), noMatchSize);
@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); }
/** * 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); }
@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 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 serializeDeserializeSampling() throws Exception { int sketchSize = 16384; int numberOfUniques = sketchSize; UpdatableSketch<Double, DoubleSummary> sketch1 = new UpdatableSketchBuilder<>(new DoubleSummaryFactory()) .setNominalEntries(sketchSize).setSamplingProbability(0.5f).build(); for (int i = 0; i < numberOfUniques; i++) { sketch1.update(i, 1.0); } Sketch<DoubleSummary> sketch2 = Sketches.heapifySketch( Memory.wrap(sketch1.toByteArray()), new DoubleSummaryDeserializer()); Assert.assertTrue(sketch2.isEstimationMode()); Assert.assertEquals(sketch2.getEstimate() / numberOfUniques, 1.0, 0.01); Assert.assertEquals(sketch2.getRetainedEntries() / (double) numberOfUniques, 0.5, 0.01); Assert.assertEquals(sketch1.getTheta(), sketch2.getTheta()); }
private long[] convertToHashTable(final Sketch<S> sketch) { final int size = Math.max( ceilingPowerOf2((int) Math.ceil(sketch.getRetainedEntries() / REBUILD_THRESHOLD)), 1 << MIN_LG_NOM_LONGS ); final long[] hashTable = new long[size]; HashOperations.hashArrayInsert( sketch.keys_, hashTable, Integer.numberOfTrailingZeros(size), theta_); return hashTable; }
/** * Returns the estimate of the uniques in the Sketch. Only applicable after {@link #merge()}. * * @return A Double representing the number of unique values in the Sketch. */ private Double getUniquesEstimate() { return result.getEstimate(); } }
@Override protected Double getLowerBound(int standardDeviation) { return result.getLowerBound(standardDeviation); }
@Override protected Boolean isEstimationMode() { return result.isEstimationMode(); }
@Override protected Double getUpperBound(int standardDeviation) { return result.getUpperBound(standardDeviation); }
@Override protected Double getTheta() { return result.getTheta(); }
@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; }
/** * Returns true if the sketch is Estimation Mode (as opposed to Exact Mode). * This is true if theta < 1.0 AND isEmpty() is false. * @return true if the sketch is in estimation mode. */ public boolean isEstimationMode() { return ((theta_ < Long.MAX_VALUE) && !isEmpty()); }
@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); }
/** * Gets the approximate upper error bound given the specified number of Standard Deviations. * This will return getEstimate() if isEmpty() is true. * * @param numStdDev * <a href="{@docRoot}/resources/dictionary.html#numStdDev">See Number of Standard Deviations</a> * @return the upper bound. */ public double getUpperBound(final int numStdDev) { if (!isEstimationMode()) { return getRetainedEntries(); } return BinomialBoundsN.getUpperBound(getRetainedEntries(), getTheta(), numStdDev, isEmpty_); }