Search in sources :

Example 6 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class HeapAlphaSketchTest method checkMemDeSerExceptions.

@Test
public void checkMemDeSerExceptions() {
    int k = 1024;
    UpdateSketch sk1 = UpdateSketch.builder().setFamily(ALPHA).setNominalEntries(k).build();
    //forces preLongs to 3
    sk1.update(1L);
    byte[] bytearray1 = sk1.toByteArray();
    WritableMemory mem = WritableMemory.wrap(bytearray1);
    long pre0 = mem.getLong(0);
    //Corrupt PreLongs
    tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    //Corrupt SerVer
    tryBadMem(mem, SER_VER_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    //Corrupt Family
    tryBadMem(mem, FAMILY_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    //Corrupt READ_ONLY to true
    tryBadMem(mem, FLAGS_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    try {
        //Corrupt the theta value
        mem.putDouble(16, 0.5);
        HeapAlphaSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    //expected
    }
    //restore theta
    mem.putDouble(16, 1.0);
    byte[] byteArray2 = new byte[bytearray1.length - 1];
    WritableMemory mem2 = WritableMemory.wrap(byteArray2);
    mem.copyTo(0, mem2, 0, mem2.getCapacity());
    try {
        HeapAlphaSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    //expected
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 7 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class DirectUnionTest method checkUpdateMemorySpecialCases.

@Test
public void checkUpdateMemorySpecialCases() {
    //4096
    int lgK = 12;
    int k = 1 << lgK;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    CompactSketch usk1c = usk1.compact(true, null);
    WritableMemory v3mem1 = WritableMemory.wrap(usk1c.toByteArray());
    Memory v1mem1 = convertSerV3toSerV1(v3mem1);
    //union memory
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.update(v1mem1);
    CompactSketch cOut = union.getResult(true, null);
    assertEquals(cOut.getEstimate(), 0.0, 0.0);
    Memory v2mem1 = convertSerV3toSerV2(v3mem1);
    //union memory
    uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.update(v2mem1);
    cOut = union.getResult(true, null);
    assertEquals(cOut.getEstimate(), 0.0, 0.0);
    //union memory
    uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.update(v3mem1);
    cOut = union.getResult(true, null);
    assertEquals(cOut.getEstimate(), 0.0, 0.0);
    //union memory
    uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    v3mem1 = null;
    union.update(v3mem1);
    cOut = union.getResult(true, null);
    assertEquals(cOut.getEstimate(), 0.0, 0.0);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 8 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class DirectUnionTest method checkUpdateMemorySpecialCases2.

@Test
public void checkUpdateMemorySpecialCases2() {
    //4096
    int lgK = 12;
    int k = 1 << lgK;
    int u = 2 * k;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    //force prelongs to 3
    for (int i = 0; i < u; i++) usk1.update(i);
    CompactSketch usk1c = usk1.compact(true, null);
    WritableMemory v3mem1 = WritableMemory.wrap(usk1c.toByteArray());
    //union memory
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.update(v3mem1);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 9 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class DirectUnionTest method checkEmptyUnionCompactResult.

@Test
public void checkEmptyUnionCompactResult() {
    int k = 64;
    //union memory
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    WritableMemory mem = WritableMemory.wrap(new byte[Sketch.getMaxCompactSketchBytes(0)]);
    //DirectCompactSketch
    CompactSketch csk = union.getResult(false, mem);
    assertTrue(csk.isEmpty());
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 10 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class DirectUnionTest method checkMemBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkMemBadSerVer() {
    //4096
    int lgK = 12;
    int k = 1 << lgK;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    CompactSketch usk1c = usk1.compact(true, null);
    WritableMemory v3mem1 = WritableMemory.wrap(usk1c.toByteArray());
    //corrupt SerVer
    v3mem1.putByte(SER_VER_BYTE, (byte) 0);
    //union memory
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.update(v3mem1);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

WritableMemory (com.yahoo.memory.WritableMemory)264 Test (org.testng.annotations.Test)210 Memory (com.yahoo.memory.Memory)34 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)15 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)6 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)5 MemoryRequestServer (com.yahoo.memory.MemoryRequestServer)2 ArrayOfDoublesSerDe (com.yahoo.sketches.ArrayOfDoublesSerDe)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 PreambleUtil.extractTgtHllType (com.yahoo.sketches.hll.PreambleUtil.extractTgtHllType)2 ResizeFactor (com.yahoo.sketches.ResizeFactor)1 PreambleUtil.extractCurMode (com.yahoo.sketches.hll.PreambleUtil.extractCurMode)1 PreambleUtil.insertTgtHllType (com.yahoo.sketches.hll.PreambleUtil.insertTgtHllType)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1