use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class MiscFloatsTest method checkHeapifyExceptions3.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions3() {
KllFloatsSketch sk = new KllFloatsSketch();
sk.update(1.0f);
sk.update(2.0f);
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
// corrupt preamble ints, should be 5
wmem.putByte(0, (byte) 1);
KllFloatsSketch.heapify(wmem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class MiscFloatsTest method checkHeapifyExceptions2.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyExceptions2() {
KllFloatsSketch sk = new KllFloatsSketch();
WritableMemory wmem = WritableMemory.writableWrap(sk.toByteArray());
// corrupt preamble ints, should be 2
wmem.putByte(0, (byte) 1);
KllFloatsSketch.heapify(wmem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectQuantilesMemoryRequestTest method checkLimitedMemoryScenarios.
@Test
public void checkLimitedMemoryScenarios() {
// Requesting application
final int k = 128;
final int u = 40 * k;
// just the BB
final int initBytes = ((2 * k) + 4) << 3;
// This part would actually be part of the Memory owning implemention so it is faked here
try (WritableHandle wdh = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
final WritableMemory wmem = wdh.getWritable();
println("Initial mem size: " + wmem.getCapacity());
// ########## Receiving Application
// The receiving application has been given wmem to use for a sketch,
// but alas, it is not ultimately large enough.
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem);
assertTrue(usk1.isEmpty());
// Load the sketch
for (int i = 0; i < u; i++) {
// The sketch uses The MemoryRequest, acquired from wmem, to acquire more memory as
// needed, and requests via the MemoryRequest to free the old allocations.
usk1.update(i);
}
final double result = usk1.getQuantile(0.5);
println("Result: " + result);
// Success
assertEquals(result, u / 2.0, 0.05 * u);
// ########## Owning Implementation
// The actual Memory has been re-allocated several times,
// so the above wmem reference is invalid.
println("\nFinal mem size: " + wmem.getCapacity());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DoublesSketchBuilderTest method checkBuilder.
@Test
public void checkBuilder() {
// default is 128
int k = 256;
DoublesSketchBuilder bldr = DoublesSketch.builder();
bldr.setK(k);
// confirms new k
assertEquals(bldr.getK(), k);
println(bldr.toString());
int bytes = DoublesSketch.getUpdatableStorageBytes(k, 0);
byte[] byteArr = new byte[bytes];
WritableMemory mem = WritableMemory.writableWrap(byteArr);
DoublesSketch ds = bldr.build(mem);
assertTrue(ds.isDirect());
println(bldr.toString());
bldr = DoublesSketch.builder();
assertEquals(bldr.getK(), PreambleUtil.DEFAULT_K);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DoublesSketchTest method checkIsSameResource.
@Test
public void checkIsSameResource() {
int k = 16;
WritableMemory mem = WritableMemory.writableWrap(new byte[(k * 16) + 24]);
WritableMemory cmem = WritableMemory.writableWrap(new byte[8]);
DirectUpdateDoublesSketch duds = (DirectUpdateDoublesSketch) DoublesSketch.builder().setK(k).build(mem);
assertTrue(duds.isSameResource(mem));
DirectCompactDoublesSketch dcds = (DirectCompactDoublesSketch) duds.compact(cmem);
assertTrue(dcds.isSameResource(cmem));
UpdateDoublesSketch uds = DoublesSketch.builder().setK(k).build();
assertFalse(uds.isSameResource(mem));
}
Aggregations