Search in sources :

Example 1 with WritableMemory

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

the class ForwardCompatibilityTest method checkSerVer2_32Bytes_1Value.

@Test
public void checkSerVer2_32Bytes_1Value() {
    byte[] byteArray = new byte[32];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //mdLongs, RF (RR) = 0
    mem.putByte(0, (byte) 3);
    //SerVer
    mem.putByte(1, (byte) 2);
    //SketchType = SetSketch
    mem.putByte(2, (byte) 3);
    //byte 3 lgNomLongs not used w SetSketch
    //byte 4 lgArrLongs not used w SetSketch
    //byte 5 Flags: b0: BigEnd, b1: ReadOnly, b2: Empty, b3: NoRebuild, b4, Unordered
    //Flags = ReadOnly
    mem.putByte(5, (byte) 2);
    short seedHash = Util.computeSeedHash(Util.DEFAULT_UPDATE_SEED);
    mem.putShort(6, seedHash);
    //curCount = 0
    mem.putInt(8, 0);
    mem.putLong(16, Long.MAX_VALUE);
    Memory srcMem = Memory.wrap(byteArray);
    Sketch sketch = Sketch.heapify(srcMem);
    assertEquals(sketch.isEmpty(), false);
    assertEquals(sketch.isEstimationMode(), false);
    assertEquals(sketch.isDirect(), false);
    assertEquals(sketch.isCompact(), true);
    assertEquals(sketch.isOrdered(), true);
    String name = sketch.getClass().getSimpleName();
    assertEquals(name, "HeapCompactOrderedSketch");
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 2 with WritableMemory

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

the class ForwardCompatibilityTest method convertSerV3toSerV1.

/**
   * Converts a SerVer3 CompactSketch to a SerVer1 SetSketch.
   * @param v3mem a SerVer3 CompactSketch, ordered and with 24 byte preamble.
   * @return a SerVer1 SetSketch as Memory object
   */
public static WritableMemory convertSerV3toSerV1(Memory v3mem) {
    //validate that v3mem is in the right form
    int serVer = v3mem.getByte(SER_VER_BYTE);
    int famId = v3mem.getByte(FAMILY_BYTE);
    int flags = v3mem.getByte(FLAGS_BYTE);
    if ((serVer != 3) || (famId != 3) || ((flags & 24) != 24))
        throw new SketchesArgumentException("Memory must be V3, Compact, Ordered");
    //must convert v3 preamble to a v1 preamble
    int v3preLongs = v3mem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
    int entries;
    long thetaLong;
    if (v3preLongs == 1) {
        entries = 0;
        thetaLong = Long.MAX_VALUE;
    } else if (v3preLongs == 2) {
        entries = v3mem.getInt(RETAINED_ENTRIES_INT);
        thetaLong = Long.MAX_VALUE;
    } else {
        //preLongs == 3
        entries = v3mem.getInt(RETAINED_ENTRIES_INT);
        thetaLong = v3mem.getLong(THETA_LONG);
    }
    //compute size
    int v1preLongs = 3;
    int v1bytes = (v1preLongs + entries) << 3;
    //create new mem and place the fields for SerVer1
    WritableMemory v1mem = WritableMemory.wrap(new byte[v1bytes]);
    //Preamble = 3
    v1mem.putByte(0, (byte) 3);
    //SerVer
    v1mem.putByte(1, (byte) 1);
    //SketchType SetSketch = Family CompactSketch
    v1mem.putByte(2, (byte) 3);
    //set bit1 = ReadOnly
    v1mem.putByte(6, (byte) 2);
    v1mem.putInt(RETAINED_ENTRIES_INT, entries);
    v1mem.putLong(THETA_LONG, thetaLong);
    //copy data
    v3mem.copyTo(v3preLongs << 3, v1mem, v1preLongs << 3, entries << 3);
    return v1mem;
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory)

Example 3 with WritableMemory

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

the class ForwardCompatibilityTest method checkSerVer1_24Bytes.

@Test
public void checkSerVer1_24Bytes() {
    byte[] byteArray = new byte[24];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //mdLongs
    mem.putByte(0, (byte) 3);
    //SerVer
    mem.putByte(1, (byte) 1);
    //SketchType = SetSketch
    mem.putByte(2, (byte) 3);
    //byte 3 lgNomLongs not used with SetSketch
    //byte 4 lgArrLongs not used with SetSketch
    //byte 5 lgRR not used with SetSketch
    //byte 6: Flags: b0: BigEnd, b1: ReadOnly
    mem.putByte(6, (byte) 2);
    //byte 7 Not used
    //curCount = 0
    mem.putInt(8, 0);
    mem.putLong(16, Long.MAX_VALUE);
    Memory srcMem = Memory.wrap(byteArray);
    Sketch sketch = Sketch.heapify(srcMem);
    assertEquals(sketch.isEmpty(), true);
    assertEquals(sketch.isEstimationMode(), false);
    assertEquals(sketch.isDirect(), false);
    assertEquals(sketch.isCompact(), true);
    assertEquals(sketch.isOrdered(), true);
    String name = sketch.getClass().getSimpleName();
    assertEquals(name, "HeapCompactOrderedSketch");
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 4 with WritableMemory

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

the class ForwardCompatibilityTest method checkSerVer2_24Bytes_1Value.

@Test
public void checkSerVer2_24Bytes_1Value() {
    byte[] byteArray = new byte[24];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //mdLongs, RF (RR) = 0
    mem.putByte(0, (byte) 2);
    //SerVer
    mem.putByte(1, (byte) 2);
    //SketchType = SetSketch
    mem.putByte(2, (byte) 3);
    //byte 3 lgNomLongs not used w SetSketch
    //byte 4 lgArrLongs not used w SetSketch
    //byte 5 Flags: b0: BigEnd, b1: ReadOnly, b2: Empty, b3: NoRebuild, b4, Unordered
    //Flags = ReadOnly
    mem.putByte(5, (byte) 2);
    short seedHash = Util.computeSeedHash(Util.DEFAULT_UPDATE_SEED);
    mem.putShort(6, seedHash);
    //curCount = 0
    mem.putInt(8, 0);
    //mem.putLong(16, Long.MAX_VALUE);
    Memory srcMem = Memory.wrap(byteArray);
    Sketch sketch = Sketch.heapify(srcMem);
    assertEquals(sketch.isEmpty(), false);
    assertEquals(sketch.isEstimationMode(), false);
    assertEquals(sketch.isDirect(), false);
    assertEquals(sketch.isCompact(), true);
    assertEquals(sketch.isOrdered(), true);
    String name = sketch.getClass().getSimpleName();
    assertEquals(name, "HeapCompactOrderedSketch");
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 5 with WritableMemory

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

the class HeapAlphaSketchTest method checkBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
    int k = 512;
    int u = k;
    long seed = DEFAULT_UPDATE_SEED;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
    //for internal checks
    HeapAlphaSketch sk1 = (HeapAlphaSketch) usk;
    assertTrue(usk.isEmpty());
    for (int i = 0; i < u; i++) sk1.update(i);
    assertFalse(usk.isEmpty());
    assertEquals(usk.getEstimate(), u, 0.0);
    assertEquals(sk1.getRetainedEntries(false), u);
    byte[] byteArray = usk.toByteArray();
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //corrupt the SerVer byte
    mem.putByte(SER_VER_BYTE, (byte) 0);
    Sketch.heapify(mem, seed);
}
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