Search in sources :

Example 16 with ArrayOfLongsSerDe

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

the class ReservoirItemsSketchTest method checkBadFamily.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadFamily() {
    final WritableMemory mem = getBasicSerializedLongsRIS();
    // corrupt the family ID
    mem.putByte(FAMILY_BYTE, (byte) Family.ALPHA.getID());
    try {
        PreambleUtil.preambleToString(mem);
    } catch (final SketchesArgumentException e) {
        assertTrue(e.getMessage().startsWith("Inspecting preamble with Sampling family"));
    }
    ReservoirItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
    fail();
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 17 with ArrayOfLongsSerDe

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

the class ReservoirItemsSketchTest method checkVersionConversion.

@Test
public void checkVersionConversion() {
    // version change from 1 to 2 only impact first preamble long, so empty sketch is sufficient
    final int k = 32768;
    final short encK = ReservoirSize.computeSize(k);
    final ArrayOfLongsSerDe serDe = new ArrayOfLongsSerDe();
    final ReservoirItemsSketch<Long> ris = ReservoirItemsSketch.newInstance(k);
    final byte[] sketchBytesOrig = ris.toByteArray(serDe);
    // get a new byte[], manually revert to v1, then reconstruct
    final byte[] sketchBytes = ris.toByteArray(serDe);
    final WritableMemory sketchMem = WritableMemory.writableWrap(sketchBytes);
    sketchMem.putByte(SER_VER_BYTE, (byte) 1);
    // zero out all 4 bytes
    sketchMem.putInt(RESERVOIR_SIZE_INT, 0);
    sketchMem.putShort(RESERVOIR_SIZE_SHORT, encK);
    println(PreambleUtil.preambleToString(sketchMem));
    final ReservoirItemsSketch<Long> rebuilt = ReservoirItemsSketch.heapify(sketchMem, serDe);
    final byte[] rebuiltBytes = rebuilt.toByteArray(serDe);
    assertEquals(sketchBytesOrig.length, rebuiltBytes.length);
    for (int i = 0; i < sketchBytesOrig.length; ++i) {
        assertEquals(sketchBytesOrig[i], rebuiltBytes[i]);
    }
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 18 with ArrayOfLongsSerDe

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

the class ReservoirItemsSketchTest method validateSerializeAndDeserialize.

private static void validateSerializeAndDeserialize(final ReservoirItemsSketch<Long> ris) {
    final byte[] sketchBytes = ris.toByteArray(new ArrayOfLongsSerDe());
    assertEquals(sketchBytes.length, (Family.RESERVOIR.getMaxPreLongs() + ris.getNumSamples()) << 3);
    // ensure full reservoir rebuilds correctly
    final Memory mem = Memory.wrap(sketchBytes);
    final ArrayOfLongsSerDe serDe = new ArrayOfLongsSerDe();
    final ReservoirItemsSketch<Long> loadedRis = ReservoirItemsSketch.heapify(mem, serDe);
    validateReservoirEquality(ris, loadedRis);
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 19 with ArrayOfLongsSerDe

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

the class ReservoirItemsSketchTest method checkFullReservoir.

@Test
public void checkFullReservoir() {
    final int k = 1000;
    final int n = 2000;
    // specify smaller ResizeFactor to ensure multiple resizes
    final ReservoirItemsSketch<Long> ris = ReservoirItemsSketch.newInstance(k, ResizeFactor.X2);
    for (int i = 0; i < n; ++i) {
        ris.update((long) i);
    }
    assertEquals(ris.getNumSamples(), ris.getK());
    validateSerializeAndDeserialize(ris);
    println("Full reservoir:");
    println("  Preamble:");
    byte[] byteArr = ris.toByteArray(new ArrayOfLongsSerDe());
    println(ReservoirItemsSketch.toString(byteArr));
    ReservoirItemsSketch.toString(Memory.wrap(byteArr));
    println("  Sketch:");
    println(ris.toString());
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe) Test(org.testng.annotations.Test)

Example 20 with ArrayOfLongsSerDe

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

the class ReservoirItemsSketchTest method getBasicSerializedLongsRIS.

private static WritableMemory getBasicSerializedLongsRIS() {
    final int k = 10;
    final int n = 20;
    final ReservoirItemsSketch<Long> ris = ReservoirItemsSketch.newInstance(k);
    assertEquals(ris.getNumSamples(), 0);
    for (int i = 0; i < n; ++i) {
        ris.update((long) i);
    }
    assertEquals(ris.getNumSamples(), Math.min(n, k));
    assertEquals(ris.getN(), n);
    assertEquals(ris.getK(), k);
    final byte[] sketchBytes = ris.toByteArray(new ArrayOfLongsSerDe());
    return WritableMemory.writableWrap(sketchBytes);
}
Also used : ArrayOfLongsSerDe(org.apache.datasketches.ArrayOfLongsSerDe)

Aggregations

ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)31 Test (org.testng.annotations.Test)28 WritableMemory (org.apache.datasketches.memory.WritableMemory)23 Memory (org.apache.datasketches.memory.Memory)14 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)4