Search in sources :

Example 21 with Memory

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

the class ReadOnlyMemoryTest method wrapAndTryUpdatingUnion.

@Test
public void wrapAndTryUpdatingUnion() {
    Union u1 = SetOperation.builder().buildUnion();
    u1.update(1);
    Memory mem = Memory.wrap(ByteBuffer.wrap(u1.toByteArray()).asReadOnlyBuffer());
    Union u2 = (Union) SetOperation.wrap(mem);
    Assert.assertEquals(u2.getResult().getEstimate(), 1.0);
    boolean thrown = false;
    try {
        u2.update(2);
    } catch (SketchesReadOnlyException e) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
}
Also used : Memory(com.yahoo.memory.Memory) SketchesReadOnlyException(com.yahoo.sketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 22 with Memory

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

the class ReadOnlyMemoryTest method wrapAndTryUpdatingUpdateSketch.

@Test
public void wrapAndTryUpdatingUpdateSketch() {
    UpdateSketch updateSketch = UpdateSketch.builder().build();
    updateSketch.update(1);
    Memory mem = Memory.wrap(ByteBuffer.wrap(updateSketch.toByteArray()).asReadOnlyBuffer());
    UpdateSketch sketch = (UpdateSketch) Sketch.wrap(mem);
    assertEquals(sketch.getEstimate(), 1.0);
    boolean thrown = false;
    try {
        sketch.update(2);
    } catch (SketchesReadOnlyException e) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
}
Also used : Memory(com.yahoo.memory.Memory) SketchesReadOnlyException(com.yahoo.sketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 23 with Memory

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

the class ReadOnlyMemoryTest method wrapCompactUnorderedSketch.

@Test
public void wrapCompactUnorderedSketch() {
    UpdateSketch updateSketch = UpdateSketch.builder().build();
    updateSketch.update(1);
    Memory mem = Memory.wrap(ByteBuffer.wrap(updateSketch.compact(false, null).toByteArray()).asReadOnlyBuffer());
    Sketch sketch = Sketch.wrap(mem);
    assertEquals(sketch.getEstimate(), 1.0);
}
Also used : Memory(com.yahoo.memory.Memory) Test(org.testng.annotations.Test)

Example 24 with Memory

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

the class HeapIntersectionTest method checkFamilyID.

@Test(expectedExceptions = ClassCastException.class)
public void checkFamilyID() {
    int k = 32;
    Union union = SetOperation.builder().setNominalEntries(k).buildUnion();
    byte[] byteArray = union.toByteArray();
    Memory mem = Memory.wrap(byteArray);
    //bad cast
    Intersection inter1 = (Intersection) SetOperation.heapify(mem);
    println(inter1.toString());
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 25 with Memory

use of com.yahoo.memory.Memory 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)

Aggregations

Memory (com.yahoo.memory.Memory)141 Test (org.testng.annotations.Test)121 WritableMemory (com.yahoo.memory.WritableMemory)111 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)10 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)10 SketchesReadOnlyException (com.yahoo.sketches.SketchesReadOnlyException)6 MemoryRegion (com.yahoo.memory.MemoryRegion)2 NativeMemory (com.yahoo.memory.NativeMemory)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 CompactSketch.createCompactSketch (com.yahoo.sketches.theta.CompactSketch.createCompactSketch)1 Union (com.yahoo.sketches.theta.Union)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1