Search in sources :

Example 1 with SketchesReadOnlyException

use of org.apache.datasketches.SketchesReadOnlyException in project sketches-core by DataSketches.

the class DirectHllSketchTest method noWriteAccess.

private static void noWriteAccess(TgtHllType tgtHllType, int n) {
    int lgConfigK = 8;
    int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
    WritableMemory wmem = WritableMemory.allocate(bytes);
    HllSketch sk = new HllSketch(lgConfigK, tgtHllType, wmem);
    for (int i = 0; i < n; i++) {
        sk.update(i);
    }
    HllSketch sk2 = HllSketch.wrap(wmem);
    try {
        sk2.update(1);
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
}
Also used : SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 2 with SketchesReadOnlyException

use of org.apache.datasketches.SketchesReadOnlyException in project sketches-core by DataSketches.

the class ReadOnlyMemoryTest method wrapAndTryUpdatingUnionEstimationMode.

@Test
public void wrapAndTryUpdatingUnionEstimationMode() {
    final int numUniques = 10000;
    int key = 0;
    final ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().build();
    for (int i = 0; i < numUniques; i++) {
        sketch1.update(key++, new double[] { 1 });
    }
    final ArrayOfDoublesUnion union1 = new ArrayOfDoublesSetOperationBuilder().buildUnion();
    union1.union(sketch1);
    final ArrayOfDoublesUnion union2 = ArrayOfDoublesSketches.wrapUnion(Memory.wrap(union1.toByteArray()));
    final ArrayOfDoublesSketch resultSketch = union2.getResult();
    Assert.assertTrue(resultSketch.isEstimationMode());
    Assert.assertEquals(resultSketch.getEstimate(), numUniques, numUniques * 0.04);
    // make sure union update actually needs to modify the union
    final ArrayOfDoublesUpdatableSketch sketch2 = new ArrayOfDoublesUpdatableSketchBuilder().build();
    for (int i = 0; i < numUniques; i++) {
        sketch2.update(key++, new double[] { 1 });
    }
    boolean thrown = false;
    try {
        union2.union(sketch2);
    } catch (final SketchesReadOnlyException e) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
}
Also used : ArrayOfDoublesUpdatableSketchBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder) ArrayOfDoublesUpdatableSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch) ArrayOfDoublesUnion(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUnion) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) ArrayOfDoublesSetOperationBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSetOperationBuilder) ArrayOfDoublesSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch) Test(org.testng.annotations.Test)

Example 3 with SketchesReadOnlyException

use of org.apache.datasketches.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().order(ByteOrder.nativeOrder()));
    Union u2 = (Union) Sketches.wrapSetOperation(mem);
    Union u3 = Sketches.wrapUnion(mem);
    Assert.assertEquals(u2.getResult().getEstimate(), 1.0);
    Assert.assertEquals(u3.getResult().getEstimate(), 1.0);
    try {
        u2.update(2);
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
    try {
        u3.update(2);
        fail();
    } catch (SketchesReadOnlyException e) {
    // expected
    }
}
Also used : Memory(org.apache.datasketches.memory.Memory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 4 with SketchesReadOnlyException

use of org.apache.datasketches.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().order(ByteOrder.nativeOrder()));
    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(org.apache.datasketches.memory.Memory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 5 with SketchesReadOnlyException

use of org.apache.datasketches.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 = DoublesUnion.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(org.apache.datasketches.memory.Memory) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Aggregations

SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)12 Test (org.testng.annotations.Test)10 Memory (org.apache.datasketches.memory.Memory)8 WritableMemory (org.apache.datasketches.memory.WritableMemory)3 SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)2 ArrayOfDoublesUpdatableSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch)2 ArrayOfDoublesUpdatableSketchBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder)2 WritableHandle (org.apache.datasketches.memory.WritableHandle)1 ArrayOfDoublesSetOperationBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSetOperationBuilder)1 ArrayOfDoublesSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch)1 ArrayOfDoublesUnion (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUnion)1