use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectCouponListTest method promotions.
private static void promotions(int lgConfigK, int n, TgtHllType tgtHllType, boolean compact, CurMode tgtMode) {
int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
HllSketch hllSketch;
// println("DIRECT");
byte[] barr1;
WritableMemory wmem = null;
try (WritableHandle hand = WritableMemory.allocateDirect(bytes)) {
wmem = hand.getWritable();
// byte[] byteArr = new byte[bytes];
// WritableMemory wmem = WritableMemory.wrap(byteArr);
hllSketch = new HllSketch(lgConfigK, tgtHllType, wmem);
assertTrue(hllSketch.isEmpty());
for (int i = 0; i < n; i++) {
hllSketch.update(i);
}
// println(hllSketch.toString(true, true, false, false));
assertFalse(hllSketch.isEmpty());
assertEquals(hllSketch.getCurMode(), tgtMode);
assertTrue(hllSketch.isMemory());
assertTrue(hllSketch.isOffHeap());
assertTrue(hllSketch.isSameResource(wmem));
// convert direct sketch to byte[]
barr1 = (compact) ? hllSketch.toCompactByteArray() : hllSketch.toUpdatableByteArray();
// println(PreambleUtil.toString(barr1));
hllSketch.reset();
assertTrue(hllSketch.isEmpty());
} catch (final Exception e) {
throw new RuntimeException(e);
}
// println("HEAP");
HllSketch hllSketch2 = new HllSketch(lgConfigK, tgtHllType);
for (int i = 0; i < n; i++) {
hllSketch2.update(i);
}
// println(hllSketch2.toString(true, true, false, false));
// println(PreambleUtil.toString(barr2));
assertEquals(hllSketch2.getCurMode(), tgtMode);
assertFalse(hllSketch2.isMemory());
assertFalse(hllSketch2.isOffHeap());
assertFalse(hllSketch2.isSameResource(wmem));
byte[] barr2 = (compact) ? hllSketch2.toCompactByteArray() : hllSketch2.toUpdatableByteArray();
assertEquals(barr1.length, barr2.length, barr1.length + ", " + barr2.length);
// printDiffs(barr1, barr2);
assertEquals(barr1, barr2);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method newUnion.
private static Union newUnion(int lgK) {
int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgK, TgtHllType.HLL_8);
WritableMemory wmem = WritableMemory.allocate(bytes);
return new Union(lgK, wmem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HllSketchTest method checkCopies.
@Test
public void checkCopies() {
runCheckCopy(14, HLL_4, null);
runCheckCopy(8, HLL_6, null);
runCheckCopy(8, HLL_8, null);
int bytes = getMaxUpdatableSerializationBytes(14, TgtHllType.HLL_8);
WritableMemory wmem = WritableMemory.allocate(bytes);
runCheckCopy(14, HLL_4, wmem);
runCheckCopy(8, HLL_6, wmem);
runCheckCopy(8, HLL_8, wmem);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HllSketchTest method misc.
private static void misc(boolean direct) {
int lgConfigK = 8;
TgtHllType srcType = TgtHllType.HLL_8;
int bytes = getMaxUpdatableSerializationBytes(lgConfigK, srcType);
WritableMemory wmem = WritableMemory.allocate(bytes);
HllSketch sk = (direct) ? new HllSketch(lgConfigK, srcType, wmem) : new HllSketch(lgConfigK, srcType);
// LIST
for (int i = 0; i < 7; i++) {
sk.update(i);
}
AbstractCoupons absCoupons = (AbstractCoupons) sk.hllSketchImpl;
assertEquals(absCoupons.getCouponCount(), 7);
assertEquals(sk.getCompactSerializationBytes(), 36);
assertEquals(sk.getUpdatableSerializationBytes(), 40);
// SET
for (int i = 7; i < 24; i++) {
sk.update(i);
}
absCoupons = (AbstractCoupons) sk.hllSketchImpl;
assertEquals(absCoupons.getCouponCount(), 24);
assertEquals(sk.getCompactSerializationBytes(), 108);
assertEquals(sk.getUpdatableSerializationBytes(), 140);
// HLL
sk.update(24);
AbstractHllArray absHll = (AbstractHllArray) sk.hllSketchImpl;
assertNull(absHll.getAuxIterator());
assertEquals(absHll.getCurMin(), 0);
assertEquals(absHll.getHipAccum(), 25.0, 25 * .02);
assertTrue(absHll.getNumAtCurMin() >= 0);
assertEquals(sk.getUpdatableSerializationBytes(), 40 + 256);
assertEquals(absHll.getMemDataStart(), 40);
assertEquals(absHll.getPreInts(), 10);
final int hllBytes = PreambleUtil.HLL_BYTE_ARR_START + (1 << lgConfigK);
assertEquals(sk.getCompactSerializationBytes(), hllBytes);
assertEquals(getMaxUpdatableSerializationBytes(lgConfigK, TgtHllType.HLL_8), hllBytes);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HllSketchTest method checkCompact.
// Creates either a direct or heap sketch,
// Serializes to either compact or updatable form.
// Confirms the isMemory() for direct, isOffHeap(), and the
// get compact or updatable serialization bytes.
// Returns true if the compact flag is set.
private static boolean checkCompact(int lgK, int n, TgtHllType type, boolean direct, boolean compact) {
int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgK, type);
WritableMemory wmem = WritableMemory.allocate(bytes);
HllSketch sk = (direct) ? new HllSketch(lgK, type, wmem) : new HllSketch(lgK, type);
assertEquals(sk.isMemory(), direct);
assertFalse(sk.isOffHeap());
// LOAD
for (int i = 0; i < n; i++) {
sk.update(i);
}
byte[] byteArr = (compact) ? sk.toCompactByteArray() : sk.toUpdatableByteArray();
int len = byteArr.length;
if (compact) {
assertEquals(len, sk.getCompactSerializationBytes());
} else {
assertEquals(len, sk.getUpdatableSerializationBytes());
}
HllSketch sk2 = HllSketch.wrap(Memory.wrap(byteArr));
assertEquals(sk2.getEstimate(), n, .01);
boolean resourceCompact = sk2.isCompact();
if (resourceCompact) {
try {
HllSketch.writableWrap(WritableMemory.writableWrap(byteArr));
fail();
} catch (SketchesArgumentException e) {
// OK
}
}
return resourceCompact;
// return (byteArr[5] & COMPACT_FLAG_MASK) > 0;
}
Aggregations