use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class DirectUpdateDoublesSketchTest method variousExceptions.
@SuppressWarnings("unused")
@Test
public void variousExceptions() {
WritableMemory mem = WritableMemory.wrap(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 com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class ItemsSketchTest method tryBadMem.
private static void tryBadMem(WritableMemory mem, int byteOffset, int byteValue) {
ArrayOfLongsSerDe serDe = new ArrayOfLongsSerDe();
try {
//Corrupt
mem.putByte(byteOffset, (byte) byteValue);
ItemsSketch.getInstance(mem, serDe);
fail();
} catch (SketchesArgumentException e) {
//expected
}
}
use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class HeapArrayOfDoublesUnion method heapifyUnion.
/**
* This is to create an instance given a serialized form and a custom seed
* @param mem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
* @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a>
*/
static ArrayOfDoublesUnion heapifyUnion(final Memory mem, final long seed) {
final SerializerDeserializer.SketchType type = SerializerDeserializer.getSketchType(mem);
// compatibility with version 0.9.1 and lower
if (type == SerializerDeserializer.SketchType.ArrayOfDoublesQuickSelectSketch) {
final ArrayOfDoublesQuickSelectSketch sketch = new HeapArrayOfDoublesQuickSelectSketch(mem, seed);
return new HeapArrayOfDoublesUnion(sketch);
}
final byte version = mem.getByte(SERIAL_VERSION_BYTE);
if (version != serialVersionUID) {
throw new SketchesArgumentException("Serial version mismatch. Expected: " + serialVersionUID + ", actual: " + version);
}
SerializerDeserializer.validateFamily(mem.getByte(FAMILY_ID_BYTE), mem.getByte(PREAMBLE_LONGS_BYTE));
SerializerDeserializer.validateType(mem.getByte(SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.ArrayOfDoublesUnion);
final long unionTheta = mem.getLong(THETA_LONG);
final Memory sketchMem = mem.region(PREAMBLE_SIZE_BYTES, mem.getCapacity() - PREAMBLE_SIZE_BYTES);
final ArrayOfDoublesQuickSelectSketch sketch = new HeapArrayOfDoublesQuickSelectSketch(sketchMem, seed);
final ArrayOfDoublesUnion union = new HeapArrayOfDoublesUnion(sketch);
union.theta_ = unionTheta;
return union;
}
use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class SerializerDeserializer method deserializeFromMemory.
@SuppressWarnings("unchecked")
static <T> DeserializeResult<T> deserializeFromMemory(final Memory mem, final int offset, final String className) {
try {
Method method = deserializeMethodCache.get(className);
if (method == null) {
method = Class.forName(className).getMethod("fromMemory", Memory.class);
deserializeMethodCache.put(className, method);
}
return (DeserializeResult<T>) method.invoke(null, mem.region(offset, mem.getCapacity() - offset));
} catch (final IllegalAccessException | SketchesArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
throw new SketchesArgumentException("Failed to deserialize class " + className + " " + e);
}
}
use of com.yahoo.sketches.SketchesArgumentException in project sketches-core by DataSketches.
the class SerializerDeserializer method toByteArray.
static byte[] toByteArray(final Object object) {
try {
final String className = object.getClass().getName();
final byte[] objectBytes = ((byte[]) object.getClass().getMethod("toByteArray", (Class<?>[]) null).invoke(object));
final byte[] bytes = new byte[1 + className.length() + objectBytes.length];
final WritableMemory mem = WritableMemory.wrap(bytes);
int offset = 0;
mem.putByte(offset++, (byte) className.length());
mem.putByteArray(offset, className.getBytes(UTF_8), 0, className.length());
offset += className.length();
mem.putByteArray(offset, objectBytes, 0, objectBytes.length);
return bytes;
} catch (final NoSuchMethodException | SecurityException | IllegalAccessException | SketchesArgumentException | InvocationTargetException e) {
throw new SketchesArgumentException("Failed to serialize given object: " + e);
}
}
Aggregations