Search in sources :

Example 46 with WritableMemory

use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.

the class DoublesUnionImplTest method checkUpdateMemoryDirect.

@Test
public void checkUpdateMemoryDirect() {
    final DoublesSketch qs1 = buildAndLoadDQS(256, 1000);
    final int bytes = qs1.getCompactStorageBytes();
    final WritableMemory dstMem = WritableMemory.writableWrap(new byte[bytes]);
    qs1.putMemory(dstMem);
    final Memory srcMem = dstMem;
    // virgin
    final DoublesUnion union = DoublesUnion.builder().build();
    union.update(srcMem);
    for (int i = 1000; i < 2000; i++) {
        union.update(i);
    }
    final DoublesSketch qs2 = union.getResult();
    assertEquals(qs2.getMaxValue(), 1999, 0.0);
    final String s = union.toString();
    // enable printing to see
    println(s);
    // sets to null
    union.reset();
}
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 47 with WritableMemory

use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.

the class DoublesUnionImplTest method checkResultViaMemory.

@Test
public void checkResultViaMemory() {
    // empty gadget
    final DoublesUnion union = DoublesUnion.builder().build();
    // memory too small
    WritableMemory mem = WritableMemory.allocate(1);
    try {
        union.getResult(mem);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // sufficient memory
    mem = WritableMemory.allocate(8);
    DoublesSketch result = union.getResult(mem);
    assertTrue(result.isEmpty());
    final int k = 128;
    final int n = 1392;
    mem = WritableMemory.allocate(DoublesSketch.getUpdatableStorageBytes(k, n));
    final DoublesSketch qs = buildAndLoadQS(k, n);
    union.update(qs);
    result = union.getResult(mem);
    DoublesSketchTest.testSketchEquality(result, qs);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 48 with WritableMemory

use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.

the class PreambleUtilTest method checkCorruptMemoryInput.

@SuppressWarnings("unused")
@Test
public void checkCorruptMemoryInput() {
    HllSketch sk = new HllSketch(12);
    byte[] memObj = sk.toCompactByteArray();
    WritableMemory wmem = WritableMemory.writableWrap(memObj);
    long memAdd = wmem.getCumulativeOffset(0);
    HllSketch bad;
    // checkFamily
    try {
        // corrupt, should be 7
        wmem.putByte(FAMILY_BYTE, (byte) 0);
        bad = HllSketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    /* OK */
    }
    // corrected
    insertFamilyId(wmem);
    // check SerVer
    try {
        // corrupt, should be 1
        wmem.putByte(SER_VER_BYTE, (byte) 0);
        bad = HllSketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    /* OK */
    }
    // corrected
    insertSerVer(wmem);
    // check bad PreInts
    try {
        // corrupt, should be 2
        insertPreInts(wmem, 0);
        bad = HllSketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    /* OK */
    }
    // corrected
    insertPreInts(wmem, 2);
    // check wrong PreInts and LIST
    try {
        // corrupt, should be 2
        insertPreInts(wmem, 3);
        bad = HllSketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    /* OK */
    }
    // corrected
    insertPreInts(wmem, 2);
    // move to Set mode
    for (int i = 1; i <= 15; i++) {
        sk.update(i);
    }
    memObj = sk.toCompactByteArray();
    wmem = WritableMemory.writableWrap(memObj);
    memAdd = wmem.getCumulativeOffset(0);
    // check wrong PreInts and SET
    try {
        // corrupt, should be 3
        insertPreInts(wmem, 2);
        bad = HllSketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    /* OK */
    }
    // corrected
    insertPreInts(wmem, 3);
    // move to HLL mode
    for (int i = 15; i <= 1000; i++) {
        sk.update(i);
    }
    memObj = sk.toCompactByteArray();
    wmem = WritableMemory.writableWrap(memObj);
    memAdd = wmem.getCumulativeOffset(0);
    // check wrong PreInts and HLL
    try {
        // corrupt, should be 10
        insertPreInts(wmem, 2);
        bad = HllSketch.heapify(wmem);
        fail();
    } catch (SketchesArgumentException e) {
    /* OK */
    }
    // corrected
    insertPreInts(wmem, 10);
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 49 with WritableMemory

use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.

the class PreambleUtilTest method checkExtractFlags.

@SuppressWarnings("unused")
@Test
public void checkExtractFlags() {
    int bytes = HllSketch.getMaxUpdatableSerializationBytes(4, TgtHllType.HLL_4);
    WritableMemory wmem = WritableMemory.allocate(bytes);
    Object memObj = wmem.getArray();
    long memAdd = wmem.getCumulativeOffset(0L);
    HllSketch sk = new HllSketch(4, TgtHllType.HLL_4, wmem);
    int flags = extractFlags(wmem);
    assertEquals(flags, EMPTY_FLAG_MASK);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 50 with WritableMemory

use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.

the class ToFromByteArrayTest method toFrom1.

private static void toFrom1(int lgConfigK, TgtHllType tgtHllType, int n) {
    HllSketch src = new HllSketch(lgConfigK, tgtHllType);
    for (int i = 0; i < n; i++) {
        src.update(i);
    }
    // println("n: " + n + ", lgK: " + lgK + ", type: " + tgtHllType);
    // printSketch(src, "SRC");
    // compact
    byte[] byteArr1 = src.toCompactByteArray();
    // using byte[] interface
    HllSketch dst = HllSketch.heapify(byteArr1);
    // printSketch(dst, "DST");
    assertEquals(dst.getEstimate(), src.getEstimate(), 0.0);
    // updatable
    byte[] byteArr2 = src.toUpdatableByteArray();
    Memory mem2 = Memory.wrap(byteArr2);
    // using Memory interface
    HllSketch dst2 = HllSketch.heapify(mem2);
    // printSketch(dst, "DST");
    assertEquals(dst2.getEstimate(), src.getEstimate(), 0.0);
    WritableMemory mem3 = WritableMemory.writableWrap(byteArr2);
    // using WritableMemory interface
    HllSketch dst3 = HllSketch.heapify(mem3);
    // printSketch(dst, "DST");
    assertEquals(dst3.getEstimate(), src.getEstimate(), 0.0);
}
Also used : Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

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