Search in sources :

Example 16 with WritableMemory

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

the class DirectUnionTest method checkSerVer2Handling.

@Test
public void checkSerVer2Handling() {
    //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 v2mem1 = convertSerV3toSerV2(skMem1);
    Memory v2mem2 = convertSerV3toSerV2(skMem2);
    //union memory
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    union.update(v2mem1);
    union.update(v2mem2);
    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 17 with WritableMemory

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

the class DirectUnionTest method checkSizeTooSmall.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkSizeTooSmall() {
    int k = 16;
    //initialized
    WritableMemory mem = WritableMemory.wrap(new byte[k * 16 + 32]);
    SetOperation setOp = new SetOperationBuilder().setNominalEntries(k).build(Family.UNION, mem);
    println(setOp.toString());
    //for just preamble
    WritableMemory mem2 = WritableMemory.wrap(new byte[32]);
    //too small
    mem.copyTo(0, mem2, 0, 32);
    DirectQuickSelectSketch.writableWrap(mem2, Util.DEFAULT_UPDATE_SEED);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 18 with WritableMemory

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

the class DirectUnionTest method checkDirectWrap.

//Special DirectUnion cases
//Himanshu's issue
@Test
public void checkDirectWrap() {
    int nomEntries = 16;
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(nomEntries)]);
    SetOperation.builder().setNominalEntries(nomEntries).buildUnion(uMem);
    UpdateSketch sk1 = UpdateSketch.builder().setNominalEntries(nomEntries).build();
    sk1.update("a");
    sk1.update("b");
    UpdateSketch sk2 = UpdateSketch.builder().setNominalEntries(nomEntries).build();
    sk2.update("c");
    sk2.update("d");
    Union union = Sketches.wrapUnion(uMem);
    union.update(sk1);
    union = Sketches.wrapUnion(uMem);
    union.update(sk2);
    CompactSketch sketch = union.getResult(true, null);
    assertEquals(4.0, sketch.getEstimate(), 0.0);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 19 with WritableMemory

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

the class DirectUnionTest method checkWrapExact.

//these parallel the checkHeapifyExact, etc.
@Test
public void checkWrapExact() {
    //512
    int lgK = 9;
    int k = 1 << lgK;
    int u = k;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    //256
    for (int i = 0; i < u / 2; i++) usk1.update(i);
    //256 no overlap
    for (int i = u / 2; i < u; i++) usk2.update(i);
    //exact, no overlap
    assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
    //update with heap UpdateSketch
    union.update(usk1);
    //update with heap UpdateSketch
    union.update(usk2);
    testAllCompactForms(union, u, 0.0);
    Union union2 = Sketches.wrapUnion(WritableMemory.wrap(union.toByteArray()));
    testAllCompactForms(union2, u, 0.0);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 20 with WritableMemory

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

the class DirectUnionTest method checkEmptyUnionCompactOrderedResult.

@Test
public void checkEmptyUnionCompactOrderedResult() {
    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(true, mem);
    assertTrue(csk.isEmpty());
}
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