use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkExactUnionWithOverlap.
@Test
public void checkExactUnionWithOverlap() {
//512
int lgK = 9;
int k = 1 << lgK;
int u = k;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
//256
for (int i = 0; i < u / 2; i++) usk1.update(i);
//512, 256 overlapped
for (int i = 0; i < u; i++) usk2.update(i);
//exact, overlapped
assertEquals(u, usk1.getEstimate() + usk2.getEstimate() / 2, 0.0);
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
//update with heap UpdateSketch
union.update(usk1);
//update with heap UpdateSketch
union.update(usk2);
testAllCompactForms(union, u, 0.0);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkPrimitiveUpdates.
@Test
public void checkPrimitiveUpdates() {
int k = 32;
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
union.update(1L);
//#1 double
union.update(1.5);
union.update(0.0);
union.update(-0.0);
String s = null;
//null string
union.update(s);
s = "";
//empty string
union.update(s);
s = "String";
//#2 actual string
union.update(s);
byte[] byteArr = null;
//null byte[]
union.update(byteArr);
byteArr = new byte[0];
//empty byte[]
union.update(byteArr);
byteArr = "Byte Array".getBytes(UTF_8);
//#3 actual byte[]
union.update(byteArr);
int[] intArr = null;
//null int[]
union.update(intArr);
intArr = new int[0];
//empty int[]
union.update(intArr);
int[] intArr2 = { 1, 2, 3, 4, 5 };
//#4 actual int[]
union.update(intArr2);
long[] longArr = null;
//null long[]
union.update(longArr);
longArr = new long[0];
//empty long[]
union.update(longArr);
long[] longArr2 = { 6, 7, 8, 9 };
//#5 actual long[]
union.update(longArr2);
CompactSketch comp = union.getResult();
double est = comp.getEstimate();
boolean empty = comp.isEmpty();
assertEquals(est, 7.0, 0.0);
assertFalse(empty);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkHeapifyExact.
@Test
public void checkHeapifyExact() {
//512
int lgK = 9;
int k = 1 << lgK;
int u = k;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
//256
for (int i = 0; i < u / 2; i++) usk1.update(i);
//256 no overlap
for (int i = u / 2; i < u; i++) usk2.update(i);
//exact, no overlap
assertEquals(u, usk1.getEstimate() + usk2.getEstimate(), 0.0);
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
//update with heap UpdateSketch
union.update(usk1);
//update with heap UpdateSketch
union.update(usk2);
testAllCompactForms(union, u, 0.0);
Union union2 = (Union) SetOperation.heapify(WritableMemory.wrap(union.toByteArray()));
testAllCompactForms(union2, u, 0.0);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkGetResult.
@Test
public void checkGetResult() {
int k = 1024;
UpdateSketch sk = Sketches.updateSketchBuilder().build();
int memBytes = getMaxUnionBytes(k);
byte[] memArr = new byte[memBytes];
WritableMemory iMem = WritableMemory.wrap(memArr);
Union union = Sketches.setOperationBuilder().setNominalEntries(k).buildUnion(iMem);
union.update(sk);
CompactSketch csk = union.getResult();
assertEquals(csk.getCurrentBytes(true), 8);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectUnionTest method checkWrapEstNoOverlapOrderedDirectIn.
@Test
public void checkWrapEstNoOverlapOrderedDirectIn() {
//4096
int lgK = 12;
int k = 1 << lgK;
int u = 4 * k;
//2k estimating
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
//2k exact for early stop test
UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(2 * k).build();
//2k estimating
for (int i = 0; i < u / 2; i++) usk1.update(i);
//2k no overlap, exact, will force early stop
for (int i = u / 2; i < u; i++) usk2.update(i);
WritableMemory cskMem2 = WritableMemory.wrap(new byte[usk2.getCurrentBytes(true)]);
//ordered, loads the cskMem2 as ordered
CompactSketch cosk2 = usk2.compact(true, cskMem2);
//union memory
WritableMemory uMem = WritableMemory.wrap(new byte[getMaxUnionBytes(k)]);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion(uMem);
//updates with heap UpdateSketch
union.update(usk1);
//updates with direct CompactSketch, ordered, use early stop
union.update(cosk2);
UpdateSketch emptySketch = UpdateSketch.builder().setNominalEntries(k).build();
//updates with empty sketch
union.update(emptySketch);
emptySketch = null;
//updates with null sketch
union.update(emptySketch);
testAllCompactForms(union, u, 0.05);
Union union2 = Sketches.wrapUnion(WritableMemory.wrap(union.toByteArray()));
testAllCompactForms(union2, u, 0.05);
union2.reset();
assertEquals(union2.getResult(true, null).getEstimate(), 0.0, 0.0);
}
Aggregations