Search in sources :

Example 41 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkDQStoCompactEmptyForms.

@Test
public void checkDQStoCompactEmptyForms() {
    int k = 512;
    WritableMemory mem = makeNativeMemory(k);
    UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(mem);
    //empty
    //exercise toString
    usk.toString(false, true, 0, false);
    assertEquals(usk.getClass().getSimpleName(), "DirectQuickSelectSketch");
    double uskEst = usk.getEstimate();
    double uskLB = usk.getLowerBound(2);
    double uskUB = usk.getUpperBound(2);
    assertEquals(usk.isEstimationMode(), false);
    //compact form
    int bytes = usk.getCurrentBytes(true);
    assertEquals(bytes, 8);
    byte[] memArr2 = new byte[bytes];
    WritableMemory mem2 = WritableMemory.wrap(memArr2);
    CompactSketch csk2 = usk.compact(false, mem2);
    assertEquals(csk2.getEstimate(), uskEst);
    assertEquals(csk2.getLowerBound(2), uskLB);
    assertEquals(csk2.getUpperBound(2), uskUB);
    assertEquals(csk2.isEmpty(), true);
    assertEquals(csk2.isEstimationMode(), false);
    assertEquals(csk2.getClass().getSimpleName(), "DirectCompactSketch");
    CompactSketch csk3 = usk.compact(true, mem2);
    csk3.toString(false, true, 0, false);
    csk3.toString();
    assertEquals(csk3.getEstimate(), uskEst);
    assertEquals(csk3.getLowerBound(2), uskLB);
    assertEquals(csk3.getUpperBound(2), uskUB);
    assertEquals(csk3.isEmpty(), true);
    assertEquals(csk3.isEstimationMode(), false);
    assertEquals(csk3.getClass().getSimpleName(), "DirectCompactOrderedSketch");
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 42 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkSamplingMode.

@Test
public void checkSamplingMode() {
    int k = 4096;
    float p = (float) 0.5;
    WritableMemory mem = makeNativeMemory(k);
    UpdateSketch usk = UpdateSketch.builder().setP(p).setNominalEntries(k).build(mem);
    //for internal checks
    DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
    for (int i = 0; i < k; i++) {
        usk.update(i);
    }
    double p2 = sk1.getP();
    double theta = sk1.getTheta();
    assertTrue(theta <= p2);
    double est = usk.getEstimate();
    assertEquals(k, est, k * .05);
    double ub = usk.getUpperBound(1);
    assertTrue(ub > est);
    double lb = usk.getLowerBound(1);
    assertTrue(lb < est);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 43 with WritableMemory

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

the class DirectQuickSelectSketchTest method checkFamilyAndRF.

@Test
public void checkFamilyAndRF() {
    int k = 16;
    WritableMemory mem = WritableMemory.wrap(new byte[k * 16 + 24]);
    UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(mem);
    assertEquals(sketch.getFamily(), Family.QUICKSELECT);
    assertEquals(sketch.getResizeFactor(), ResizeFactor.X8);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 44 with WritableMemory

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

the class DirectUnionTest method checkGetFamily.

@Test
public void checkGetFamily() {
    int k = 16;
    WritableMemory mem = WritableMemory.wrap(new byte[k * 16 + 32]);
    SetOperation setOp = new SetOperationBuilder().setNominalEntries(k).build(Family.UNION, mem);
    assertEquals(setOp.getFamily(), Family.UNION);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 45 with WritableMemory

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

the class DirectUnionTest method checkExactUnionNoOverlap.

@Test
public void checkExactUnionNoOverlap() {
    //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);
}
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