Search in sources :

Example 21 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkConstructorMemTooSmall.

// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkConstructorMemTooSmall() {
    int k = 16;
    try (WritableHandle h = makeNativeMemory(k / 2)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch.builder().setNominalEntries(k).build(mem);
    } 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) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 22 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkDQStoCompactEmptyForms.

@Test
public void checkDQStoCompactEmptyForms() {
    int k = 512;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        // empty
        // exercise toString
        usk.toString(false, true, 0, false);
        assertEquals(usk.getClass().getSimpleName(), "DirectQuickSelectSketch");
        double uskEst = usk.getEstimate();
        double uskLB = usk.getLowerBound(2);
        double uskUB = usk.getUpperBound(2);
        assertEquals(usk.isEstimationMode(), false);
        // compact form
        int bytes = usk.getCompactBytes();
        assertEquals(bytes, 8);
        byte[] memArr2 = new byte[bytes];
        WritableMemory mem2 = WritableMemory.writableWrap(memArr2);
        CompactSketch csk2 = usk.compact(false, mem2);
        assertEquals(csk2.getEstimate(), uskEst);
        assertEquals(csk2.getLowerBound(2), uskLB);
        assertEquals(csk2.getUpperBound(2), uskUB);
        assertEquals(csk2.isEmpty(), true);
        assertEquals(csk2.isEstimationMode(), false);
        assertEquals(csk2.getClass().getSimpleName(), "DirectCompactSketch");
        CompactSketch csk3 = usk.compact(true, mem2);
        csk3.toString(false, true, 0, false);
        csk3.toString();
        assertEquals(csk3.getEstimate(), uskEst);
        assertEquals(csk3.getLowerBound(2), uskLB);
        assertEquals(csk3.getUpperBound(2), uskUB);
        assertEquals(csk3.isEmpty(), true);
        assertEquals(csk3.isEstimationMode(), false);
        assertEquals(csk3.getClass().getSimpleName(), "DirectCompactSketch");
    } catch (final Exception e) {
        // if (e instanceof SketchesArgumentException) {}
        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 23 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkSamplingMode.

@Test
public void checkSamplingMode() {
    int k = 4096;
    float p = (float) 0.5;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setP(p).setNominalEntries(k).build(mem);
        // for internal checks
        DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
        for (int i = 0; i < k; i++) {
            usk.update(i);
        }
        double p2 = sk1.getP();
        double theta = sk1.getTheta();
        assertTrue(theta <= p2);
        double est = usk.getEstimate();
        assertEquals(k, est, k * .05);
        double ub = usk.getUpperBound(1);
        assertTrue(ub > est);
        double lb = usk.getLowerBound(1);
        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 24 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkEstModeNativeMemory.

@Test
public void checkEstModeNativeMemory() {
    int k = 4096;
    int u = 2 * k;
    int memCapacity = (k << 4) + (Family.QUICKSELECT.getMinPreLongs() << 3);
    try (WritableHandle memHandler = WritableMemory.allocateDirect(memCapacity)) {
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(memHandler.getWritable());
        // for internal checks
        DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
        assertTrue(usk.isEmpty());
        for (int i = 0; i < u; i++) {
            usk.update(i);
        }
        double est = usk.getEstimate();
        println("" + est);
        assertEquals(usk.getEstimate(), u, u * .05);
        assertTrue(sk1.getRetainedEntries(false) > k);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 25 with WritableHandle

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

the class PreambleUtilTest method checkInsertsAndExtracts.

@Test
public void checkInsertsAndExtracts() {
    final int bytes = 32;
    try (WritableHandle offHeapMemHandler = WritableMemory.allocateDirect(bytes)) {
        final WritableMemory offHeapMem = offHeapMemHandler.getWritable();
        final WritableMemory onHeapMem = WritableMemory.writableWrap(new byte[bytes]);
        onHeapMem.clear();
        offHeapMem.clear();
        // BYTES
        int v = 0XFF;
        int onH, offH;
        // PREAMBLE_LONGS_BYTE;
        insertPreLongs(onHeapMem, v);
        onH = extractPreLongs(onHeapMem);
        assertEquals(onH, v);
        insertPreLongs(offHeapMem, v);
        offH = extractPreLongs(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        // SER_VER_BYTE;
        insertSerVer(onHeapMem, v);
        onH = extractSerVer(onHeapMem);
        assertEquals(onH, v);
        insertSerVer(offHeapMem, v);
        offH = extractSerVer(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        // FAMILY_BYTE;
        insertFamilyID(onHeapMem, v);
        onH = extractFamilyID(onHeapMem);
        assertEquals(onH, v);
        insertFamilyID(offHeapMem, v);
        offH = extractFamilyID(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        // FLAGS_BYTE;
        insertFlags(onHeapMem, v);
        onH = extractFlags(onHeapMem);
        assertEquals(onH, v);
        insertFlags(offHeapMem, v);
        offH = extractFlags(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        // SHORTS
        v = 0XFFFF;
        // K_SHORT;
        insertK(onHeapMem, v);
        onH = extractK(onHeapMem);
        assertEquals(onH, v);
        insertK(offHeapMem, v);
        offH = extractK(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        // LONGS
        // N_LONG;
        long onHL, offHL, vL = 1L << 30;
        insertN(onHeapMem, vL);
        onHL = extractN(onHeapMem);
        assertEquals(onHL, vL);
        insertN(offHeapMem, vL);
        offHL = extractN(offHeapMem);
        assertEquals(offHL, vL);
        onHeapMem.clear();
        offHeapMem.clear();
        // DOUBLES
        // MIN_DOUBLE;
        double onHD, offHD, vD = 1L << 40;
        insertMinDouble(onHeapMem, vD);
        onHD = extractMinDouble(onHeapMem);
        assertEquals(onHD, vD);
        insertMinDouble(offHeapMem, vD);
        offHD = extractMinDouble(offHeapMem);
        assertEquals(offHD, vD);
        onHeapMem.clear();
        offHeapMem.clear();
        // MAX_DOUBLE;
        insertMaxDouble(onHeapMem, vD);
        onHD = extractMaxDouble(onHeapMem);
        assertEquals(onHD, vD);
        insertMaxDouble(offHeapMem, vD);
        offHD = extractMaxDouble(offHeapMem);
        assertEquals(offHD, vD);
        onHeapMem.clear();
        offHeapMem.clear();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) 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