use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class ForwardCompatibilityTest method convertSerV3toSerV1.
/**
* Converts a SerVer3 CompactSketch to a SerVer1 SetSketch.
* @param v3mem a SerVer3 CompactSketch, ordered and with 24 byte preamble.
* @return a SerVer1 SetSketch as Memory object
*/
public static WritableMemory convertSerV3toSerV1(Memory v3mem) {
//validate that v3mem is in the right form
int serVer = v3mem.getByte(SER_VER_BYTE);
int famId = v3mem.getByte(FAMILY_BYTE);
int flags = v3mem.getByte(FLAGS_BYTE);
if ((serVer != 3) || (famId != 3) || ((flags & 24) != 24))
throw new SketchesArgumentException("Memory must be V3, Compact, Ordered");
//must convert v3 preamble to a v1 preamble
int v3preLongs = v3mem.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
int entries;
long thetaLong;
if (v3preLongs == 1) {
entries = 0;
thetaLong = Long.MAX_VALUE;
} else if (v3preLongs == 2) {
entries = v3mem.getInt(RETAINED_ENTRIES_INT);
thetaLong = Long.MAX_VALUE;
} else {
//preLongs == 3
entries = v3mem.getInt(RETAINED_ENTRIES_INT);
thetaLong = v3mem.getLong(THETA_LONG);
}
//compute size
int v1preLongs = 3;
int v1bytes = (v1preLongs + entries) << 3;
//create new mem and place the fields for SerVer1
WritableMemory v1mem = WritableMemory.wrap(new byte[v1bytes]);
//Preamble = 3
v1mem.putByte(0, (byte) 3);
//SerVer
v1mem.putByte(1, (byte) 1);
//SketchType SetSketch = Family CompactSketch
v1mem.putByte(2, (byte) 3);
//set bit1 = ReadOnly
v1mem.putByte(6, (byte) 2);
v1mem.putInt(RETAINED_ENTRIES_INT, entries);
v1mem.putLong(THETA_LONG, thetaLong);
//copy data
v3mem.copyTo(v3preLongs << 3, v1mem, v1preLongs << 3, entries << 3);
return v1mem;
}
use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class HeapAlphaSketchTest method checkMemDeSerExceptions.
@Test
public void checkMemDeSerExceptions() {
int k = 1024;
UpdateSketch sk1 = UpdateSketch.builder().setFamily(ALPHA).setNominalEntries(k).build();
//forces preLongs to 3
sk1.update(1L);
byte[] bytearray1 = sk1.toByteArray();
WritableMemory mem = WritableMemory.wrap(bytearray1);
long pre0 = mem.getLong(0);
//Corrupt PreLongs
tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt SerVer
tryBadMem(mem, SER_VER_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt Family
tryBadMem(mem, FAMILY_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt READ_ONLY to true
tryBadMem(mem, FLAGS_BYTE, 2);
//restore
mem.putLong(0, pre0);
try {
//Corrupt the theta value
mem.putDouble(16, 0.5);
HeapAlphaSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
//restore theta
mem.putDouble(16, 1.0);
byte[] byteArray2 = new byte[bytearray1.length - 1];
WritableMemory mem2 = WritableMemory.wrap(byteArray2);
mem.copyTo(0, mem2, 0, mem2.getCapacity());
try {
HeapAlphaSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
}
use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class HeapQuickSelectSketchTest method checkMemSerDeExceptions.
@Test
public void checkMemSerDeExceptions() {
int k = 1024;
UpdateSketch sk1 = UpdateSketch.builder().setFamily(QUICKSELECT).setNominalEntries(k).build();
//forces preLongs to 3
sk1.update(1L);
byte[] bytearray1 = sk1.toByteArray();
WritableMemory mem = WritableMemory.wrap(bytearray1);
long pre0 = mem.getLong(0);
//Corrupt PreLongs
tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt SerVer
tryBadMem(mem, SER_VER_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt Family
tryBadMem(mem, FAMILY_BYTE, 1);
//restore
mem.putLong(0, pre0);
//Corrupt READ_ONLY to true
tryBadMem(mem, FLAGS_BYTE, 2);
//restore
mem.putLong(0, pre0);
//Corrupt, Family to Union
tryBadMem(mem, FAMILY_BYTE, 4);
try {
//Corrupt the theta value
mem.putDouble(16, 0.5);
HeapQuickSelectSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
//restore theta
mem.putDouble(16, 1.0);
byte[] byteArray2 = new byte[bytearray1.length - 1];
WritableMemory mem2 = WritableMemory.wrap(byteArray2);
mem.copyTo(0, mem2, 0, mem2.getCapacity());
try {
HeapQuickSelectSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
fail();
} catch (SketchesArgumentException e) {
//expected
}
}
use of com.yahoo.sketches.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.wrap(copyBytes);
// may be null
final Object memObj = mem.getArray();
final long memAddr = mem.getCumulativeOffset(0L);
// copy the bytes
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractPreLongs(mem), PreambleUtil.VO_WARMUP_PRELONGS);
// no items in R but max preLongs
try {
PreambleUtil.insertPreLongs(memObj, memAddr, 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_WARMUP_PRELONGS);
// negative H region count
try {
PreambleUtil.insertHRegionItemCount(memObj, memAddr, -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(memObj, memAddr, -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 < 2
try {
PreambleUtil.insertK(memObj, memAddr, 0);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: k must be at least 2: 0"));
}
// refresh the copy
srcMem.copyTo(0, mem, 0, sketchBytes.length);
assertEquals(PreambleUtil.extractK(mem), k);
// invalid n < 0
try {
PreambleUtil.insertN(memObj, memAddr, -1024);
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
assertTrue(e.getMessage().equals("Possible Corruption: n cannot be negative: -1024"));
}
}
use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class VarOptItemsSketchTest method checkBadPreLongs.
@Test
public void checkBadPreLongs() {
final VarOptItemsSketch<Long> sketch = getUnweightedLongsVIS(32, 33);
final byte[] bytes = sketch.toByteArray(new ArrayOfLongsSerDe());
final WritableMemory mem = WritableMemory.wrap(bytes);
// corrupt the preLongs count to 0
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMinPreLongs() - 1));
try {
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
// expected
}
// corrupt the preLongs count to 2
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 2);
try {
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
// expected
}
// corrupt the preLongs count to be too large
mem.putByte(PREAMBLE_LONGS_BYTE, (byte) (Family.VAROPT.getMaxPreLongs() + 1));
try {
VarOptItemsSketch.heapify(mem, new ArrayOfLongsSerDe());
fail();
} catch (final SketchesArgumentException e) {
// expected
}
}
Aggregations