Search in sources :

Example 51 with WritableMemory

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

the class HeapIntersectionTest method checkExactIntersectionNoOverlap.

@Test
public void checkExactIntersectionNoOverlap() {
    int lgK = 9;
    int k = 1 << lgK;
    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);
    Intersection inter = SetOperation.builder().buildIntersection();
    inter.update(usk1);
    inter.update(usk2);
    CompactSketch rsk1;
    boolean ordered = true;
    assertTrue(inter.hasResult());
    rsk1 = inter.getResult(!ordered, null);
    assertEquals(rsk1.getEstimate(), 0.0);
    rsk1 = inter.getResult(ordered, null);
    assertEquals(rsk1.getEstimate(), 0.0);
    boolean compact = true;
    int bytes = rsk1.getCurrentBytes(compact);
    byte[] byteArray = new byte[bytes];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    rsk1 = inter.getResult(!ordered, mem);
    assertEquals(rsk1.getEstimate(), 0.0);
    //executed twice to fully exercise the internal state machine
    rsk1 = inter.getResult(ordered, mem);
    assertEquals(rsk1.getEstimate(), 0.0);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 52 with WritableMemory

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

the class HeapIntersectionTest method checkBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
    //virgin
    Intersection inter1 = SetOperation.builder().buildIntersection();
    byte[] byteArray = inter1.toByteArray();
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //corrupt:
    mem.putByte(SER_VER_BYTE, (byte) 2);
    SetOperation.heapify(mem);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 53 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkHeapifyByteArrayEstimating.

@Test
public void checkHeapifyByteArrayEstimating() {
    int k = 4096;
    int u = 2 * k;
    WritableMemory mem = makeNativeMemory(k);
    UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
    for (int i = 0; i < u; i++) {
        usk.update(i);
    }
    double uskEst = usk.getEstimate();
    double uskLB = usk.getLowerBound(2);
    double uskUB = usk.getUpperBound(2);
    assertEquals(usk.isEstimationMode(), true);
    byte[] byteArray = usk.toByteArray();
    Memory srcMem = Memory.wrap(byteArray);
    Sketch usk2 = Sketch.heapify(srcMem);
    assertEquals(usk2.getEstimate(), uskEst);
    assertEquals(usk2.getLowerBound(2), uskLB);
    assertEquals(usk2.getUpperBound(2), uskUB);
    assertEquals(usk2.isEmpty(), false);
    assertEquals(usk2.isEstimationMode(), true);
    assertEquals(usk2.getClass().getSimpleName(), "HeapQuickSelectSketch");
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 54 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkHeapifyMemoryEstimating.

@Test
public void checkHeapifyMemoryEstimating() {
    int k = 512;
    int u = 2 * k;
    int maxBytes = (k << 4) + (Family.QUICKSELECT.getMinPreLongs() << 3);
    boolean estimating = (u > k);
    WritableMemory mem = WritableMemory.wrap(new byte[maxBytes]);
    UpdateSketch sk1 = UpdateSketch.builder().setNominalEntries(k).build(mem);
    for (int i = 0; i < u; i++) {
        sk1.update(i);
    }
    double sk1est = sk1.getEstimate();
    double sk1lb = sk1.getLowerBound(2);
    double sk1ub = sk1.getUpperBound(2);
    assertEquals(sk1.isEstimationMode(), estimating);
    assertEquals(sk1.getClass().getSimpleName(), "DirectQuickSelectSketch");
    int curCount1 = sk1.getRetainedEntries(true);
    assertTrue(sk1.isDirect());
    assertFalse(sk1.isDirty());
    UpdateSketch sk2 = UpdateSketch.heapify(mem);
    assertEquals(sk2.getEstimate(), sk1est);
    assertEquals(sk2.getLowerBound(2), sk1lb);
    assertEquals(sk2.getUpperBound(2), sk1ub);
    assertEquals(sk2.isEmpty(), false);
    assertEquals(sk2.isEstimationMode(), estimating);
    assertEquals(sk2.getClass().getSimpleName(), "HeapQuickSelectSketch");
    int curCount2 = sk2.getRetainedEntries(true);
    long[] cache = sk2.getCache();
    assertEquals(curCount1, curCount2);
    long thetaLong = sk2.getThetaLong();
    int cacheCount = HashOperations.count(cache, thetaLong);
    assertEquals(curCount1, cacheCount);
    assertFalse(sk2.isDirect());
    assertFalse(sk2.isDirty());
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 55 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkEstMode.

@Test
public void checkEstMode() {
    int k = 4096;
    int u = 2 * k;
    WritableMemory mem = makeNativeMemory(k);
    UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
    //for internal checks
    DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
    assertTrue(usk.isEmpty());
    for (int i = 0; i < u; i++) {
        usk.update(i);
    }
    assertTrue(sk1.getRetainedEntries(false) > k);
}
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