Search in sources :

Example 11 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkReadOnlyRebuildResize.

@Test
public void checkReadOnlyRebuildResize() {
    int k = 1 << 12;
    int u = 2 * k;
    int bytes = Sketches.getMaxUpdateSketchBytes(k);
    try (WritableHandle wdh = WritableMemory.allocateDirect(bytes / 2)) {
        // will request
        WritableMemory wmem = wdh.getWritable();
        UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
        for (int i = 0; i < u; i++) {
            sketch.update(i);
        }
        double est1 = sketch.getEstimate();
        byte[] ser = sketch.toByteArray();
        Memory mem = Memory.wrap(ser);
        UpdateSketch roSketch = (UpdateSketch) Sketches.wrapSketch(mem);
        double est2 = roSketch.getEstimate();
        assertEquals(est2, est1);
        try {
            roSketch.rebuild();
            fail();
        } catch (SketchesReadOnlyException e) {
        // expected
        }
        try {
            roSketch.reset();
            fail();
        } catch (SketchesReadOnlyException e) {
        // expected
        }
    } 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) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 12 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkUpperAndLowerBounds.

@Test
public void checkUpperAndLowerBounds() {
    int k = 512;
    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 est = usk.getEstimate();
        double ub = usk.getUpperBound(1);
        double lb = usk.getLowerBound(1);
        assertTrue(ub > est);
        assertTrue(lb < est);
    } 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 13 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkDQStoCompactForms.

@Test
public void checkDQStoCompactForms() {
    int k = 512;
    int u = 4 * k;
    boolean estimating = (u > 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;
        assertEquals(usk.getClass().getSimpleName(), "DirectQuickSelectSketch");
        assertTrue(usk.isDirect());
        assertTrue(usk.hasMemory());
        assertFalse(usk.isCompact());
        assertFalse(usk.isOrdered());
        for (int i = 0; i < u; i++) {
            usk.update(i);
        }
        // forces size back to k
        sk1.rebuild();
        // get baseline values
        double uskEst = usk.getEstimate();
        double uskLB = usk.getLowerBound(2);
        double uskUB = usk.getUpperBound(2);
        assertEquals(usk.isEstimationMode(), estimating);
        CompactSketch csk;
        csk = usk.compact(false, null);
        assertEquals(csk.getEstimate(), uskEst);
        assertEquals(csk.getLowerBound(2), uskLB);
        assertEquals(csk.getUpperBound(2), uskUB);
        assertEquals(csk.isEmpty(), false);
        assertEquals(csk.isEstimationMode(), estimating);
        assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
        csk = usk.compact(true, null);
        assertEquals(csk.getEstimate(), uskEst);
        assertEquals(csk.getLowerBound(2), uskLB);
        assertEquals(csk.getUpperBound(2), uskUB);
        assertEquals(csk.isEmpty(), false);
        assertEquals(csk.isEstimationMode(), estimating);
        assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
        int bytes = usk.getCompactBytes();
        assertEquals(bytes, (k * 8) + (Family.COMPACT.getMaxPreLongs() << 3));
        byte[] memArr2 = new byte[bytes];
        WritableMemory mem2 = WritableMemory.writableWrap(memArr2);
        csk = usk.compact(false, mem2);
        assertEquals(csk.getEstimate(), uskEst);
        assertEquals(csk.getLowerBound(2), uskLB);
        assertEquals(csk.getUpperBound(2), uskUB);
        assertEquals(csk.isEmpty(), false);
        assertEquals(csk.isEstimationMode(), estimating);
        assertEquals(csk.getClass().getSimpleName(), "DirectCompactSketch");
        mem2.clear();
        csk = usk.compact(true, mem2);
        assertEquals(csk.getEstimate(), uskEst);
        assertEquals(csk.getLowerBound(2), uskLB);
        assertEquals(csk.getUpperBound(2), uskUB);
        assertEquals(csk.isEmpty(), false);
        assertEquals(csk.isEstimationMode(), estimating);
        assertEquals(csk.getClass().getSimpleName(), "DirectCompactSketch");
        csk.toString(false, true, 0, false);
    } 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 14 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkErrorBounds.

@Test
public void checkErrorBounds() {
    int k = 512;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        // Exact mode
        for (int i = 0; i < k; i++) {
            usk.update(i);
        }
        double est = usk.getEstimate();
        double lb = usk.getLowerBound(2);
        double ub = usk.getUpperBound(2);
        assertEquals(est, ub, 0.0);
        assertEquals(est, lb, 0.0);
        // Est mode
        int u = 100 * k;
        for (int i = k; i < u; i++) {
            usk.update(i);
            // test duplicate rejection
            usk.update(i);
        }
        est = usk.getEstimate();
        lb = usk.getLowerBound(2);
        ub = usk.getUpperBound(2);
        assertTrue(est <= ub);
        assertTrue(est >= lb);
    } 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 15 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkMoveAndResize.

@Test
public void checkMoveAndResize() {
    int k = 1 << 12;
    int u = 2 * k;
    int bytes = Sketches.getMaxUpdateSketchBytes(k);
    try (WritableHandle wdh = WritableMemory.allocateDirect(bytes / 2)) {
        // will request
        WritableMemory wmem = wdh.getWritable();
        UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
        assertTrue(sketch.isSameResource(wmem));
        for (int i = 0; i < u; i++) {
            sketch.update(i);
        }
        assertFalse(sketch.isSameResource(wmem));
    } 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