Search in sources :

Example 46 with WritableMemory

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

the class DirectUnionTest method checkPreambleLongsCorruption.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkPreambleLongsCorruption() {
    int k = 16;
    WritableMemory mem = WritableMemory.wrap(new byte[k * 16 + 32]);
    //may be null
    Object memObj = mem.getArray();
    long memAdd = mem.getCumulativeOffset(0L);
    SetOperation setOp = new SetOperationBuilder().setNominalEntries(k).build(Family.UNION, mem);
    println(setOp.toString());
    int familyID = PreambleUtil.extractFamilyID(memObj, memAdd);
    int preLongs = PreambleUtil.extractPreLongs(memObj, memAdd);
    assertEquals(familyID, Family.UNION.getID());
    assertEquals(preLongs, Family.UNION.getMaxPreLongs());
    //Corrupt with 3; correct value is 4
    PreambleUtil.insertPreLongs(memObj, memAdd, 3);
    DirectQuickSelectSketch.writableWrap(mem, Util.DEFAULT_UPDATE_SEED);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 47 with WritableMemory

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

the class DirectUnionTest method checkUnionMemToString.

@Test
public void checkUnionMemToString() {
    int k = 64;
    //union memory
    WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
    SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 48 with WritableMemory

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

the class DirectUnionTest method checkWrapEstNoOverlapOrderedIn.

@Test
public void checkWrapEstNoOverlapOrderedIn() {
    //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);
    CompactSketch cosk2 = usk2.compact(true, null);
    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 Compact, Ordered input, early stop
    union.update(cosk2);
    UpdateSketch emptySketch = UpdateSketch.builder().setNominalEntries(k).build();
    //updates with empty
    union.update(emptySketch);
    emptySketch = null;
    //updates with null
    union.update(emptySketch);
    testAllCompactForms(union, u, 0.05);
    Union union2 = Sketches.wrapUnion(WritableMemory.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 49 with WritableMemory

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

the class DirectUnionTest method checkEstUnionNoOverlap.

@Test
public void checkEstUnionNoOverlap() {
    //4096
    int lgK = 12;
    int k = 1 << lgK;
    int u = 4 * k;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    //2*k
    for (int i = 0; i < u / 2; i++) usk1.update(i);
    //2*k no overlap
    for (int i = u / 2; i < u; i++) usk2.update(i);
    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.05);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 50 with WritableMemory

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

the class HeapAnotBTest method checkExactAnotB_AvalidNoOverlap.

@Test
public void checkExactAnotB_AvalidNoOverlap() {
    int k = 512;
    UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
    UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
    for (int i = 0; i < k / 2; i++) usk1.update(i);
    for (int i = k / 2; i < k; i++) usk2.update(i);
    AnotB aNb = SetOperation.builder().buildANotB();
    aNb.update(usk1, usk2);
    CompactSketch rsk1;
    rsk1 = aNb.getResult(false, null);
    assertEquals(rsk1.getEstimate(), k / 2.0);
    aNb.update(usk1, usk2);
    rsk1 = aNb.getResult(true, null);
    assertEquals(rsk1.getEstimate(), k / 2.0);
    //getCurrentBytes( compact )
    int bytes = rsk1.getCurrentBytes(true);
    byte[] byteArray = new byte[bytes];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    aNb.update(usk1, usk2);
    rsk1 = aNb.getResult(false, mem);
    assertEquals(rsk1.getEstimate(), k / 2.0);
    aNb.update(usk1, usk2);
    rsk1 = aNb.getResult(true, mem);
    assertEquals(rsk1.getEstimate(), k / 2.0);
}
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