use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class Sketch method wrap.
/**
* Wrap takes the sketch image in the given Memory and refers to it directly.
* There is no data copying onto the java heap.
* The wrap operation enables fast read-only merging and access to all the public read-only API.
*
* <p>Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
* been explicitly stored as direct sketches can be wrapped.
* Wrapping earlier serial version sketches will result in a on-heap CompactSketch
* where all data will be copied to the heap. These early versions were never designed to
* "wrap".</p>
*
* <p>Wrapping any subclass of this class that is empty or contains only a single item will
* result in on-heap equivalent forms of empty and single item sketch respectively.
* This is actually faster and consumes less overall memory.</p>
*
* <p>For Update and Compact Sketches this method checks if the given expectedSeed was used to
* create the source Memory image. However, SerialVersion 1 sketches cannot be checked.</p>
*
* @param srcMem an image of a Sketch.
* <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
* @param expectedSeed the seed used to validate the given Memory image.
* <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
* @return a UpdateSketch backed by the given Memory except as above.
*/
public static Sketch wrap(final Memory srcMem, final long expectedSeed) {
final int preLongs = srcMem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
final Family family = Family.idToFamily(familyID);
if (family == Family.QUICKSELECT) {
if (serVer == 3 && preLongs == 3) {
return DirectQuickSelectSketchR.readOnlyWrap(srcMem, expectedSeed);
} else {
throw new SketchesArgumentException("Corrupted: " + family + " family image: must have SerVer = 3 and preLongs = 3");
}
}
if (family == Family.COMPACT) {
return CompactSketch.wrap(srcMem, expectedSeed);
}
throw new SketchesArgumentException("Cannot wrap family: " + family + " as a Sketch");
}
use of org.apache.datasketches.SketchesArgumentException 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.SketchesArgumentException in project sketches-core by DataSketches.
the class Sketch method wrap.
/**
* Wrap takes the sketch image in the given Memory and refers to it directly.
* There is no data copying onto the java heap.
* The wrap operation enables fast read-only merging and access to all the public read-only API.
*
* <p>Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
* been explicitly stored as direct sketches can be wrapped.
* Wrapping earlier serial version sketches will result in a on-heap CompactSketch
* where all data will be copied to the heap. These early versions were never designed to
* "wrap".</p>
*
* <p>Wrapping any subclass of this class that is empty or contains only a single item will
* result in on-heap equivalent forms of empty and single item sketch respectively.
* This is actually faster and consumes less overall memory.</p>
*
* <p>For Update Sketches this method checks if the
* <a href="{@docRoot}/resources/dictionary.html#defaultUpdateSeed">Default Update Seed</a></p>
* was used to create the source Memory image.
*
* <p>For Compact Sketches this method assumes that the sketch image was created with the
* correct hash seed, so it is not checked.</p>
*
* @param srcMem an image of a Sketch.
* <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>.
* @return a Sketch backed by the given Memory
*/
public static Sketch wrap(final Memory srcMem) {
final int preLongs = srcMem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
final Family family = Family.idToFamily(familyID);
if (family == Family.QUICKSELECT) {
if (serVer == 3 && preLongs == 3) {
return DirectQuickSelectSketchR.readOnlyWrap(srcMem, DEFAULT_UPDATE_SEED);
} else {
throw new SketchesArgumentException("Corrupted: " + family + " family image: must have SerVer = 3 and preLongs = 3");
}
}
if (family == Family.COMPACT) {
return CompactSketch.wrap(srcMem);
}
throw new SketchesArgumentException("Cannot wrap family: " + family + " as a Sketch");
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class CompactSketch method wrap.
/**
* Wrap takes the sketch image in the given Memory and refers to it directly.
* There is no data copying onto the java heap.
* The wrap operation enables fast read-only merging and access to all the public read-only API.
*
* <p>Only "Direct" Serialization Version 3 (i.e, OpenSource) sketches that have
* been explicitly stored as direct sketches can be wrapped.
* Wrapping earlier serial version sketches will result in a heapify operation.
* These early versions were never designed to "wrap".</p>
*
* <p>Wrapping any subclass of this class that is empty or contains only a single item will
* result in heapified forms of empty and single item sketch respectively.
* This is actually faster and consumes less overall memory.</p>
*
* <p>This method checks if the given expectedSeed was used to create the source Memory image.
* However, SerialVersion 1 sketches cannot be checked as they don't have a seedHash field,
* so the resulting heapified CompactSketch will be given the hash of the expectedSeed.</p>
*
* @param srcMem an image of a Sketch that was created using the given expectedSeed.
* <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
* @param expectedSeed the seed used to validate the given Memory image.
* <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
* @return a CompactSketch backed by the given Memory except as above.
*/
public static CompactSketch wrap(final Memory srcMem, final long expectedSeed) {
final int serVer = srcMem.getByte(SER_VER_BYTE) & 0XFF;
final int familyID = srcMem.getByte(FAMILY_BYTE) & 0XFF;
final Family family = Family.idToFamily(familyID);
if (family != Family.COMPACT) {
throw new IllegalArgumentException("Corrupted: " + family + " is not Compact!");
}
final short seedHash = Util.computeSeedHash(expectedSeed);
if (serVer == 3) {
if (PreambleUtil.isEmptyFlag(srcMem)) {
return EmptyCompactSketch.getHeapInstance(srcMem);
}
if (otherCheckForSingleItem(srcMem)) {
// SINGLEITEM?
return SingleItemSketch.heapify(srcMem, seedHash);
}
// not empty & not singleItem
final int flags = srcMem.getByte(FLAGS_BYTE);
final boolean compactFlag = (flags & COMPACT_FLAG_MASK) > 0;
if (!compactFlag) {
throw new SketchesArgumentException("Corrupted: COMPACT family sketch image must have compact flag set");
}
final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0;
if (!readOnly) {
throw new SketchesArgumentException("Corrupted: COMPACT family sketch image must have Read-Only flag set");
}
return DirectCompactSketch.wrapInstance(srcMem, seedHash);
} else // end of serVer 3
if (serVer == 1) {
return ForwardCompatibility.heapify1to3(srcMem, seedHash);
} else if (serVer == 2) {
return ForwardCompatibility.heapify2to3(srcMem, seedHash);
}
throw new SketchesArgumentException("Corrupted: Serialization Version " + serVer + " not recognized.");
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class CompactSketch method heapify.
/**
* Heapify takes a CompactSketch image in Memory and instantiates an on-heap CompactSketch.
*
* <p>The resulting sketch will not retain any link to the source Memory and all of its data will be
* copied to the heap CompactSketch.</p>
*
* <p>This method checks if the given expectedSeed was used to create the source Memory image.
* However, SerialVersion 1 sketch images cannot be checked as they don't have a seedHash field,
* so the resulting heapified CompactSketch will be given the hash of the expectedSeed.</p>
*
* @param srcMem an image of a CompactSketch that was created using the given expectedSeed.
* <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>.
* @param expectedSeed the seed used to validate the given Memory image.
* <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a>.
* @return a CompactSketch on the heap.
*/
public static CompactSketch heapify(final Memory srcMem, final long expectedSeed) {
final int serVer = srcMem.getByte(SER_VER_BYTE);
final byte familyID = srcMem.getByte(FAMILY_BYTE);
final Family family = idToFamily(familyID);
if (family != Family.COMPACT) {
throw new IllegalArgumentException("Corrupted: " + family + " is not Compact!");
}
if (serVer == 3) {
final int flags = PreambleUtil.extractFlags(srcMem);
final boolean srcOrdered = (flags & ORDERED_FLAG_MASK) != 0;
final boolean empty = (flags & EMPTY_FLAG_MASK) != 0;
if (!empty) {
PreambleUtil.checkMemorySeedHash(srcMem, expectedSeed);
}
return CompactOperations.memoryToCompact(srcMem, srcOrdered, null);
}
// not SerVer 3, assume compact stored form
final short seedHash = Util.computeSeedHash(expectedSeed);
if (serVer == 1) {
return ForwardCompatibility.heapify1to3(srcMem, seedHash);
}
if (serVer == 2) {
return ForwardCompatibility.heapify2to3(srcMem, seedHash);
}
throw new SketchesArgumentException("Unknown Serialization Version: " + serVer);
}
Aggregations