use of org.apache.datasketches.memory.WritableMemory 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.WritableMemory in project sketches-core by DataSketches.
the class DoublesUnionImplTest method checkResultViaMemory.
@Test
public void checkResultViaMemory() {
// empty gadget
final DoublesUnion union = DoublesUnion.builder().build();
// memory too small
WritableMemory mem = WritableMemory.allocate(1);
try {
union.getResult(mem);
fail();
} catch (final SketchesArgumentException e) {
// expected
}
// sufficient memory
mem = WritableMemory.allocate(8);
DoublesSketch result = union.getResult(mem);
assertTrue(result.isEmpty());
final int k = 128;
final int n = 1392;
mem = WritableMemory.allocate(DoublesSketch.getUpdatableStorageBytes(k, n));
final DoublesSketch qs = buildAndLoadQS(k, n);
union.update(qs);
result = union.getResult(mem);
DoublesSketchTest.testSketchEquality(result, qs);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class PreambleUtilTest method checkCorruptMemoryInput.
@SuppressWarnings("unused")
@Test
public void checkCorruptMemoryInput() {
HllSketch sk = new HllSketch(12);
byte[] memObj = sk.toCompactByteArray();
WritableMemory wmem = WritableMemory.writableWrap(memObj);
long memAdd = wmem.getCumulativeOffset(0);
HllSketch bad;
// checkFamily
try {
// corrupt, should be 7
wmem.putByte(FAMILY_BYTE, (byte) 0);
bad = HllSketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
/* OK */
}
// corrected
insertFamilyId(wmem);
// check SerVer
try {
// corrupt, should be 1
wmem.putByte(SER_VER_BYTE, (byte) 0);
bad = HllSketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
/* OK */
}
// corrected
insertSerVer(wmem);
// check bad PreInts
try {
// corrupt, should be 2
insertPreInts(wmem, 0);
bad = HllSketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
/* OK */
}
// corrected
insertPreInts(wmem, 2);
// check wrong PreInts and LIST
try {
// corrupt, should be 2
insertPreInts(wmem, 3);
bad = HllSketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
/* OK */
}
// corrected
insertPreInts(wmem, 2);
// move to Set mode
for (int i = 1; i <= 15; i++) {
sk.update(i);
}
memObj = sk.toCompactByteArray();
wmem = WritableMemory.writableWrap(memObj);
memAdd = wmem.getCumulativeOffset(0);
// check wrong PreInts and SET
try {
// corrupt, should be 3
insertPreInts(wmem, 2);
bad = HllSketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
/* OK */
}
// corrected
insertPreInts(wmem, 3);
// move to HLL mode
for (int i = 15; i <= 1000; i++) {
sk.update(i);
}
memObj = sk.toCompactByteArray();
wmem = WritableMemory.writableWrap(memObj);
memAdd = wmem.getCumulativeOffset(0);
// check wrong PreInts and HLL
try {
// corrupt, should be 10
insertPreInts(wmem, 2);
bad = HllSketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
/* OK */
}
// corrected
insertPreInts(wmem, 10);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class PreambleUtilTest method checkExtractFlags.
@SuppressWarnings("unused")
@Test
public void checkExtractFlags() {
int bytes = HllSketch.getMaxUpdatableSerializationBytes(4, TgtHllType.HLL_4);
WritableMemory wmem = WritableMemory.allocate(bytes);
Object memObj = wmem.getArray();
long memAdd = wmem.getCumulativeOffset(0L);
HllSketch sk = new HllSketch(4, TgtHllType.HLL_4, wmem);
int flags = extractFlags(wmem);
assertEquals(flags, EMPTY_FLAG_MASK);
}
use of org.apache.datasketches.memory.WritableMemory 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);
}
Aggregations