use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DoublesUnionImplTest method checkWrapInstance.
@Test
public void checkWrapInstance() {
int k = 128;
int n = 1000;
UpdateDoublesSketch sketch = DoublesSketch.builder().setK(k).build();
for (int i = 1; i <= n; i++) {
sketch.update(i);
}
double skMedian = sketch.getQuantile(.5);
Assert.assertEquals(skMedian, 500, 10);
byte[] byteArr = sketch.toByteArray(false);
WritableMemory mem = WritableMemory.wrap(byteArr);
DoublesUnion union = DoublesUnionBuilder.wrap(mem);
Assert.assertFalse(union.isEmpty());
assertTrue(union.isDirect());
DoublesSketch sketch2 = union.getResult();
double uMedian = sketch2.getQuantile(0.5);
Assert.assertEquals(skMedian, uMedian, 0.0);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DoublesUtilTest method checkCopyToHeap.
@Test
public void checkCopyToHeap() {
final int k = 128;
final int n = 400;
// HeapUpdateDoublesSketch
final HeapUpdateDoublesSketch huds = (HeapUpdateDoublesSketch) buildAndLoadQS(k, n);
final HeapUpdateDoublesSketch target1 = DoublesUtil.copyToHeap(huds);
DoublesSketchTest.testSketchEquality(huds, target1);
// DirectUpdateDoublesSketch
final WritableMemory mem1 = WritableMemory.wrap(huds.toByteArray());
final DirectUpdateDoublesSketch duds = (DirectUpdateDoublesSketch) UpdateDoublesSketch.wrap(mem1);
final HeapUpdateDoublesSketch target2 = DoublesUtil.copyToHeap(duds);
DoublesSketchTest.testSketchEquality(huds, duds);
DoublesSketchTest.testSketchEquality(duds, target2);
// HeapCompactDoublesSketch
final CompactDoublesSketch hcds = huds.compact();
final HeapUpdateDoublesSketch target3 = DoublesUtil.copyToHeap(hcds);
DoublesSketchTest.testSketchEquality(huds, hcds);
DoublesSketchTest.testSketchEquality(hcds, target3);
// DirectCompactDoublesSketch
final Memory mem2 = Memory.wrap(hcds.toByteArray());
final DirectCompactDoublesSketch dcds = (DirectCompactDoublesSketch) DoublesSketch.wrap(mem2);
final HeapUpdateDoublesSketch target4 = DoublesUtil.copyToHeap(dcds);
DoublesSketchTest.testSketchEquality(huds, dcds);
DoublesSketchTest.testSketchEquality(dcds, target4);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class SketchTest method checkIsSameResource.
@Test
public void checkIsSameResource() {
int k = 16;
WritableMemory mem = WritableMemory.wrap(new byte[(k * 16) + 24]);
WritableMemory cmem = WritableMemory.wrap(new byte[8]);
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(mem);
assertTrue(sketch.isSameResource(mem));
DirectCompactOrderedSketch dcos = (DirectCompactOrderedSketch) sketch.compact(true, cmem);
assertTrue(dcos.isSameResource(cmem));
//never create 2 sketches with the same memory, so don't do as I do :)
DirectCompactSketch dcs = (DirectCompactSketch) sketch.compact(false, cmem);
assertTrue(dcs.isSameResource(cmem));
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class SketchTest method checkHeapifyNotCompactExcep.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkHeapifyNotCompactExcep() {
int k = 512;
UpdateSketch sketch1 = UpdateSketch.builder().setFamily(QUICKSELECT).setNominalEntries(k).build();
int bytes = Sketch.getMaxCompactSketchBytes(0);
byte[] byteArray = new byte[bytes];
WritableMemory mem = WritableMemory.wrap(byteArray);
sketch1.compact(false, mem);
//corrupt:
mem.clearBits(FLAGS_BYTE, (byte) COMPACT_FLAG_MASK);
Sketch.heapify(mem);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class SketchesTest method checkSetOpMethods.
@Test
public void checkSetOpMethods() {
int k = 1024;
Memory mem1 = getCompactSketch(k, 0, k);
Memory mem2 = getCompactSketch(k, k / 2, 3 * k / 2);
SetOperationBuilder bldr = setOperationBuilder();
Union union = bldr.setNominalEntries(2 * k).buildUnion();
union.update(mem1);
CompactSketch cSk = union.getResult(true, null);
assertEquals((int) cSk.getEstimate(), k);
union.update(mem2);
cSk = union.getResult(true, null);
assertEquals((int) cSk.getEstimate(), 3 * k / 2);
byte[] ubytes = union.toByteArray();
WritableMemory uMem = WritableMemory.wrap(ubytes);
Union union2 = (Union) heapifySetOperation(uMem);
cSk = union2.getResult(true, null);
assertEquals((int) cSk.getEstimate(), 3 * k / 2);
union2 = (Union) heapifySetOperation(uMem, Util.DEFAULT_UPDATE_SEED);
cSk = union2.getResult(true, null);
assertEquals((int) cSk.getEstimate(), 3 * k / 2);
union2 = (Union) wrapSetOperation(uMem);
cSk = union2.getResult(true, null);
assertEquals((int) cSk.getEstimate(), 3 * k / 2);
union2 = (Union) wrapSetOperation(uMem, Util.DEFAULT_UPDATE_SEED);
cSk = union2.getResult(true, null);
assertEquals((int) cSk.getEstimate(), 3 * k / 2);
int serVer = getSerializationVersion(uMem);
assertEquals(serVer, 3);
}
Aggregations