Search in sources :

Example 1 with SketchesReadOnlyException

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

the class ReadOnlyMemoryTest method wrapIntersection.

@Test
public void wrapIntersection() {
    UpdateSketch us1 = UpdateSketch.builder().build();
    us1.update(1);
    us1.update(2);
    UpdateSketch us2 = UpdateSketch.builder().build();
    us2.update(2);
    us2.update(3);
    Intersection i1 = SetOperation.builder().buildIntersection();
    i1.update(us1);
    i1.update(us2);
    Memory mem = Memory.wrap(ByteBuffer.wrap(i1.toByteArray()).asReadOnlyBuffer());
    Intersection i2 = (Intersection) SetOperation.wrap(mem);
    Assert.assertEquals(i2.getResult().getEstimate(), 1.0);
    boolean thrown = false;
    try {
        i2.update(us1);
    } 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 2 with SketchesReadOnlyException

use of com.yahoo.sketches.SketchesReadOnlyException 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 3 with SketchesReadOnlyException

use of com.yahoo.sketches.SketchesReadOnlyException 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 4 with SketchesReadOnlyException

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

the class ReadOnlyMemoryTest method wrapAndTryUpdatingSparseSketch.

@Test
public void wrapAndTryUpdatingSparseSketch() {
    UpdateDoublesSketch s1 = DoublesSketch.builder().build();
    s1.update(1);
    s1.update(2);
    final byte[] bytes = s1.toByteArray(false);
    // 32 + MIN_K(=2) * 2 * 8 = 64
    Assert.assertEquals(bytes.length, 64);
    //final Memory mem = Memory.wrap(ByteBuffer.wrap(bytes).asReadOnlyBuffer());
    final Memory mem = Memory.wrap(bytes);
    final UpdateDoublesSketch s2 = (UpdateDoublesSketch) DoublesSketch.wrap(mem);
    Assert.assertEquals(s2.getMinValue(), 1.0);
    Assert.assertEquals(s2.getMaxValue(), 2.0);
    try {
        s2.update(3);
        fail();
    } catch (final SketchesReadOnlyException e) {
    // expected
    }
}
Also used : Memory(com.yahoo.memory.Memory) SketchesReadOnlyException(com.yahoo.sketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 5 with SketchesReadOnlyException

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

the class ReadOnlyMemoryTest method wrapUnionFromSparse.

@Test
public void wrapUnionFromSparse() {
    UpdateDoublesSketch s1 = DoublesSketch.builder().build();
    s1.update(1);
    s1.update(2);
    Memory mem = Memory.wrap(s1.toByteArray(false));
    DoublesUnion u = DoublesUnionBuilder.wrap(mem);
    DoublesSketch s2 = u.getResult();
    Assert.assertEquals(s2.getMinValue(), 1.0);
    Assert.assertEquals(s2.getMaxValue(), 2.0);
    // ensure update and reset methods fail
    try {
        u.update(3);
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
    try {
        u.update(s2);
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
    try {
        u.update(mem);
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
    try {
        u.reset();
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
    try {
        u.getResultAndReset();
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
}
Also used : Memory(com.yahoo.memory.Memory) SketchesReadOnlyException(com.yahoo.sketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Aggregations

Memory (com.yahoo.memory.Memory)6 SketchesReadOnlyException (com.yahoo.sketches.SketchesReadOnlyException)6 Test (org.testng.annotations.Test)6