Search in sources :

Example 6 with WritableDirectHandle

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

the class DirectQuantilesMemoryRequestTest method checkGrowFromWrappedEmptySketch.

@Test
public void checkGrowFromWrappedEmptySketch() {
    final int k = 16;
    final int n = 0;
    final int initBytes = DoublesSketch.getUpdatableStorageBytes(k, n);
    final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build();
    final Memory origSketchMem = Memory.wrap(usk1.toByteArray());
    try (WritableDirectHandle memHandler = WritableMemory.allocateDirect(initBytes)) {
        // putN() -- force-increment, check regular update
        final WritableMemory mem = memHandler.get();
        origSketchMem.copyTo(0, mem, 0, initBytes);
        UpdateDoublesSketch usk2 = DirectUpdateDoublesSketch.wrapInstance(mem);
        assertEquals(usk2.getMemory().getCapacity(), initBytes);
        assertTrue(usk2.isEmpty());
        usk2.putN(5);
        assertEquals(usk2.getN(), 5);
        // will request a full base buffer
        usk2.update(1.0);
        assertEquals(usk2.getN(), 6);
        final int expectedSize = COMBINED_BUFFER + ((2 * k) << 3);
        assertEquals(usk2.getMemory().getCapacity(), expectedSize);
        // putMinValue()
        origSketchMem.copyTo(0, mem, 0, initBytes);
        usk2 = DirectUpdateDoublesSketch.wrapInstance(mem);
        assertEquals(usk2.getMemory().getCapacity(), initBytes);
        assertEquals(usk2.getMinValue(), Double.POSITIVE_INFINITY);
        usk2.putMinValue(5.0);
        assertEquals(usk2.getMinValue(), 5.0);
        assertEquals(usk2.getMemory().getCapacity(), expectedSize);
        // putMaxValue()
        origSketchMem.copyTo(0, mem, 0, initBytes);
        usk2 = DirectUpdateDoublesSketch.wrapInstance(mem);
        assertEquals(usk2.getMemory().getCapacity(), initBytes);
        assertEquals(usk2.getMaxValue(), Double.NEGATIVE_INFINITY);
        usk2.putMaxValue(5.0);
        assertEquals(usk2.getMaxValue(), 5.0);
        assertEquals(usk2.getMemory().getCapacity(), expectedSize);
    }
}
Also used : WritableDirectHandle(com.yahoo.memory.WritableDirectHandle) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)6 Test (org.testng.annotations.Test)6 WritableMemory (com.yahoo.memory.WritableMemory)5 Memory (com.yahoo.memory.Memory)1