use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest method checkHQStoCompactEmptyForms.
@Test
public void checkHQStoCompactEmptyForms() {
int k = 512;
UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X2).setNominalEntries(k).build();
println("lgArr: " + usk.getLgArrLongs());
// empty
println(usk.toString(false, true, 0, false));
boolean estimating = false;
assertEquals(usk.getClass().getSimpleName(), "HeapQuickSelectSketch");
double uskEst = usk.getEstimate();
double uskLB = usk.getLowerBound(2);
double uskUB = usk.getUpperBound(2);
int currentUSBytes = usk.getCurrentBytes();
// clumsy, but a function of RF and TCF
assertEquals(currentUSBytes, (32 * 8) + 24);
// compact form
int compBytes = usk.getCompactBytes();
assertEquals(compBytes, 8);
assertEquals(usk.isEstimationMode(), estimating);
byte[] arr2 = new byte[compBytes];
WritableMemory mem2 = WritableMemory.writableWrap(arr2);
CompactSketch csk2 = usk.compact(false, mem2);
assertEquals(csk2.getEstimate(), uskEst);
assertEquals(csk2.getLowerBound(2), uskLB);
assertEquals(csk2.getUpperBound(2), uskUB);
assertEquals(csk2.isEmpty(), true);
assertEquals(csk2.isEstimationMode(), estimating);
assertEquals(csk2.getClass().getSimpleName(), "DirectCompactSketch");
CompactSketch csk3 = usk.compact(true, mem2);
println(csk3.toString(false, true, 0, false));
println(csk3.toString());
assertEquals(csk3.getEstimate(), uskEst);
assertEquals(csk3.getLowerBound(2), uskLB);
assertEquals(csk3.getUpperBound(2), uskUB);
assertEquals(csk3.isEmpty(), true);
assertEquals(csk3.isEstimationMode(), estimating);
assertEquals(csk3.getClass().getSimpleName(), "DirectCompactSketch");
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SetOpsCornerCasesTest method cornerCaseChecksMemory.
// @Test
// public void checkExactNullSpecificCase() {
// cornerCaseChecksMemory(State.EXACT, State.NULL, 64);
// }
private static void cornerCaseChecksMemory(State stateA, State stateB, int k) {
println("StateA: " + stateA + ", StateB: " + stateB);
CompactSketch tcskA = generate(stateA, k);
CompactSketch tcskB = generate(stateB, k);
WritableMemory wmem = WritableMemory.allocate(SetOperation.getMaxUnionBytes(k));
CompactSketch rcskStdU = doStdUnion(tcskA, tcskB, k, null);
CompactSketch rcskPwU = doPwUnion(tcskA, tcskB, k);
// heap, heap
checkCornerCase(rcskPwU, rcskStdU);
rcskStdU = doStdUnion(tcskA, tcskB, k, wmem);
CompactSketch rcskStdPairU = doStdPairUnion(tcskA, tcskB, k, wmem);
// direct, direct
checkCornerCase(rcskStdPairU, rcskStdU);
wmem = WritableMemory.allocate(SetOperation.getMaxIntersectionBytes(k));
CompactSketch rcskStdI = doStdIntersection(tcskA, tcskB, null);
CompactSketch rcskPwI = doPwIntersection(tcskA, tcskB);
// empty, empty
checkCornerCase(rcskPwI, rcskStdI);
rcskStdI = doStdIntersection(tcskA, tcskB, wmem);
CompactSketch rcskStdPairI = doStdPairIntersection(tcskA, tcskB, wmem);
// empty, empty //direct, direct???
checkCornerCase(rcskStdPairI, rcskStdI);
wmem = WritableMemory.allocate(SetOperation.getMaxAnotBResultBytes(k));
CompactSketch rcskStdAnotB = doStdAnotB(tcskA, tcskB, null);
CompactSketch rcskPwAnotB = doPwAnotB(tcskA, tcskB);
// heap, heap
checkCornerCase(rcskPwAnotB, rcskStdAnotB);
rcskStdAnotB = doStdAnotB(tcskA, tcskB, wmem);
CompactSketch rcskStdStatefulAnotB = doStdStatefulAnotB(tcskA, tcskB, wmem);
// direct, heap
checkCornerCase(rcskStdStatefulAnotB, rcskStdAnotB);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SketchTest method createCompactSketchMemory.
private static WritableMemory createCompactSketchMemory(int k, int u) {
UpdateSketch usk = Sketches.updateSketchBuilder().setNominalEntries(k).build();
for (int i = 0; i < u; i++) {
usk.update(i);
}
int bytes = Sketch.getMaxCompactSketchBytes(usk.getRetainedEntries(true));
WritableMemory wmem = WritableMemory.allocate(bytes);
usk.compact(true, wmem);
return wmem;
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SketchTest method checkCompactSizeAndFlagsOnHeapify.
@Test
public void checkCompactSizeAndFlagsOnHeapify() {
WritableMemory wmem = createCompactSketchMemory(16, 32);
Sketch sk = Sketch.heapify(wmem);
assertTrue(sk instanceof CompactSketch);
int flags = PreambleUtil.extractFlags(wmem);
int flagsNoCompact = flags & ~READ_ONLY_FLAG_MASK;
PreambleUtil.insertFlags(wmem, flagsNoCompact);
try {
sk = Sketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
}
wmem = WritableMemory.allocate(7);
PreambleUtil.insertSerVer(wmem, 3);
// PreambleUtil.insertFamilyID(wmem, 3);
try {
sk = Sketch.heapify(wmem);
fail();
} catch (SketchesArgumentException e) {
}
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SketchTest method checkSerVer.
@SuppressWarnings("static-access")
@Test
public void checkSerVer() {
UpdateSketch sketch = UpdateSketch.builder().setNominalEntries(1024).build();
byte[] sketchArray = sketch.toByteArray();
Memory mem = Memory.wrap(sketchArray);
int serVer = Sketch.getSerializationVersion(mem);
assertEquals(serVer, 3);
WritableMemory wmem = WritableMemory.writableWrap(sketchArray);
UpdateSketch sk2 = UpdateSketch.wrap(wmem);
serVer = sk2.getSerializationVersion(wmem);
assertEquals(serVer, 3);
}
Aggregations