Search in sources :

Example 41 with WritableHandle

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

the class DoublesSketchTest method directSketchShouldMoveOntoHeapEventually.

@Test
public void directSketchShouldMoveOntoHeapEventually() {
    try (WritableHandle wdh = WritableMemory.allocateDirect(1000, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        WritableMemory mem = wdh.getWritable();
        UpdateDoublesSketch sketch = DoublesSketch.builder().build(mem);
        Assert.assertTrue(sketch.isSameResource(mem));
        for (int i = 0; i < 1000; i++) {
            sketch.update(i);
        }
        Assert.assertFalse(sketch.isSameResource(mem));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Example 42 with WritableHandle

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

the class DoublesSketchTest method directSketchShouldMoveOntoHeapEventually2.

@Test
public void directSketchShouldMoveOntoHeapEventually2() {
    int i = 0;
    try (WritableHandle wdh = WritableMemory.allocateDirect(50, ByteOrder.LITTLE_ENDIAN, new DefaultMemoryRequestServer())) {
        WritableMemory mem = wdh.getWritable();
        UpdateDoublesSketch sketch = DoublesSketch.builder().build(mem);
        Assert.assertTrue(sketch.isSameResource(mem));
        for (; i < 1000; i++) {
            if (sketch.isSameResource(mem)) {
                sketch.update(i);
            } else {
                // println("MOVED OUT at i = " + i);
                break;
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Example 43 with WritableHandle

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

the class DirectAuxHashMapTest method checkGrow.

@Test
public void checkGrow() {
    int lgConfigK = 4;
    TgtHllType tgtHllType = TgtHllType.HLL_4;
    // put lgConfigK == 4 into HLL mode
    int n = 8;
    int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
    HllSketch hllSketch;
    try (WritableHandle handle = WritableMemory.allocateDirect(bytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        WritableMemory wmem = handle.getWritable();
        hllSketch = new HllSketch(lgConfigK, tgtHllType, wmem);
        for (int i = 0; i < n; i++) {
            hllSketch.update(i);
        }
        // mock extreme values
        hllSketch.couponUpdate(HllUtil.pair(7, 15));
        hllSketch.couponUpdate(HllUtil.pair(8, 15));
        hllSketch.couponUpdate(HllUtil.pair(9, 15));
        // println(hllSketch.toString(true, true, true, true));
        DirectHllArray dha = (DirectHllArray) hllSketch.hllSketchImpl;
        assertEquals(dha.getAuxHashMap().getLgAuxArrInts(), 2);
        assertTrue(hllSketch.isMemory());
        assertTrue(hllSketch.isOffHeap());
        assertTrue(hllSketch.isSameResource(wmem));
        // Check heapify
        byte[] byteArray = hllSketch.toCompactByteArray();
        HllSketch hllSketch2 = HllSketch.heapify(byteArray);
        HllArray ha = (HllArray) hllSketch2.hllSketchImpl;
        assertEquals(ha.getAuxHashMap().getLgAuxArrInts(), 2);
        assertEquals(ha.getAuxHashMap().getAuxCount(), 3);
        // Check wrap
        byteArray = hllSketch.toUpdatableByteArray();
        WritableMemory wmem2 = WritableMemory.writableWrap(byteArray);
        hllSketch2 = HllSketch.writableWrap(wmem2);
        // println(hllSketch2.toString(true, true, true, true));
        DirectHllArray dha2 = (DirectHllArray) hllSketch2.hllSketchImpl;
        assertEquals(dha2.getAuxHashMap().getLgAuxArrInts(), 2);
        assertEquals(dha2.getAuxHashMap().getAuxCount(), 3);
        // Check grow to on-heap
        // puts it over the edge, must grow
        hllSketch.couponUpdate(HllUtil.pair(10, 15));
        // println(hllSketch.toString(true, true, true, true));
        dha = (DirectHllArray) hllSketch.hllSketchImpl;
        assertEquals(dha.getAuxHashMap().getLgAuxArrInts(), 3);
        assertEquals(dha.getAuxHashMap().getAuxCount(), 4);
        assertTrue(hllSketch.isMemory());
        assertFalse(hllSketch.isOffHeap());
        assertFalse(hllSketch.isSameResource(wmem));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) SketchesStateException(org.apache.datasketches.SketchesStateException) Test(org.testng.annotations.Test)

Example 44 with WritableHandle

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

the class HeapifyWrapSerVer1and2Test method checkWrapSketchAssumedDifferentSeed.

@Test
public void checkWrapSketchAssumedDifferentSeed() {
    final int k = 64;
    final long seed = 128L;
    final short seedHash = Util.computeSeedHash(seed);
    UpdateSketch sv3usk = UpdateSketch.builder().setNominalEntries(k).setSeed(seed).build();
    for (int i = 0; i < k; i++) {
        sv3usk.update(i);
    }
    CompactSketch sv3cskResult;
    WritableHandle wh;
    CompactSketch sv3csk = sv3usk.compact();
    // SV3 test
    wh = putOffHeap(Memory.wrap(sv3csk.toByteArray()));
    sv3cskResult = (CompactSketch) Sketches.wrapSketch(wh.getWritable());
    assertEquals(sv3cskResult.getEstimate(), sv3usk.getEstimate());
    assertEquals(sv3cskResult.getSeedHash(), seedHash);
    assertTrue(sv3cskResult.isDirect());
    try {
        wh.close();
    } catch (Exception e) {
    }
    // SV2 test
    wh = putOffHeap(BackwardConversions.convertSerVer3toSerVer2(sv3csk, seed));
    sv3cskResult = (CompactSketch) Sketches.wrapSketch(wh.getWritable());
    assertEquals(sv3cskResult.getEstimate(), sv3usk.getEstimate());
    assertEquals(sv3cskResult.getSeedHash(), seedHash);
    assertFalse(sv3cskResult.isDirect());
    try {
        wh.close();
    } catch (Exception e) {
    }
    // SV1 test
    wh = putOffHeap(BackwardConversions.convertSerVer3toSerVer1(sv3csk));
    sv3cskResult = (CompactSketch) Sketches.wrapSketch(wh.getWritable());
    assertEquals(sv3cskResult.getEstimate(), sv3usk.getEstimate());
    assertEquals(sv3cskResult.getSeedHash(), defaultSeedHash);
    assertFalse(sv3cskResult.isDirect());
    try {
        wh.close();
    } catch (Exception e) {
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) Test(org.testng.annotations.Test)

Example 45 with WritableHandle

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

the class HeapifyWrapSerVer1and2Test method checkWrapCompactSketchAssumedDifferentSeed.

@Test
public void checkWrapCompactSketchAssumedDifferentSeed() {
    final int k = 64;
    final long seed = 128L;
    final short seedHash = Util.computeSeedHash(seed);
    UpdateSketch sv3usk = UpdateSketch.builder().setNominalEntries(k).setSeed(seed).build();
    for (int i = 0; i < k; i++) {
        sv3usk.update(i);
    }
    CompactSketch sv3cskResult;
    WritableHandle wh;
    CompactSketch sv3csk = sv3usk.compact();
    // SV3 test
    wh = putOffHeap(Memory.wrap(sv3csk.toByteArray()));
    sv3cskResult = Sketches.wrapCompactSketch(wh.getWritable());
    assertEquals(sv3cskResult.getEstimate(), sv3usk.getEstimate());
    assertEquals(sv3cskResult.getSeedHash(), seedHash);
    assertTrue(sv3cskResult.isDirect());
    try {
        wh.close();
    } catch (Exception e) {
    }
    // SV2 test
    wh = putOffHeap(BackwardConversions.convertSerVer3toSerVer2(sv3csk, seed));
    sv3cskResult = Sketches.wrapCompactSketch(wh.getWritable());
    assertEquals(sv3cskResult.getEstimate(), sv3usk.getEstimate());
    assertEquals(sv3cskResult.getSeedHash(), seedHash);
    assertFalse(sv3cskResult.isDirect());
    try {
        wh.close();
    } catch (Exception e) {
    }
    // SV1 test
    wh = putOffHeap(BackwardConversions.convertSerVer3toSerVer1(sv3csk));
    sv3cskResult = Sketches.wrapCompactSketch(wh.getWritable());
    assertEquals(sv3cskResult.getEstimate(), sv3usk.getEstimate());
    assertEquals(sv3cskResult.getSeedHash(), defaultSeedHash);
    assertFalse(sv3cskResult.isDirect());
    try {
        wh.close();
    } catch (Exception e) {
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) Test(org.testng.annotations.Test)

Aggregations

WritableHandle (org.apache.datasketches.memory.WritableHandle)47 Test (org.testng.annotations.Test)44 WritableMemory (org.apache.datasketches.memory.WritableMemory)38 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)27 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)24 DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)7 Memory (org.apache.datasketches.memory.Memory)6 HashSet (java.util.HashSet)1 SketchesStateException (org.apache.datasketches.SketchesStateException)1