use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkDirectWrap.
// Special DirectUnion cases
// Himanshu's issue
@Test
public void checkDirectWrap() {
final int nomEntries = 16;
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(nomEntries)]);
SetOperation.builder().setNominalEntries(nomEntries).buildUnion(uMem);
final UpdateSketch sk1 = UpdateSketch.builder().setNominalEntries(nomEntries).build();
sk1.update("a");
sk1.update("b");
final UpdateSketch sk2 = UpdateSketch.builder().setNominalEntries(nomEntries).build();
sk2.update("c");
sk2.update("d");
Union union = Sketches.wrapUnion(uMem);
union.union(sk1);
union = Sketches.wrapUnion(uMem);
union.union(sk2);
final CompactSketch sketch = union.getResult(true, null);
assertEquals(4.0, sketch.getEstimate(), 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkEmptySerVer2and3.
@Test
public void checkEmptySerVer2and3() {
// 4096
final int lgK = 12;
final int k = 1 << lgK;
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
final CompactSketch usk1c = usk1.compact(true, null);
final byte[] skArr = usk1c.toByteArray();
final byte[] skArr2 = Arrays.copyOf(skArr, skArr.length * 2);
final WritableMemory v3mem1 = WritableMemory.writableWrap(skArr2);
// union memory
WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.union(v3mem1);
final Memory v2mem1 = convertSerVer3toSerVer2(usk1c, Util.DEFAULT_UPDATE_SEED);
final WritableMemory v2mem2 = WritableMemory.writableWrap(new byte[16]);
v2mem1.copyTo(0, v2mem2, 0, 8);
// union memory
uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.union(v2mem2);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkEmptyUnionCompactResult.
@Test
public void checkEmptyUnionCompactResult() {
final int k = 64;
// union memory
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
final WritableMemory mem = WritableMemory.writableWrap(new byte[Sketch.getMaxCompactSketchBytes(0)]);
// DirectCompactSketch
final CompactSketch csk = union.getResult(false, mem);
assertTrue(csk.isEmpty());
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkHeapifyExact.
@Test
public void checkHeapifyExact() {
// 512
final int lgK = 9;
final int k = 1 << lgK;
final int u = k;
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
for (int i = 0; i < u / 2; i++) {
// 256
usk1.update(i);
}
for (int i = u / 2; i < u; i++) {
// 256 no overlap
usk2.update(i);
}
// exact, no overlap
assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
// update with heap UpdateSketch
union.union(usk1);
// update with heap UpdateSketch
union.union(usk2);
testAllCompactForms(union, u, 0.0);
final Union union2 = (Union) SetOperation.heapify(WritableMemory.writableWrap(union.toByteArray()));
testAllCompactForms(union2, u, 0.0);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkWrapEstNoOverlapUnorderedMemIn.
@Test
public void checkWrapEstNoOverlapUnorderedMemIn() {
// 4096
final int lgK = 12;
final int k = 1 << lgK;
final int u = 4 * k;
// 2k estimating
final UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
// 2k exact for early stop test
final UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(2 * k).build();
for (int i = 0; i < u / 2; i++) {
// 2k estimating
usk1.update(i);
}
for (int i = u / 2; i < u; i++) {
// 2k no overlap, exact, will force early stop
usk2.update(i);
}
final WritableMemory cskMem2 = WritableMemory.writableWrap(new byte[usk2.getCompactBytes()]);
// unordered, loads the cskMem2 as unordered
usk2.compact(false, cskMem2);
// union memory
final WritableMemory uMem = WritableMemory.writableWrap(new byte[getMaxUnionBytes(k)]);
final Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
// updates with heap UpdateSketch
union.union(usk1);
// updates with direct CompactSketch, ordered, use early stop
union.union(cskMem2);
UpdateSketch emptySketch = UpdateSketch.builder().setNominalEntries(k).build();
// updates with empty sketch
union.union(emptySketch);
emptySketch = null;
// updates with null sketch
union.union(emptySketch);
testAllCompactForms(union, u, 0.05);
final Union union2 = Sketches.wrapUnion(WritableMemory.writableWrap(union.toByteArray()));
testAllCompactForms(union2, u, 0.05);
union2.reset();
assertEquals(union2.getResult(true, null).getEstimate(), 0.0, 0.0);
}
Aggregations