use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class ReservoirItemsSketchTest method checkBadFamily.
@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadFamily() {
final WritableMemory mem = getBasicSerializedLongsRIS();
// corrupt the family ID
mem.putByte(FAMILY_BYTE, (byte) Family.ALPHA.getID());
try {
PreambleUtil.preambleToString(mem);
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().startsWith("Inspecting preamble with Sampling family"));
}
ReservoirItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkCorruptSerializedWeight.
@Test
public void checkCorruptSerializedWeight() {
final VarOptItemsSketch<String> vis = VarOptItemsSketch.newInstance(24);
for (int i = 1; i < 10; ++i) {
vis.update(Integer.toString(i), i);
}
final byte[] sketchBytes = vis.toByteArray(new ArrayOfStringsSerDe(), String.class);
final WritableMemory mem = WritableMemory.writableWrap(sketchBytes);
// weights will be stored in the first double after the preamble
final int numPreLongs = PreambleUtil.extractPreLongs(mem);
final int weightOffset = numPreLongs << 3;
// inject a negative weight
mem.putDouble(weightOffset, -1.25);
try {
VarOptItemsSketch.heapify(mem, new ArrayOfStringsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: Non-positive weight in " + "heapify(): -1.25"));
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkMalformedPreamble.
@Test
public void checkMalformedPreamble() {
final int k = 50;
final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(k, k);
final byte[] sketchBytes = sketch.toByteArray(new ArrayOfLongsSerDe());
final Memory srcMem = Memory.wrap(sketchBytes);
// we'll use the same initial sketch a few times, so grab a copy of it
final byte[] copyBytes = new byte[sketchBytes.length];
final WritableMemory mem = WritableMemory.writableWrap(copyBytes);
// copy the bytes
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_PRELONGS_WARMUP);
// no items in R but max preLongs
try {
PreambleUtil.insertPreLongs(mem, Family.VAROPT.getMaxPreLongs());
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().startsWith("Possible Corruption: " + Family.VAROPT.getMaxPreLongs() + " preLongs but"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_PRELONGS_WARMUP);
// negative H region count
try {
PreambleUtil.insertHRegionItemCount(mem, -1);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: H region count cannot be negative: -1"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractHRegionItemCount(mem), k);
// negative R region count
try {
PreambleUtil.insertRRegionItemCount(mem, -128);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: R region count cannot be negative: -128"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractRRegionItemCount(mem), 0);
// invalid k < 1
try {
PreambleUtil.insertK(mem, 0);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: k must be at least 1: 0"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractK(mem), k);
// invalid n < 0
try {
PreambleUtil.insertN(mem, -1024);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: n cannot be negative: -1024"));
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class DirectUpdateDoublesSketchTest method variousExceptions.
@SuppressWarnings("unused")
@Test
public void variousExceptions() {
WritableMemory mem = WritableMemory.writableWrap(new byte[8]);
try {
int flags = PreambleUtil.COMPACT_FLAG_MASK;
DirectUpdateDoublesSketchR.checkCompact(2, 0);
fail();
}// OK
catch (SketchesArgumentException e) {
}
try {
int flags = PreambleUtil.COMPACT_FLAG_MASK;
DirectUpdateDoublesSketchR.checkCompact(3, flags);
fail();
}// OK
catch (SketchesArgumentException e) {
}
try {
DirectUpdateDoublesSketchR.checkPreLongs(3);
fail();
}// OK
catch (SketchesArgumentException e) {
}
try {
DirectUpdateDoublesSketchR.checkPreLongs(0);
fail();
}// OK
catch (SketchesArgumentException e) {
}
try {
DirectUpdateDoublesSketchR.checkDirectFlags(PreambleUtil.COMPACT_FLAG_MASK);
fail();
}// OK
catch (SketchesArgumentException e) {
}
try {
DirectUpdateDoublesSketchR.checkEmptyAndN(true, 1);
fail();
}// OK
catch (SketchesArgumentException e) {
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class BackwardConversions method convertSerVer3toSerVer1.
/**
* Converts a SerVer3 ordered, heap CompactSketch to a SerVer1 ordered, SetSketch in Memory.
* This is exclusively for testing purposes.
*
* <p>V1 dates from roughly Aug 2014 to about May 2015.
* The library at that time had an early Theta sketch with set operations based on ByteBuffer,
* the Alpha sketch, and an early HLL sketch. It also had an early adaptor for Pig.
* It also had code for the even earlier CountUniqueSketch (for backward compatibility),
* which was the bucket sketch based on Giroire.
*
* <p><b>Serialization Version 1:</b></p>
* <pre>
* Long || Start Byte Adr:
* Adr:
* || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* 0 || | Flags | LgResize | LgArr | lgNom | SkType | SerVer | MD_LONGS |
*
* || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
* 1 || | ------------CurCount-------------- |
*
* || 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
* 2 || --------------------------THETA_LONG------------------------------ |
*
* || | 24 |
* 3 || ----------------------Start of Long Array------------------------ |
* </pre>
*
* <ul>
* <li>The serialization for V1 was always to a compact form (no hash table spaces).</li>
* <li><i>MD_LONGS</i> (Metadata Longs, now Preamble Longs) was always 3.</li>
* <li><i>SerVer</i> is always 1.</li>
* <li>The <i>SkType</i> had three values: 1,2,3 for Alpha, QuickSelect, and SetSketch,
* respectively.</li>
* <li>Bytes <i>lgNom</i> and <i>lgArr</i> were only used by the QS and Alpha sketches.</li>
* <li>V1 <i>LgResize</i> (2 bits) was only relevant to the Alpha and QS sketches.</li>
* <li>The flags byte is in byte 6 (moved to 5 in V2).</li>
* <li>The only flag bits are BE(bit0)=0, and Read-Only(bit1)=1. Read-only was only set for the
* SetSketch.</li>
* <li>There is no seedHash.</li>
* <li>There is no concept of p-sampling so bytes 12-15 of Pre1 are empty.</li>
* <li>The determination of empty is when both curCount=0 and thetaLong = Long.MAX_VALUE.</li>
* </ul>
*
* @param skV3 a SerVer3, ordered CompactSketch
* @return a SerVer1 SetSketch as Memory object.
*/
public static Memory convertSerVer3toSerVer1(final CompactSketch skV3) {
// Check input sketch
final boolean validIn = skV3.isCompact() && skV3.isOrdered() && !skV3.hasMemory();
if (!validIn) {
throw new SketchesArgumentException("Invalid input sketch.");
}
// Build V1 SetSketch in memory
final int curCount = skV3.getRetainedEntries(true);
final WritableMemory wmem = WritableMemory.allocate((3 + curCount) << 3);
// Pre0
// preLongs
wmem.putByte(0, (byte) 3);
// SerVer
wmem.putByte(1, (byte) 1);
// Compact (SetSketch)
wmem.putByte(2, (byte) 3);
// Flags ReadOnly, LittleEndian
wmem.putByte(6, (byte) 2);
// Pre1
wmem.putInt(8, curCount);
// Pre2
wmem.putLong(16, skV3.getThetaLong());
// Data
if (curCount > 0) {
wmem.putLongArray(24, skV3.getCache(), 0, curCount);
}
return wmem;
}
Aggregations