use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapUnionTest method checkSerVer1Handling.
@Test
public void checkSerVer1Handling() {
//4096
int lgK = 12;
int k = 1 << lgK;
int u1 = 2 * k;
//smaller exact sketch forces early stop
int u2 = 1024;
int totU = u1 + u2;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).build();
UpdateSketch usk2 = UpdateSketch.builder().setNominalEntries(k).build();
//2*k
for (int i = 0; i < u1; i++) usk1.update(i);
//2*k + 1024 no overlap
for (int i = u1; i < totU; i++) usk2.update(i);
WritableMemory skMem1 = WritableMemory.wrap(usk1.compact(true, null).toByteArray());
WritableMemory skMem2 = WritableMemory.wrap(usk2.compact(true, null).toByteArray());
Memory v1mem1 = convertSerV3toSerV1(skMem1);
Memory v1mem2 = convertSerV3toSerV1(skMem2);
Union union = SetOperation.builder().setNominalEntries(k).buildUnion();
union.update(v1mem1);
union.update(v1mem2);
CompactSketch cOut = union.getResult(true, null);
assertEquals(cOut.getEstimate(), totU, .05 * k);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapUnionTest method checkEmptySerVer2and3.
@Test
public //where the granted mem is larger than required
void checkEmptySerVer2and3() {
UpdateSketch usk1 = UpdateSketch.builder().build();
CompactSketch usk1c = usk1.compact(true, null);
byte[] skArr = usk1c.toByteArray();
byte[] skArr2 = Arrays.copyOf(skArr, skArr.length * 2);
WritableMemory v3mem1 = WritableMemory.wrap(skArr2);
Union union = SetOperation.builder().buildUnion();
union.update(v3mem1);
Memory v2mem1 = convertSerV3toSerV2(v3mem1);
WritableMemory v2mem2 = WritableMemory.wrap(new byte[16]);
v2mem1.copyTo(0, v2mem2, 0, 8);
union = SetOperation.builder().buildUnion();
union.update(v2mem2);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapUnionTest 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 union = SetOperation.builder().setNominalEntries(k).buildUnion();
//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 = (Union) SetOperation.heapify(Memory.wrap(union.toByteArray()));
testAllCompactForms(union2, u, 0.05);
union2.reset();
assertEquals(union2.getResult(true, null).getEstimate(), 0.0, 0.0);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class HeapUnionTest method testAllCompactForms.
//used by DirectUnionTest as well
public static void testAllCompactForms(Union union, double expected, double toll) {
double compEst1, compEst2;
//not ordered, no mem
compEst1 = union.getResult(false, null).getEstimate();
assertEquals(compEst1, expected, toll * expected);
//ordered, no mem
CompactSketch comp2 = union.getResult(true, null);
compEst2 = comp2.getEstimate();
assertEquals(compEst2, compEst1, 0.0);
WritableMemory mem = WritableMemory.wrap(new byte[comp2.getCurrentBytes(false)]);
//not ordered, mem
compEst2 = union.getResult(false, mem).getEstimate();
assertEquals(compEst2, compEst1, 0.0);
//ordered, mem
compEst2 = union.getResult(true, mem).getEstimate();
assertEquals(compEst2, compEst1, 0.0);
}
use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.
the class DirectIntersectionTest method checkExceptions1.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkExceptions1() {
int k = 16;
WritableMemory mem = WritableMemory.wrap(new byte[k * 16 + PREBYTES]);
IntersectionImpl.initNewDirectInstance(DEFAULT_UPDATE_SEED, mem);
//corrupt SerVer
mem.putByte(PreambleUtil.SER_VER_BYTE, (byte) 2);
IntersectionImplR.wrapInstance(mem, DEFAULT_UPDATE_SEED);
}
Aggregations