use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ArrayOfStringsSummaryTest method checkInBytes.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkInBytes() {
Memory mem = Memory.wrap(new byte[100]);
ArrayOfStringsSummary.checkInBytes(mem, 200);
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class Sketch method checkSketchAndMemoryFlags.
/**
* Checks Ordered and Compact flags for integrity between sketch and Memory
* @param sketch the given sketch
*/
static final void checkSketchAndMemoryFlags(final Sketch sketch) {
final Memory mem = sketch.getMemory();
if (mem == null) {
return;
}
final int flags = PreambleUtil.extractFlags(mem);
if ((flags & COMPACT_FLAG_MASK) > 0 ^ sketch.isCompact()) {
throw new SketchesArgumentException("Possible corruption: " + "Memory Compact Flag inconsistent with Sketch");
}
if ((flags & ORDERED_FLAG_MASK) > 0 ^ sketch.isOrdered()) {
throw new SketchesArgumentException("Possible corruption: " + "Memory Ordered Flag inconsistent with Sketch");
}
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ReservoirItemsUnion method heapify.
/**
* Instantiates a Union from Memory
*
* @param <T> The type of item this sketch contains
* @param srcMem Memory object containing a serialized union
* @param serDe An instance of ArrayOfItemsSerDe
* @return A ReservoirItemsUnion created from the provided Memory
*/
public static <T> ReservoirItemsUnion<T> heapify(final Memory srcMem, final ArrayOfItemsSerDe<T> serDe) {
Family.RESERVOIR_UNION.checkFamilyID(srcMem.getByte(FAMILY_BYTE));
final int numPreLongs = extractPreLongs(srcMem);
final int serVer = extractSerVer(srcMem);
final boolean isEmpty = (extractFlags(srcMem) & EMPTY_FLAG_MASK) != 0;
int maxK = extractMaxK(srcMem);
final boolean preLongsEqMin = (numPreLongs == Family.RESERVOIR_UNION.getMinPreLongs());
final boolean preLongsEqMax = (numPreLongs == Family.RESERVOIR_UNION.getMaxPreLongs());
if (!preLongsEqMin & !preLongsEqMax) {
throw new SketchesArgumentException("Possible corruption: Non-empty union with only " + Family.RESERVOIR_UNION.getMinPreLongs() + "preLongs");
}
if (serVer != SER_VER) {
if (serVer == 1) {
final short encMaxK = extractEncodedReservoirSize(srcMem);
maxK = ReservoirSize.decodeValue(encMaxK);
} else {
throw new SketchesArgumentException("Possible Corruption: Ser Ver must be " + SER_VER + ": " + serVer);
}
}
final ReservoirItemsUnion<T> riu = new ReservoirItemsUnion<>(maxK);
if (!isEmpty) {
final int preLongBytes = numPreLongs << 3;
final Memory sketchMem = srcMem.region(preLongBytes, srcMem.getCapacity() - preLongBytes);
riu.update(sketchMem, serDe);
}
return riu;
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class ReservoirLongsUnion method heapify.
/**
* Instantiates a Union from Memory
*
* @param srcMem Memory object containing a serialized union
* @return A ReservoirLongsUnion created from the provided Memory
*/
public static ReservoirLongsUnion heapify(final Memory srcMem) {
Family.RESERVOIR_UNION.checkFamilyID(srcMem.getByte(FAMILY_BYTE));
final int numPreLongs = extractPreLongs(srcMem);
final int serVer = extractSerVer(srcMem);
final boolean isEmpty = (extractFlags(srcMem) & EMPTY_FLAG_MASK) != 0;
int maxK = extractMaxK(srcMem);
final boolean preLongsEqMin = (numPreLongs == Family.RESERVOIR_UNION.getMinPreLongs());
final boolean preLongsEqMax = (numPreLongs == Family.RESERVOIR_UNION.getMaxPreLongs());
if (!preLongsEqMin & !preLongsEqMax) {
throw new SketchesArgumentException("Possible corruption: Non-empty union with only " + Family.RESERVOIR_UNION.getMinPreLongs() + "preLongs");
}
if (serVer != SER_VER) {
if (serVer == 1) {
final short encMaxK = extractEncodedReservoirSize(srcMem);
maxK = ReservoirSize.decodeValue(encMaxK);
} else {
throw new SketchesArgumentException("Possible Corruption: Ser Ver must be " + SER_VER + ": " + serVer);
}
}
final ReservoirLongsUnion rlu = new ReservoirLongsUnion(maxK);
if (!isEmpty) {
final int preLongBytes = numPreLongs << 3;
final Memory sketchMem = srcMem.region(preLongBytes, srcMem.getCapacity() - preLongBytes);
rlu.update(sketchMem);
}
return rlu;
}
use of org.apache.datasketches.memory.Memory in project sketches-core by DataSketches.
the class DoublesSketch method putMemory.
/**
* Puts the current sketch into the given Memory if there is sufficient space, otherwise,
* throws an error.
*
* @param dstMem the given memory.
* @param compact if true, compacts and sorts the base buffer, which optimizes merge
* performance at the cost of slightly increased serialization time.
*/
public void putMemory(final WritableMemory dstMem, final boolean compact) {
if (isDirect() && isCompact() == compact) {
final Memory srcMem = getMemory();
srcMem.copyTo(0, dstMem, 0, getStorageBytes());
} else {
final byte[] byteArr = toByteArray(compact);
final int arrLen = byteArr.length;
final long memCap = dstMem.getCapacity();
if (memCap < arrLen) {
throw new SketchesArgumentException("Destination Memory not large enough: " + memCap + " < " + arrLen);
}
dstMem.putByteArray(0, byteArr, 0, arrLen);
}
}
Aggregations