Search in sources :

Example 31 with WritableMemory

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

the class HeapUnionTest method checkSerVer1Handling.

@Test
public void checkSerVer1Handling() {
    //4096
    int lgK = 12;
    int k = 1 << lgK;
    int u1 = 2 * k;
    //smaller exact sketch forces early stop
    int u2 = 1024;
    int totU = u1 + u2;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    //2*k
    for (int i = 0; i < u1; i++) usk1.update(i);
    //2*k + 1024 no overlap
    for (int i = u1; i < totU; i++) usk2.update(i);
    WritableMemory skMem1 = WritableMemory.wrap(usk1.compact(true, null).toByteArray());
    WritableMemory skMem2 = WritableMemory.wrap(usk2.compact(true, null).toByteArray());
    Memory v1mem1 = convertSerV3toSerV1(skMem1);
    Memory v1mem2 = convertSerV3toSerV1(skMem2);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion();
    union.update(v1mem1);
    union.update(v1mem2);
    CompactSketch cOut = union.getResult(true, null);
    assertEquals(cOut.getEstimate(), totU, .05 * k);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 32 with WritableMemory

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

the class HeapUnionTest method checkEmptySerVer2and3.

@Test
public //where the granted mem is larger than required
void checkEmptySerVer2and3() {
    UpdateSketch usk1 = UpdateSketch.builder().build();
    CompactSketch usk1c = usk1.compact(true, null);
    byte[] skArr = usk1c.toByteArray();
    byte[] skArr2 = Arrays.copyOf(skArr, skArr.length * 2);
    WritableMemory v3mem1 = WritableMemory.wrap(skArr2);
    Union union = SetOperation.builder().buildUnion();
    union.update(v3mem1);
    Memory v2mem1 = convertSerV3toSerV2(v3mem1);
    WritableMemory v2mem2 = WritableMemory.wrap(new byte[16]);
    v2mem1.copyTo(0, v2mem2, 0, 8);
    union = SetOperation.builder().buildUnion();
    union.update(v2mem2);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 33 with WritableMemory

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

the class HeapUnionTest method checkWrapEstNoOverlapOrderedDirectIn.

@Test
public void checkWrapEstNoOverlapOrderedDirectIn() {
    //4096
    int lgK = 12;
    int k = 1 << lgK;
    int u = 4 * k;
    //2k estimating
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    //2k exact for early stop test
    UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(2 * k).build();
    //2k estimating
    for (int i = 0; i < u / 2; i++) usk1.update(i);
    //2k no overlap, exact, will force early stop
    for (int i = u / 2; i < u; i++) usk2.update(i);
    WritableMemory cskMem2 = WritableMemory.wrap(new byte[usk2.getCurrentBytes(true)]);
    //ordered, loads the cskMem2 as ordered
    CompactSketch cosk2 = usk2.compact(true, cskMem2);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion();
    //updates with heap UpdateSketch
    union.update(usk1);
    //updates with direct CompactSketch, ordered, use early stop
    union.update(cosk2);
    UpdateSketch emptySketch = UpdateSketch.builder().setNominalEntries(k).build();
    //updates with empty sketch
    union.update(emptySketch);
    emptySketch = null;
    //updates with null sketch
    union.update(emptySketch);
    testAllCompactForms(union, u, 0.05);
    Union union2 = (Union) SetOperation.heapify(Memory.wrap(union.toByteArray()));
    testAllCompactForms(union2, u, 0.05);
    union2.reset();
    assertEquals(union2.getResult(true, null).getEstimate(), 0.0, 0.0);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 34 with WritableMemory

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

the class HeapUnionTest method testAllCompactForms.

//used by DirectUnionTest as well
public static void testAllCompactForms(Union union, double expected, double toll) {
    double compEst1, compEst2;
    //not ordered, no mem
    compEst1 = union.getResult(false, null).getEstimate();
    assertEquals(compEst1, expected, toll * expected);
    //ordered, no mem
    CompactSketch comp2 = union.getResult(true, null);
    compEst2 = comp2.getEstimate();
    assertEquals(compEst2, compEst1, 0.0);
    WritableMemory mem = WritableMemory.wrap(new byte[comp2.getCurrentBytes(false)]);
    //not ordered, mem
    compEst2 = union.getResult(false, mem).getEstimate();
    assertEquals(compEst2, compEst1, 0.0);
    //ordered, mem
    compEst2 = union.getResult(true, mem).getEstimate();
    assertEquals(compEst2, compEst1, 0.0);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory)

Example 35 with WritableMemory

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

the class DirectIntersectionTest method checkExceptions1.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkExceptions1() {
    int k = 16;
    WritableMemory mem = WritableMemory.wrap(new byte[k * 16 + PREBYTES]);
    IntersectionImpl.initNewDirectInstance(DEFAULT_UPDATE_SEED, mem);
    //corrupt SerVer
    mem.putByte(PreambleUtil.SER_VER_BYTE, (byte) 2);
    IntersectionImplR.wrapInstance(mem, DEFAULT_UPDATE_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