use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class CompactOperations method componentsToCompact.
static // No error checking
CompactSketch componentsToCompact(final long thetaLong, final int curCount, final short seedHash, final boolean srcEmpty, final boolean srcCompact, final boolean srcOrdered, final boolean dstOrdered, final WritableMemory dstMem, // may not be compacted, ordered or unordered, may be null
final long[] hashArr) {
final boolean direct = dstMem != null;
final boolean empty = srcEmpty || ((curCount == 0) && (thetaLong == Long.MAX_VALUE));
final boolean single = (curCount == 1) && (thetaLong == Long.MAX_VALUE);
final long[] hashArrOut;
if (!srcCompact) {
hashArrOut = CompactOperations.compactCache(hashArr, curCount, thetaLong, dstOrdered);
} else {
hashArrOut = hashArr;
}
if (!srcOrdered && dstOrdered && !empty && !single) {
Arrays.sort(hashArrOut);
}
// Note: for empty or single we always output the ordered form.
final boolean dstOrderedOut = (empty || single) ? true : dstOrdered;
if (direct) {
final int preLongs = computeCompactPreLongs(empty, curCount, thetaLong);
// always LE
int flags = READ_ONLY_FLAG_MASK | COMPACT_FLAG_MASK;
flags |= empty ? EMPTY_FLAG_MASK : 0;
flags |= dstOrderedOut ? ORDERED_FLAG_MASK : 0;
flags |= single ? SINGLEITEM_FLAG_MASK : 0;
final Memory mem = loadCompactMemory(hashArrOut, seedHash, curCount, thetaLong, dstMem, (byte) flags, preLongs);
return new DirectCompactSketch(mem);
} else {
// Heap
if (empty) {
return EmptyCompactSketch.getInstance();
}
if (single) {
return new SingleItemSketch(hashArrOut[0], seedHash);
}
return new HeapCompactSketch(hashArrOut, empty, seedHash, curCount, thetaLong, dstOrderedOut);
}
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SingleItemSketchTest method siSkWithSiFlag24Bytes.
static Memory siSkWithSiFlag24Bytes() {
// 8 extra bytes
int cap = 24;
WritableMemory wmem = WritableMemory.allocate(cap);
wmem.putLong(0, SiSkPre0WithSiFlag);
wmem.putLong(8, Hash);
return wmem;
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SingleItemSketchTest method buildAndCompact.
@Test
public void buildAndCompact() {
UpdateSketch sk1;
CompactSketch csk;
int bytes;
// On-heap
sk1 = Sketches.updateSketchBuilder().setNominalEntries(32).build();
sk1.update(1);
csk = sk1.compact(true, null);
assertTrue(csk instanceof SingleItemSketch);
csk = sk1.compact(false, null);
assertTrue(csk instanceof SingleItemSketch);
// Off-heap
bytes = Sketches.getMaxUpdateSketchBytes(32);
WritableMemory wmem = WritableMemory.writableWrap(new byte[bytes]);
sk1 = Sketches.updateSketchBuilder().setNominalEntries(32).build(wmem);
sk1.update(1);
csk = sk1.compact(true, null);
assertTrue(csk instanceof SingleItemSketch);
csk = sk1.compact(false, null);
assertTrue(csk instanceof SingleItemSketch);
bytes = Sketches.getMaxCompactSketchBytes(1);
wmem = WritableMemory.writableWrap(new byte[bytes]);
csk = sk1.compact(true, wmem);
assertTrue(csk.isOrdered());
csk = sk1.compact(false, wmem);
assertTrue(csk.isOrdered());
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SingleItemSketchTest method intersection.
@Test
public void intersection() {
UpdateSketch sk1, sk2;
CompactSketch csk;
int bytes;
// Intersection on-heap
sk1 = Sketches.updateSketchBuilder().setNominalEntries(32).build();
sk2 = Sketches.updateSketchBuilder().setNominalEntries(32).build();
sk1.update(1);
sk1.update(2);
sk2.update(1);
Intersection inter = Sketches.setOperationBuilder().buildIntersection();
inter.intersect(sk1);
inter.intersect(sk2);
csk = inter.getResult(true, null);
assertTrue(csk instanceof SingleItemSketch);
// Intersection off-heap
bytes = Sketches.getMaxIntersectionBytes(32);
WritableMemory wmem = WritableMemory.writableWrap(new byte[bytes]);
inter = Sketches.setOperationBuilder().buildIntersection(wmem);
inter.intersect(sk1);
inter.intersect(sk2);
csk = inter.getResult(true, null);
assertTrue(csk instanceof SingleItemSketch);
csk = inter.getResult(false, null);
assertTrue(csk instanceof SingleItemSketch);
}
use of org.apache.datasketches.memory.WritableMemory in project sketches-core by DataSketches.
the class SingleItemSketchTest method checkHeapifyInstance.
@Test
public void checkHeapifyInstance() {
UpdateSketch sk1 = new UpdateSketchBuilder().build();
sk1.update(1);
UpdateSketch sk2 = new UpdateSketchBuilder().build();
sk2.update(1);
Intersection inter = Sketches.setOperationBuilder().buildIntersection();
inter.intersect(sk1);
inter.intersect(sk2);
WritableMemory wmem = WritableMemory.writableWrap(new byte[16]);
CompactSketch csk = inter.getResult(false, wmem);
assertTrue(csk.isOrdered());
Sketch csk2 = Sketches.heapifySketch(wmem);
assertTrue(csk2 instanceof SingleItemSketch);
println(csk2.toString(true, true, 1, true));
}
Aggregations