Search in sources :

Example 31 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkCorruptLgNomLongs.

// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkCorruptLgNomLongs() {
    int k = 16;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch.builder().setNominalEntries(k).build(mem);
        // corrupt
        mem.putByte(LG_NOM_LONGS_BYTE, (byte) 2);
        Sketch.heapify(mem, DEFAULT_UPDATE_SEED);
    } 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 32 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkHeapifyByteArrayExact.

@Test
public void checkHeapifyByteArrayExact() {
    int k = 512;
    try (WritableHandle h = makeNativeMemory(k)) {
        WritableMemory mem = h.getWritable();
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
        for (int i = 0; i < k; i++) {
            usk.update(i);
        }
        int bytes = usk.getCurrentBytes();
        byte[] byteArray = usk.toByteArray();
        assertEquals(bytes, byteArray.length);
        Memory srcMem = Memory.wrap(byteArray);
        Sketch usk2 = Sketch.heapify(srcMem);
        assertEquals(usk2.getEstimate(), k, 0.0);
        assertEquals(usk2.getLowerBound(2), k, 0.0);
        assertEquals(usk2.getUpperBound(2), k, 0.0);
        assertEquals(usk2.isEmpty(), false);
        assertEquals(usk2.isEstimationMode(), false);
        assertEquals(usk2.getClass().getSimpleName(), "HeapQuickSelectSketch");
        // Run toString just to make sure that we can pull out all of the relevant information.
        // That is, this is being run for its side-effect of accessing things.
        // If something is wonky, it will generate an exception and fail the test.
        usk2.toString(true, true, 8, true);
    } 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 33 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkConstructorKtooSmall.

// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkConstructorKtooSmall() {
    int k = 8;
    try (WritableHandle h = makeNativeMemory(k)) {
        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 34 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkConstructReconstructFromMemory.

@Test
public void checkConstructReconstructFromMemory() {
    int k = 4096;
    int u = 2 * k;
    try (WritableHandle h = makeNativeMemory(k)) {
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(h.getWritable());
        assertTrue(usk.isEmpty());
        // force estimation
        for (int i = 0; i < u; i++) {
            usk.update(i);
        }
        double est1 = usk.getEstimate();
        int count1 = usk.getRetainedEntries(false);
        assertEquals(est1, u, u * .05);
        assertTrue(count1 >= k);
        byte[] serArr;
        double est2;
        int count2;
        serArr = usk.toByteArray();
        WritableMemory mem2 = WritableMemory.writableWrap(serArr);
        // reconstruct to Native/Direct
        UpdateSketch usk2 = Sketches.wrapUpdateSketch(mem2);
        est2 = usk2.getEstimate();
        count2 = usk2.getRetainedEntries(false);
        assertEquals(count2, count1);
        assertEquals(est2, est1, 0.0);
    } 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 35 with WritableHandle

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

the class DirectQuickSelectSketchTest method checkEstModeMemoryArr.

@Test
public void checkEstModeMemoryArr() {
    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);
        }
        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) 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