Search in sources :

Example 51 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryBuilderReader method parseValue.

/**
 * @return Parsed value.
 */
public Object parseValue() {
    int valPos = pos;
    byte type = arr[pos++];
    int plainLazyValLen;
    boolean modifiableLazyVal = false;
    switch(type) {
        case GridBinaryMarshaller.NULL:
            return null;
        case GridBinaryMarshaller.HANDLE:
            {
                int objStart = pos - 1 - readInt();
                Object res = objMap.get(objStart);
                if (res != null)
                    return res;
                // Read handle by position
                int savedPos = pos;
                pos = objStart;
                res = parseValue();
                pos = savedPos;
                objMap.put(objStart, res);
                return res;
            }
        case GridBinaryMarshaller.OBJ:
            {
                pos--;
                Object res = objMap.get(pos);
                if (res == null) {
                    res = new BinaryObjectBuilderImpl(new BinaryBuilderReader(this, pos), pos);
                    objMap.put(pos, res);
                }
                pos += readInt(GridBinaryMarshaller.TOTAL_LEN_POS);
                return res;
            }
        case GridBinaryMarshaller.BYTE:
            return arr[pos++];
        case GridBinaryMarshaller.SHORT:
            {
                Object res = BinaryPrimitives.readShort(arr, pos);
                pos += 2;
                return res;
            }
        case GridBinaryMarshaller.INT:
            return readInt();
        case GridBinaryMarshaller.LONG:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.FLOAT:
            plainLazyValLen = 4;
            break;
        case GridBinaryMarshaller.DOUBLE:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.CHAR:
            plainLazyValLen = 2;
            break;
        case GridBinaryMarshaller.BOOLEAN:
            return arr[pos++] != 0;
        case GridBinaryMarshaller.DECIMAL:
            plainLazyValLen = /**
             * scale
             */
            4 + /**
             * mag len
             */
            4 + /**
             * mag bytes count
             */
            readInt(4);
            break;
        case GridBinaryMarshaller.STRING:
            plainLazyValLen = 4 + readStringLength();
            break;
        case GridBinaryMarshaller.UUID:
            plainLazyValLen = 8 + 8;
            break;
        case GridBinaryMarshaller.DATE:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.TIMESTAMP:
            plainLazyValLen = 8 + 4;
            break;
        case GridBinaryMarshaller.TIME:
            plainLazyValLen = 8;
            break;
        case GridBinaryMarshaller.BYTE_ARR:
            plainLazyValLen = 4 + readLength();
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.SHORT_ARR:
            plainLazyValLen = 4 + readLength() * 2;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.INT_ARR:
            plainLazyValLen = 4 + readLength() * 4;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.LONG_ARR:
            plainLazyValLen = 4 + readLength() * 8;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.FLOAT_ARR:
            plainLazyValLen = 4 + readLength() * 4;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.DOUBLE_ARR:
            plainLazyValLen = 4 + readLength() * 8;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.CHAR_ARR:
            plainLazyValLen = 4 + readLength() * 2;
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.BOOLEAN_ARR:
            plainLazyValLen = 4 + readLength();
            modifiableLazyVal = true;
            break;
        case GridBinaryMarshaller.OBJ_ARR:
            return new BinaryObjectArrayLazyValue(this);
        case GridBinaryMarshaller.DATE_ARR:
            {
                int size = readInt();
                Date[] res = new Date[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.DATE)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    res[i] = new Date(time);
                }
                return res;
            }
        case GridBinaryMarshaller.TIMESTAMP_ARR:
            {
                int size = readInt();
                Timestamp[] res = new Timestamp[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.TIMESTAMP)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    int nano = BinaryPrimitives.readInt(arr, pos);
                    pos += 4;
                    Timestamp ts = new Timestamp(time);
                    ts.setNanos(ts.getNanos() + nano);
                    res[i] = ts;
                }
                return res;
            }
        case GridBinaryMarshaller.TIME_ARR:
            {
                int size = readInt();
                Time[] res = new Time[size];
                for (int i = 0; i < res.length; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.NULL)
                        continue;
                    if (flag != GridBinaryMarshaller.TIME)
                        throw new BinaryObjectException("Invalid flag value: " + flag);
                    long time = BinaryPrimitives.readLong(arr, pos);
                    pos += 8;
                    res[i] = new Time(time);
                }
                return res;
            }
        case GridBinaryMarshaller.UUID_ARR:
        case GridBinaryMarshaller.STRING_ARR:
        case GridBinaryMarshaller.DECIMAL_ARR:
            {
                int size = readInt();
                for (int i = 0; i < size; i++) {
                    byte flag = arr[pos++];
                    if (flag == GridBinaryMarshaller.UUID)
                        pos += 8 + 8;
                    else if (flag == GridBinaryMarshaller.STRING)
                        pos += 4 + readStringLength();
                    else if (flag == GridBinaryMarshaller.DECIMAL) {
                        // scale value
                        pos += 4;
                        pos += 4 + readLength();
                    } else
                        assert flag == GridBinaryMarshaller.NULL;
                }
                return new BinaryModifiableLazyValue(this, valPos, pos - valPos);
            }
        case GridBinaryMarshaller.COL:
            {
                int size = readInt();
                byte colType = arr[pos++];
                Object res = objMap.get(valPos);
                Object parseRes;
                switch(colType) {
                    case GridBinaryMarshaller.USER_COL:
                    case GridBinaryMarshaller.ARR_LIST:
                        parseRes = new BinaryLazyArrayList(this, size);
                        break;
                    case GridBinaryMarshaller.LINKED_LIST:
                        parseRes = new BinaryLazyLinkedList(this, size);
                        break;
                    case GridBinaryMarshaller.HASH_SET:
                    case GridBinaryMarshaller.LINKED_HASH_SET:
                        parseRes = new BinaryLazySet(this, size);
                        break;
                    default:
                        throw new BinaryObjectException("Unknown collection type: " + colType);
                }
                if (res == null || res instanceof LazyCollection) {
                    objMap.put(valPos, parseRes);
                    res = parseRes;
                }
                return res;
            }
        case GridBinaryMarshaller.MAP:
            {
                Object res = objMap.get(valPos);
                Object parseRes = BinaryLazyMap.parseMap(this);
                if (res == null || res instanceof LazyCollection) {
                    objMap.put(valPos, parseRes);
                    res = parseRes;
                }
                return res;
            }
        case GridBinaryMarshaller.ENUM:
            return new BinaryBuilderEnum(this);
        case GridBinaryMarshaller.ENUM_ARR:
            return new BinaryEnumArrayLazyValue(this);
        case GridBinaryMarshaller.BINARY_OBJ:
            {
                int size = readInt();
                pos += size;
                int start = readInt();
                BinaryObjectImpl binaryObj = new BinaryObjectImpl(ctx, arr, pos - 4 - size + start);
                return new BinaryPlainBinaryObject(binaryObj);
            }
        case GridBinaryMarshaller.OPTM_MARSH:
            {
                final BinaryHeapInputStream bin = BinaryHeapInputStream.create(arr, pos);
                final Object obj = BinaryUtils.doReadOptimized(bin, ctx, U.resolveClassLoader(ctx.configuration()));
                pos = bin.position();
                return obj;
            }
        default:
            throw new BinaryObjectException("Invalid flag value: " + type);
    }
    BinaryAbstractLazyValue res;
    if (modifiableLazyVal)
        res = new BinaryModifiableLazyValue(this, valPos, 1 + plainLazyValLen);
    else
        res = new BinaryPlainLazyValue(this, valPos, 1 + plainLazyValLen);
    pos += plainLazyValLen;
    return res;
}
Also used : BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 52 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryBuilderReader method readString.

/**
 * Reads string.
 *
 * @return String.
 */
public String readString() {
    byte flag = readByte();
    if (flag == GridBinaryMarshaller.NULL)
        return null;
    if (flag != GridBinaryMarshaller.STRING)
        throw new BinaryObjectException("Failed to deserialize String.");
    int len = readInt();
    String str = new String(arr, pos, len, UTF_8);
    pos += len;
    return str;
}
Also used : BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 53 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryReaderExImpl method deserialize0.

/**
 * @return Deserialized object.
 * @throws BinaryObjectException If failed.
 */
@Nullable
private Object deserialize0() throws BinaryObjectException {
    Object obj;
    byte flag = in.readByte();
    switch(flag) {
        case NULL:
            obj = null;
            break;
        case HANDLE:
            int handlePos = start - in.readInt();
            obj = getHandle(handlePos);
            if (obj == null) {
                int retPos = in.position();
                streamPosition(handlePos);
                obj = BinaryUtils.doReadObject(in, ctx, ldr, this);
                streamPosition(retPos);
            }
            break;
        case OBJ:
            if (desc == null)
                desc = ctx.descriptorForTypeId(userType, typeId, ldr, false);
            streamPosition(dataStart);
            if (desc == null)
                throw new BinaryInvalidTypeException("Unknown type ID: " + typeId);
            obj = desc.read(this);
            streamPosition(footerStart + footerLen);
            break;
        case BYTE:
            obj = in.readByte();
            break;
        case SHORT:
            obj = in.readShort();
            break;
        case INT:
            obj = in.readInt();
            break;
        case LONG:
            obj = in.readLong();
            break;
        case FLOAT:
            obj = in.readFloat();
            break;
        case DOUBLE:
            obj = in.readDouble();
            break;
        case CHAR:
            obj = in.readChar();
            break;
        case BOOLEAN:
            obj = in.readBoolean();
            break;
        case DECIMAL:
            obj = BinaryUtils.doReadDecimal(in);
            break;
        case STRING:
            obj = BinaryUtils.doReadString(in);
            break;
        case UUID:
            obj = BinaryUtils.doReadUuid(in);
            break;
        case DATE:
            obj = BinaryUtils.doReadDate(in);
            break;
        case TIMESTAMP:
            obj = BinaryUtils.doReadTimestamp(in);
            break;
        case TIME:
            obj = BinaryUtils.doReadTime(in);
            break;
        case BYTE_ARR:
            obj = BinaryUtils.doReadByteArray(in);
            break;
        case SHORT_ARR:
            obj = BinaryUtils.doReadShortArray(in);
            break;
        case INT_ARR:
            obj = BinaryUtils.doReadIntArray(in);
            break;
        case LONG_ARR:
            obj = BinaryUtils.doReadLongArray(in);
            break;
        case FLOAT_ARR:
            obj = BinaryUtils.doReadFloatArray(in);
            break;
        case DOUBLE_ARR:
            obj = BinaryUtils.doReadDoubleArray(in);
            break;
        case CHAR_ARR:
            obj = BinaryUtils.doReadCharArray(in);
            break;
        case BOOLEAN_ARR:
            obj = BinaryUtils.doReadBooleanArray(in);
            break;
        case DECIMAL_ARR:
            obj = BinaryUtils.doReadDecimalArray(in);
            break;
        case STRING_ARR:
            obj = BinaryUtils.doReadStringArray(in);
            break;
        case UUID_ARR:
            obj = BinaryUtils.doReadUuidArray(in);
            break;
        case DATE_ARR:
            obj = BinaryUtils.doReadDateArray(in);
            break;
        case TIMESTAMP_ARR:
            obj = BinaryUtils.doReadTimestampArray(in);
            break;
        case TIME_ARR:
            obj = BinaryUtils.doReadTimeArray(in);
            break;
        case OBJ_ARR:
            obj = BinaryUtils.doReadObjectArray(in, ctx, ldr, this, false, true);
            break;
        case COL:
            obj = BinaryUtils.doReadCollection(in, ctx, ldr, this, false, true, null);
            break;
        case MAP:
            obj = BinaryUtils.doReadMap(in, ctx, ldr, this, false, true, null);
            break;
        case BINARY_OBJ:
            obj = BinaryUtils.doReadBinaryObject(in, ctx, false);
            ((BinaryObjectImpl) obj).context(ctx);
            break;
        case ENUM:
            obj = BinaryUtils.doReadEnum(in, BinaryUtils.doReadClass(in, ctx, ldr), GridBinaryMarshaller.USE_CACHE.get());
            break;
        case ENUM_ARR:
            obj = BinaryUtils.doReadEnumArray(in, ctx, ldr, BinaryUtils.doReadClass(in, ctx, ldr));
            break;
        case BINARY_ENUM:
            obj = BinaryUtils.doReadBinaryEnum(in, ctx);
            break;
        case CLASS:
            obj = BinaryUtils.doReadClass(in, ctx, ldr);
            break;
        case PROXY:
            obj = BinaryUtils.doReadProxy(in, ctx, ldr, this);
            break;
        case OPTM_MARSH:
            obj = BinaryUtils.doReadOptimized(in, ctx, ldr);
            break;
        default:
            throw new BinaryObjectException("Invalid flag value: " + flag);
    }
    return obj;
}
Also used : BinaryInvalidTypeException(org.apache.ignite.binary.BinaryInvalidTypeException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) Nullable(org.jetbrains.annotations.Nullable)

Example 54 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryObjectBuilderImpl method checkMetadata.

/**
 * Checks metadata when a BinaryObject is being serialized.
 *
 * @param meta Current metadata.
 * @param fieldsMeta Map holding metadata information that has to be updated.
 * @param newVal Field value being serialized.
 * @param name Field name.
 * @param fieldId Field ID.
 */
private Map<String, BinaryFieldMetadata> checkMetadata(BinaryType meta, Map<String, BinaryFieldMetadata> fieldsMeta, Object newVal, String name, int fieldId) {
    String oldFldTypeName = meta == null ? null : meta.fieldTypeName(name);
    boolean nullFieldVal = false;
    int newFldTypeId;
    if (newVal instanceof BinaryValueWithType) {
        newFldTypeId = ((BinaryValueWithType) newVal).typeId();
        if (((BinaryValueWithType) newVal).value() == null)
            nullFieldVal = true;
    } else // Detect Enum and Enum array type.
    if (newVal instanceof BinaryEnumObjectImpl)
        newFldTypeId = GridBinaryMarshaller.ENUM;
    else if (newVal.getClass().isArray() && BinaryEnumObjectImpl.class.isAssignableFrom(newVal.getClass().getComponentType()))
        newFldTypeId = GridBinaryMarshaller.ENUM_ARR;
    else if (newVal.getClass().isArray() && BinaryObject.class.isAssignableFrom(newVal.getClass().getComponentType()))
        newFldTypeId = GridBinaryMarshaller.OBJ_ARR;
    else if (newVal instanceof BinaryEnumArray)
        newFldTypeId = GridBinaryMarshaller.ENUM_ARR;
    else if (newVal instanceof BinaryArray)
        newFldTypeId = GridBinaryMarshaller.OBJ_ARR;
    else
        newFldTypeId = BinaryUtils.typeByClass(newVal.getClass());
    if (oldFldTypeName == null) {
        // It's a new field, we have to add it to metadata.
        if (fieldsMeta == null) {
            if (BinaryUtils.FIELDS_SORTED_ORDER)
                fieldsMeta = new TreeMap<>();
            else
                fieldsMeta = new LinkedHashMap<>();
        }
        fieldsMeta.put(name, new BinaryFieldMetadata(newFldTypeId, fieldId));
    } else if (!nullFieldVal) {
        String newFldTypeName = BinaryUtils.fieldTypeName(newFldTypeId);
        if (!F.eq(newFldTypeName, oldFldTypeName) && !oldFldTypeName.equals(BinaryUtils.fieldTypeName(GridBinaryMarshaller.OBJ))) {
            throw new BinaryObjectException("Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + ", fieldName=" + name + ", fieldType=" + oldFldTypeName + ", assignedValueType=" + newFldTypeName + ']');
        }
    }
    return fieldsMeta;
}
Also used : BinaryArray(org.apache.ignite.internal.binary.BinaryArray) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) TreeMap(java.util.TreeMap) BinaryEnumArray(org.apache.ignite.internal.binary.BinaryEnumArray) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryUtils method doReadUuidArray.

/**
 * @return Value.
 * @throws BinaryObjectException In case of error.
 */
public static UUID[] doReadUuidArray(BinaryInputStream in) throws BinaryObjectException {
    int len = in.readInt();
    UUID[] arr = new UUID[len];
    for (int i = 0; i < len; i++) {
        byte flag = in.readByte();
        if (flag == GridBinaryMarshaller.NULL)
            arr[i] = null;
        else {
            if (flag != GridBinaryMarshaller.UUID)
                throw new BinaryObjectException("Invalid flag value: " + flag);
            arr[i] = doReadUuid(in);
        }
    }
    return arr;
}
Also used : UUID(java.util.UUID) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)68 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 BinaryObject (org.apache.ignite.binary.BinaryObject)12 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)9 IOException (java.io.IOException)7 BinaryType (org.apache.ignite.binary.BinaryType)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Test (org.junit.Test)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)5 IgniteException (org.apache.ignite.IgniteException)5 BigInteger (java.math.BigInteger)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)3 BinaryTypeImpl (org.apache.ignite.internal.binary.BinaryTypeImpl)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2