Search in sources :

Example 41 with WritableMemory

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

the class MiscFloatsTest method checkHeapifyExceptions3.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions3() {
    KllFloatsSketch sk = new KllFloatsSketch();
    sk.update(1.0f);
    sk.update(2.0f);
    WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
    // corrupt preamble ints, should be 5
    wmem.putByte(0, (byte) 1);
    KllFloatsSketch.heapify(wmem);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 42 with WritableMemory

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

the class MiscFloatsTest method checkHeapifyExceptions2.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions2() {
    KllFloatsSketch sk = new KllFloatsSketch();
    WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
    // corrupt preamble ints, should be 2
    wmem.putByte(0, (byte) 1);
    KllFloatsSketch.heapify(wmem);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 43 with WritableMemory

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

the class DirectQuantilesMemoryRequestTest method checkLimitedMemoryScenarios.

@Test
public void checkLimitedMemoryScenarios() {
    // Requesting application
    final int k = 128;
    final int u = 40 * k;
    // just the BB
    final int initBytes = ((2 * k) + 4) << 3;
    // This part would actually be part of the Memory owning implemention so it is faked here
    try (WritableHandle wdh = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        final WritableMemory wmem = wdh.getWritable();
        println("Initial mem size: " + wmem.getCapacity());
        // ########## Receiving Application
        // The receiving application has been given wmem to use for a sketch,
        // but alas, it is not ultimately large enough.
        final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem);
        assertTrue(usk1.isEmpty());
        // Load the sketch
        for (int i = 0; i < u; i++) {
            // The sketch uses The MemoryRequest, acquired from wmem, to acquire more memory as
            // needed, and requests via the MemoryRequest to free the old allocations.
            usk1.update(i);
        }
        final double result = usk1.getQuantile(0.5);
        println("Result: " + result);
        // Success
        assertEquals(result, u / 2.0, 0.05 * u);
        // ########## Owning Implementation
        // The actual Memory has been re-allocated several times,
        // so the above wmem reference is invalid.
        println("\nFinal mem size: " + wmem.getCapacity());
    } catch (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 44 with WritableMemory

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

the class DoublesSketchBuilderTest method checkBuilder.

@Test
public void checkBuilder() {
    // default is 128
    int k = 256;
    DoublesSketchBuilder bldr = DoublesSketch.builder();
    bldr.setK(k);
    // confirms new k
    assertEquals(bldr.getK(), k);
    println(bldr.toString());
    int bytes = DoublesSketch.getUpdatableStorageBytes(k, 0);
    byte[] byteArr = new byte[bytes];
    WritableMemory mem = WritableMemory.writableWrap(byteArr);
    DoublesSketch ds = bldr.build(mem);
    assertTrue(ds.isDirect());
    println(bldr.toString());
    bldr = DoublesSketch.builder();
    assertEquals(bldr.getK(), PreambleUtil.DEFAULT_K);
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 45 with WritableMemory

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

the class DoublesSketchTest method checkIsSameResource.

@Test
public void checkIsSameResource() {
    int k = 16;
    WritableMemory mem = WritableMemory.writableWrap(new byte[(k * 16) + 24]);
    WritableMemory cmem = WritableMemory.writableWrap(new byte[8]);
    DirectUpdateDoublesSketch duds = (DirectUpdateDoublesSketch) DoublesSketch.builder().setK(k).build(mem);
    assertTrue(duds.isSameResource(mem));
    DirectCompactDoublesSketch dcds = (DirectCompactDoublesSketch) duds.compact(cmem);
    assertTrue(dcds.isSameResource(cmem));
    UpdateDoublesSketch uds = DoublesSketch.builder().setK(k).build();
    assertFalse(uds.isSameResource(mem));
}
Also used : WritableMemory(org.apache.datasketches.memory.WritableMemory) 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