Search in sources :

Example 26 with SketchesArgumentException

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) {
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 27 with SketchesArgumentException

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
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) ArrayOfLongsSerDe(com.yahoo.sketches.ArrayOfLongsSerDe)

Example 28 with SketchesArgumentException

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;
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Memory(com.yahoo.memory.Memory)

Example 29 with SketchesArgumentException

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);
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with SketchesArgumentException

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);
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)41 WritableMemory (com.yahoo.memory.WritableMemory)22 Test (org.testng.annotations.Test)12 Family (com.yahoo.sketches.Family)11 Memory (com.yahoo.memory.Memory)10 ResizeFactor (com.yahoo.sketches.ResizeFactor)6 Family.idToFamily (com.yahoo.sketches.Family.idToFamily)5 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)4 PreambleUtil.extractResizeFactor (com.yahoo.sketches.sampling.PreambleUtil.extractResizeFactor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ArrayOfBooleansSerDe (com.yahoo.sketches.ArrayOfBooleansSerDe)1 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)1 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1