use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkAlphaToCompactEmptyForms.
@Test
public void checkAlphaToCompactEmptyForms() {
int k = 512;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setNominalEntries(k).build();
// empty
usk.toString(false, true, 0, false);
boolean estimating = false;
assertTrue(usk instanceof HeapAlphaSketch);
double uskEst = usk.getEstimate();
double uskLB = usk.getLowerBound(2);
double uskUB = usk.getUpperBound(2);
assertEquals(usk.isEstimationMode(), estimating);
int bytes = usk.getCompactBytes();
// compact, empty and theta = 1.0
assertEquals(bytes, 8);
byte[] memArr2 = new byte[bytes];
WritableMemory mem2 = WritableMemory.writableWrap(memArr2);
CompactSketch csk2 = usk.compact(false, mem2);
assertEquals(csk2.getEstimate(), uskEst);
assertEquals(csk2.getLowerBound(2), uskLB);
assertEquals(csk2.getUpperBound(2), uskUB);
assertEquals(csk2.isEmpty(), true);
assertEquals(csk2.isEstimationMode(), estimating);
assertTrue(csk2.isOrdered());
CompactSketch csk3 = usk.compact(true, mem2);
csk3.toString(false, true, 0, false);
csk3.toString();
assertEquals(csk3.getEstimate(), uskEst);
assertEquals(csk3.getLowerBound(2), uskLB);
assertEquals(csk3.getUpperBound(2), uskUB);
assertEquals(csk3.isEmpty(), true);
assertEquals(csk3.isEstimationMode(), estimating);
assertTrue(csk3.isOrdered());
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class UnionImplTest method checkFastWrap.
@Test
public void checkFastWrap() {
final int k = 16;
final long seed = DEFAULT_UPDATE_SEED;
final int unionSize = Sketches.getMaxUnionBytes(k);
final WritableMemory srcMem = WritableMemory.writableWrap(new byte[unionSize]);
final Union union = Sketches.setOperationBuilder().setNominalEntries(k).buildUnion(srcMem);
// exact
for (int i = 0; i < k; i++) {
union.update(i);
}
assertEquals(union.getResult().getEstimate(), k, 0.0);
final Union union2 = UnionImpl.fastWrap(srcMem, seed);
assertEquals(union2.getResult().getEstimate(), k, 0.0);
final Memory srcMemR = srcMem;
final Union union3 = UnionImpl.fastWrap(srcMemR, seed);
assertEquals(union3.getResult().getEstimate(), k, 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class UnionImplTest method checkUpdateWithSketch.
@Test
public void checkUpdateWithSketch() {
final int k = 16;
final WritableMemory mem = WritableMemory.writableWrap(new byte[k * 8 + 24]);
final WritableMemory mem2 = WritableMemory.writableWrap(new byte[k * 8 + 24]);
final UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build();
for (int i = 0; i < k; i++) {
sketch.update(i);
}
final CompactSketch sketchInDirectOrd = sketch.compact(true, mem);
final CompactSketch sketchInDirectUnord = sketch.compact(false, mem2);
final CompactSketch sketchInHeap = sketch.compact(true, null);
final Union union = Sketches.setOperationBuilder().setNominalEntries(k).buildUnion();
union.union(sketchInDirectOrd);
union.union(sketchInHeap);
union.union(sketchInDirectUnord);
assertEquals(union.getResult().getEstimate(), k, 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class UnionImplTest method checkCompactFlagCorruption.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkCompactFlagCorruption() {
final int k = 1 << 12;
final int bytes = Sketch.getMaxUpdateSketchBytes(k);
final WritableMemory wmem1 = WritableMemory.allocate(bytes);
final UpdateSketch sk = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem1);
for (int i = 0; i < k; i++) {
sk.update(i);
}
// corrupt the wmem1 to be a compact sketch
sk.compact(true, wmem1);
final Union union = SetOperation.builder().buildUnion();
// update the union with the UpdateSketch object
union.union(sk);
final CompactSketch csk1 = union.getResult();
println("" + csk1.getEstimate());
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class UnionImplTest method checkUnionCompactOrderedSource.
@Test
public void checkUnionCompactOrderedSource() {
final int k = 1 << 12;
final UpdateSketch sk = Sketches.updateSketchBuilder().build();
for (int i = 0; i < k; i++) {
sk.update(i);
}
final double est1 = sk.getEstimate();
final int bytes = Sketches.getMaxCompactSketchBytes(sk.getRetainedEntries(true));
try (WritableHandle h = WritableMemory.allocateDirect(bytes)) {
final WritableMemory wmem = h.getWritable();
// ordered, direct
final CompactSketch csk = sk.compact(true, wmem);
final Union union = Sketches.setOperationBuilder().buildUnion();
union.union(csk);
final double est2 = union.getResult().getEstimate();
assertEquals(est2, est1);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations