Search in sources :

Example 91 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class DoublesUnionImplTest method checkWrapInstance.

@Test
public void checkWrapInstance() {
    int k = 128;
    int n = 1000;
    UpdateDoublesSketch sketch = DoublesSketch.builder().setK(k).build();
    for (int i = 1; i <= n; i++) {
        sketch.update(i);
    }
    double skMedian = sketch.getQuantile(.5);
    Assert.assertEquals(skMedian, 500, 10);
    byte[] byteArr = sketch.toByteArray(false);
    WritableMemory mem = WritableMemory.wrap(byteArr);
    DoublesUnion union = DoublesUnionBuilder.wrap(mem);
    Assert.assertFalse(union.isEmpty());
    assertTrue(union.isDirect());
    DoublesSketch sketch2 = union.getResult();
    double uMedian = sketch2.getQuantile(0.5);
    Assert.assertEquals(skMedian, uMedian, 0.0);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 92 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class DoublesUtilTest method checkCopyToHeap.

@Test
public void checkCopyToHeap() {
    final int k = 128;
    final int n = 400;
    // HeapUpdateDoublesSketch
    final HeapUpdateDoublesSketch huds = (HeapUpdateDoublesSketch) buildAndLoadQS(k, n);
    final HeapUpdateDoublesSketch target1 = DoublesUtil.copyToHeap(huds);
    DoublesSketchTest.testSketchEquality(huds, target1);
    // DirectUpdateDoublesSketch
    final WritableMemory mem1 = WritableMemory.wrap(huds.toByteArray());
    final DirectUpdateDoublesSketch duds = (DirectUpdateDoublesSketch) UpdateDoublesSketch.wrap(mem1);
    final HeapUpdateDoublesSketch target2 = DoublesUtil.copyToHeap(duds);
    DoublesSketchTest.testSketchEquality(huds, duds);
    DoublesSketchTest.testSketchEquality(duds, target2);
    // HeapCompactDoublesSketch
    final CompactDoublesSketch hcds = huds.compact();
    final HeapUpdateDoublesSketch target3 = DoublesUtil.copyToHeap(hcds);
    DoublesSketchTest.testSketchEquality(huds, hcds);
    DoublesSketchTest.testSketchEquality(hcds, target3);
    // DirectCompactDoublesSketch
    final Memory mem2 = Memory.wrap(hcds.toByteArray());
    final DirectCompactDoublesSketch dcds = (DirectCompactDoublesSketch) DoublesSketch.wrap(mem2);
    final HeapUpdateDoublesSketch target4 = DoublesUtil.copyToHeap(dcds);
    DoublesSketchTest.testSketchEquality(huds, dcds);
    DoublesSketchTest.testSketchEquality(dcds, target4);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 93 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class SketchTest method checkIsSameResource.

@Test
public void checkIsSameResource() {
    int k = 16;
    WritableMemory mem = WritableMemory.wrap(new byte[(k * 16) + 24]);
    WritableMemory cmem = WritableMemory.wrap(new byte[8]);
    UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(mem);
    assertTrue(sketch.isSameResource(mem));
    DirectCompactOrderedSketch dcos = (DirectCompactOrderedSketch) sketch.compact(true, cmem);
    assertTrue(dcos.isSameResource(cmem));
    //never create 2 sketches with the same memory, so don't do as I do :)
    DirectCompactSketch dcs = (DirectCompactSketch) sketch.compact(false, cmem);
    assertTrue(dcs.isSameResource(cmem));
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 94 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class SketchTest method checkHeapifyNotCompactExcep.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyNotCompactExcep() {
    int k = 512;
    UpdateSketch sketch1 = UpdateSketch.builder().setFamily(QUICKSELECT).setNominalEntries(k).build();
    int bytes = Sketch.getMaxCompactSketchBytes(0);
    byte[] byteArray = new byte[bytes];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    sketch1.compact(false, mem);
    //corrupt:
    mem.clearBits(FLAGS_BYTE, (byte) COMPACT_FLAG_MASK);
    Sketch.heapify(mem);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 95 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class SketchesTest method checkSetOpMethods.

@Test
public void checkSetOpMethods() {
    int k = 1024;
    Memory mem1 = getCompactSketch(k, 0, k);
    Memory mem2 = getCompactSketch(k, k / 2, 3 * k / 2);
    SetOperationBuilder bldr = setOperationBuilder();
    Union union = bldr.setNominalEntries(2 * k).buildUnion();
    union.update(mem1);
    CompactSketch cSk = union.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), k);
    union.update(mem2);
    cSk = union.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    byte[] ubytes = union.toByteArray();
    WritableMemory uMem = WritableMemory.wrap(ubytes);
    Union union2 = (Union) heapifySetOperation(uMem);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    union2 = (Union) heapifySetOperation(uMem, Util.DEFAULT_UPDATE_SEED);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    union2 = (Union) wrapSetOperation(uMem);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    union2 = (Union) wrapSetOperation(uMem, Util.DEFAULT_UPDATE_SEED);
    cSk = union2.getResult(true, null);
    assertEquals((int) cSk.getEstimate(), 3 * k / 2);
    int serVer = getSerializationVersion(uMem);
    assertEquals(serVer, 3);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

WritableMemory (com.yahoo.memory.WritableMemory)264 Test (org.testng.annotations.Test)210 Memory (com.yahoo.memory.Memory)34 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)15 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)6 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)5 MemoryRequestServer (com.yahoo.memory.MemoryRequestServer)2 ArrayOfDoublesSerDe (com.yahoo.sketches.ArrayOfDoublesSerDe)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 PreambleUtil.extractTgtHllType (com.yahoo.sketches.hll.PreambleUtil.extractTgtHllType)2 ResizeFactor (com.yahoo.sketches.ResizeFactor)1 PreambleUtil.extractCurMode (com.yahoo.sketches.hll.PreambleUtil.extractCurMode)1 PreambleUtil.insertTgtHllType (com.yahoo.sketches.hll.PreambleUtil.insertTgtHllType)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1