Search in sources :

Example 81 with WritableMemory

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());
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 82 with WritableMemory

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);
}
Also used : Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 83 with WritableMemory

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);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 84 with WritableMemory

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());
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 85 with WritableMemory

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);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Test(org.testng.annotations.Test)

Aggregations

WritableMemory (org.apache.datasketches.memory.WritableMemory)429 Test (org.testng.annotations.Test)308 Memory (org.apache.datasketches.memory.Memory)55 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)51 WritableHandle (org.apache.datasketches.memory.WritableHandle)38 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)25 ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)11 ByteBuffer (java.nio.ByteBuffer)8 Test (org.junit.Test)8 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)7 ArrayOfStringsSerDe (org.apache.datasketches.ArrayOfStringsSerDe)6 SketchesStateException (org.apache.datasketches.SketchesStateException)5 SharedLocal (org.apache.datasketches.theta.ConcurrentHeapQuickSelectSketchTest.SharedLocal)5 AggregatorAdapters (org.apache.druid.query.aggregation.AggregatorAdapters)5 Union (org.apache.datasketches.hll.Union)4 ResizeFactor (org.apache.datasketches.ResizeFactor)3 HllSketch (org.apache.datasketches.hll.HllSketch)3 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2