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
}
}
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);
}
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
}
}
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);
}
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
}
}
Aggregations