use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class DoublesUnionImplTest method checkUpdateMemoryDirect.
@Test
public void checkUpdateMemoryDirect() {
final DoublesSketch qs1 = buildAndLoadDQS(256, 1000);
final int bytes = qs1.getCompactStorageBytes();
final WritableMemory dstMem = WritableMemory.writableWrap(new byte[bytes]);
qs1.putMemory(dstMem);
final Memory srcMem = dstMem;
// virgin
final DoublesUnion union = DoublesUnion.builder().build();
union.update(srcMem);
for (int i = 1000; i < 2000; i++) {
union.update(i);
}
final DoublesSketch qs2 = union.getResult();
assertEquals(qs2.getMaxValue(), 1999, 0.0);
final String s = union.toString();
// enable printing to see
println(s);
// sets to null
union.reset();
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ToFromByteArrayTest method toFrom1.
private static void toFrom1(int lgConfigK, TgtHllType tgtHllType, int n) {
HllSketch src = new HllSketch(lgConfigK, tgtHllType);
for (int i = 0; i < n; i++) {
src.update(i);
}
// println("n: " + n + ", lgK: " + lgK + ", type: " + tgtHllType);
// printSketch(src, "SRC");
// compact
byte[] byteArr1 = src.toCompactByteArray();
// using byte[] interface
HllSketch dst = HllSketch.heapify(byteArr1);
// printSketch(dst, "DST");
assertEquals(dst.getEstimate(), src.getEstimate(), 0.0);
// updatable
byte[] byteArr2 = src.toUpdatableByteArray();
Memory mem2 = Memory.wrap(byteArr2);
// using Memory interface
HllSketch dst2 = HllSketch.heapify(mem2);
// printSketch(dst, "DST");
assertEquals(dst2.getEstimate(), src.getEstimate(), 0.0);
WritableMemory mem3 = WritableMemory.writableWrap(byteArr2);
// using WritableMemory interface
HllSketch dst3 = HllSketch.heapify(mem3);
// printSketch(dst, "DST");
assertEquals(dst3.getEstimate(), src.getEstimate(), 0.0);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class UnionTest method toFrom1.
private static void toFrom1(int lgK, TgtHllType tgtHllType, int n) {
Union srcU = newUnion(lgK);
HllSketch srcSk = new HllSketch(lgK, tgtHllType);
for (int i = 0; i < n; i++) {
srcSk.update(i);
}
println("n: " + n + ", lgK: " + lgK + ", type: " + tgtHllType);
// printSketch(src, "SRC");
srcU.update(srcSk);
byte[] byteArr = srcU.toCompactByteArray();
Memory mem = Memory.wrap(byteArr);
Union dstU = Union.heapify(mem);
assertFalse(dstU.isSameResource(mem));
assertEquals(dstU.getEstimate(), srcU.getEstimate(), 0.0);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class IntegerSketchTest method serDeTest.
@SuppressWarnings("deprecation")
@Test
public void serDeTest() {
final int lgK = 12;
final int K = 1 << lgK;
final IntegerSummary.Mode a1Mode = IntegerSummary.Mode.AlwaysOne;
final IntegerSketch a1Sk = new IntegerSketch(lgK, a1Mode);
final int m = 2 * K;
for (int i = 0; i < m; i++) {
a1Sk.update(i, 1);
}
final double est1 = a1Sk.getEstimate();
final Memory mem = Memory.wrap(a1Sk.toByteArray());
final IntegerSketch a1Sk2 = new IntegerSketch(mem, a1Mode);
final double est2 = a1Sk2.getEstimate();
assertEquals(est1, est2);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ArrayOfDoublesCompactSketchTest method heapToDirectExactTwoDoubles.
@Test
public void heapToDirectExactTwoDoubles() {
ArrayOfDoublesUpdatableSketch sketch1 = new ArrayOfDoublesUpdatableSketchBuilder().setNumberOfValues(2).build();
sketch1.update("a", new double[] { 1, 2 });
sketch1.update("b", new double[] { 1, 2 });
sketch1.update("c", new double[] { 1, 2 });
sketch1.update("d", new double[] { 1, 2 });
sketch1.update("a", new double[] { 1, 2 });
sketch1.update("b", new double[] { 1, 2 });
sketch1.update("c", new double[] { 1, 2 });
sketch1.update("d", new double[] { 1, 2 });
ArrayOfDoublesCompactSketch csk = sketch1.compact();
Memory mem = Memory.wrap(csk.toByteArray());
ArrayOfDoublesSketch sketch2 = new DirectArrayOfDoublesCompactSketch(mem);
Assert.assertFalse(sketch2.isEmpty());
Assert.assertFalse(sketch2.isEstimationMode());
Assert.assertEquals(sketch2.getEstimate(), 4.0);
Assert.assertEquals(sketch2.getUpperBound(1), 4.0);
Assert.assertEquals(sketch2.getLowerBound(1), 4.0);
Assert.assertEquals(sketch2.getThetaLong(), Long.MAX_VALUE);
Assert.assertEquals(sketch2.getTheta(), 1.0);
double[][] values = sketch2.getValues();
Assert.assertEquals(values.length, 4);
for (double[] array : values) {
Assert.assertEquals(array.length, 2);
Assert.assertEquals(array[0], 2.0);
Assert.assertEquals(array[1], 4.0);
}
}
Aggregations