Search in sources :

Example 26 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkHeapifySeedConflict.

// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkHeapifySeedConflict() {
    int k = 512;
    long seed1 = 1021;
    long seed2 = DEFAULT_UPDATE_SEED;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setSeed(seed1).setNominalEntries(k).build(mem);
        byte[] byteArray = usk.toByteArray();
        Memory srcMem = Memory.wrap(byteArray);
        Sketch.heapify(srcMem, seed2);
    } catch (final Exception e) {
        if (e instanceof SketchesArgumentException) {
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 27 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkHeapifyByteArrayEstimating.

@Test
public void checkHeapifyByteArrayEstimating() {
    int k = 4096;
    int u = 2 * k;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        for (int i = 0; i < u; i++) {
            usk.update(i);
        }
        double uskEst = usk.getEstimate();
        double uskLB = usk.getLowerBound(2);
        double uskUB = usk.getUpperBound(2);
        assertEquals(usk.isEstimationMode(), true);
        byte[] byteArray = usk.toByteArray();
        Memory srcMem = Memory.wrap(byteArray);
        Sketch usk2 = Sketch.heapify(srcMem);
        assertEquals(usk2.getEstimate(), uskEst);
        assertEquals(usk2.getLowerBound(2), uskLB);
        assertEquals(usk2.getUpperBound(2), uskUB);
        assertEquals(usk2.isEmpty(), false);
        assertEquals(usk2.isEstimationMode(), true);
        assertEquals(usk2.getClass().getSimpleName(), "HeapQuickSelectSketch");
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 28 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkEstMode.

@Test
public void checkEstMode() {
    int k = 4096;
    int u = 2 * k;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        // for internal checks
        DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
        assertTrue(usk.isEmpty());
        for (int i = 0; i < u; i++) {
            usk.update(i);
        }
        assertTrue(sk1.getRetainedEntries(false) > k);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 29 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkWrapMemoryEst.

@Test
public void checkWrapMemoryEst() {
    int k = 512;
    int u = 2 * k;
    boolean estimating = (u > k);
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch sk1 = UpdateSketch.builder().setNominalEntries(k).build(mem);
        for (int i = 0; i < u; i++) {
            sk1.update(i);
        }
        double sk1est = sk1.getEstimate();
        double sk1lb = sk1.getLowerBound(2);
        double sk1ub = sk1.getUpperBound(2);
        assertEquals(sk1.isEstimationMode(), estimating);
        Sketch sk2 = Sketch.wrap(mem);
        assertEquals(sk2.getEstimate(), sk1est);
        assertEquals(sk2.getLowerBound(2), sk1lb);
        assertEquals(sk2.getUpperBound(2), sk1ub);
        assertEquals(sk2.isEmpty(), false);
        assertEquals(sk2.isEstimationMode(), estimating);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 30 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkHeapifyMemoryEstimating.

@Test
public void checkHeapifyMemoryEstimating() {
    int k = 512;
    int u = 2 * k;
    boolean estimating = (u > k);
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch sk1 = UpdateSketch.builder().setNominalEntries(k).build(mem);
        for (int i = 0; i < u; i++) {
            sk1.update(i);
        }
        double sk1est = sk1.getEstimate();
        double sk1lb = sk1.getLowerBound(2);
        double sk1ub = sk1.getUpperBound(2);
        assertEquals(sk1.isEstimationMode(), estimating);
        assertEquals(sk1.getClass().getSimpleName(), "DirectQuickSelectSketch");
        int curCount1 = sk1.getRetainedEntries(true);
        assertTrue(sk1.isDirect());
        assertTrue(sk1.hasMemory());
        assertFalse(sk1.isDirty());
        assertTrue(sk1.hasMemory());
        assertEquals(sk1.getCurrentPreambleLongs(), 3);
        UpdateSketch sk2 = Sketches.heapifyUpdateSketch(mem);
        assertEquals(sk2.getEstimate(), sk1est);
        assertEquals(sk2.getLowerBound(2), sk1lb);
        assertEquals(sk2.getUpperBound(2), sk1ub);
        assertEquals(sk2.isEmpty(), false);
        assertEquals(sk2.isEstimationMode(), estimating);
        assertEquals(sk2.getClass().getSimpleName(), "HeapQuickSelectSketch");
        int curCount2 = sk2.getRetainedEntries(true);
        long[] cache = sk2.getCache();
        assertEquals(curCount1, curCount2);
        long thetaLong = sk2.getThetaLong();
        int cacheCount = HashOperations.count(cache, thetaLong);
        assertEquals(curCount1, cacheCount);
        assertFalse(sk2.isDirect());
        assertFalse(sk2.hasMemory());
        assertFalse(sk2.isDirty());
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) 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