Search in sources :

Example 1 with BinaryEnumArray

use of org.apache.ignite.internal.binary.BinaryEnumArray in project ignite by apache.

the class BinaryBuilderSerializer method writeValue.

/**
 * @param writer Writer.
 * @param val Value.
 * @param forceCol Whether to force collection type.
 * @param forceMap Whether to force map type.
 */
public void writeValue(BinaryWriterExImpl writer, Object val, boolean forceCol, boolean forceMap) {
    assert !(forceCol && forceMap);
    if (val == null) {
        writer.writeByte(GridBinaryMarshaller.NULL);
        return;
    }
    if (val instanceof BinaryBuilderSerializationAware) {
        ((BinaryBuilderSerializationAware) val).writeTo(writer, this);
        return;
    }
    if (val instanceof BinaryObjectExImpl) {
        if (binaryObjToWrapper == null)
            binaryObjToWrapper = new IdentityHashMap<>();
        BinaryObjectBuilderImpl wrapper = binaryObjToWrapper.get(val);
        if (wrapper == null) {
            wrapper = BinaryObjectBuilderImpl.wrap((BinaryObject) val);
            binaryObjToWrapper.put((BinaryObject) val, wrapper);
        }
        val = wrapper;
    }
    if (val instanceof BinaryObjectBuilderImpl) {
        BinaryObjectBuilderImpl obj = (BinaryObjectBuilderImpl) val;
        Integer posInResArr = objToPos.get(obj);
        if (posInResArr == null) {
            objToPos.put(obj, writer.out().position());
            obj.serializeTo(writer.newWriter(obj.typeId()), this);
        } else {
            int handle = writer.out().position() - posInResArr;
            writer.writeByte(GridBinaryMarshaller.HANDLE);
            writer.writeInt(handle);
        }
        return;
    }
    if (val instanceof BinaryEnumObjectImpl) {
        BinaryEnumObjectImpl obj = (BinaryEnumObjectImpl) val;
        writer.writeByte(GridBinaryMarshaller.ENUM);
        writer.writeInt(obj.typeId());
        if (obj.typeId() == GridBinaryMarshaller.UNREGISTERED_TYPE_ID)
            writer.doWriteString(obj.className());
        writer.writeInt(obj.enumOrdinal());
        return;
    }
    if (IgniteUtils.isEnum(val.getClass())) {
        String clsName = ((Enum) val).getDeclaringClass().getName();
        int typeId = writer.context().typeId(clsName);
        // Need register class for marshaller to be able to deserialize enum value.
        writer.context().registerClass(((Enum) val).getDeclaringClass(), true, false);
        writer.writeByte(GridBinaryMarshaller.ENUM);
        writer.writeInt(typeId);
        writer.writeInt(((Enum) val).ordinal());
        return;
    }
    if (forceCol || BinaryUtils.isSpecialCollection(val.getClass())) {
        Collection<?> c = (Collection<?>) val;
        writer.writeByte(GridBinaryMarshaller.COL);
        writer.writeInt(c.size());
        byte colType = writer.context().collectionType(c.getClass());
        writer.writeByte(colType);
        for (Object obj : c) writeValue(writer, obj);
        return;
    }
    if (forceMap || BinaryUtils.isSpecialMap(val.getClass())) {
        Map<?, ?> map = (Map<?, ?>) val;
        writer.writeByte(GridBinaryMarshaller.MAP);
        writer.writeInt(map.size());
        writer.writeByte(writer.context().mapType(map.getClass()));
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            writeValue(writer, entry.getKey());
            writeValue(writer, entry.getValue());
        }
        return;
    }
    Byte flag = BinaryUtils.PLAIN_CLASS_TO_FLAG.get(val.getClass());
    if (flag != null) {
        BinaryUtils.writePlainObject(writer, val);
        return;
    }
    if (val instanceof BinaryEnumArray) {
        BinaryArray val0 = (BinaryArray) val;
        if (val0.componentTypeId() == GridBinaryMarshaller.UNREGISTERED_TYPE_ID)
            writeArray(writer, GridBinaryMarshaller.ENUM_ARR, val0.array(), val0.componentClassName());
        else
            writeArray(writer, GridBinaryMarshaller.ENUM_ARR, val0.array(), val0.componentTypeId());
        return;
    }
    if (val instanceof BinaryArray) {
        BinaryArray val0 = (BinaryArray) val;
        if (val0.componentTypeId() == GridBinaryMarshaller.UNREGISTERED_TYPE_ID)
            writeArray(writer, GridBinaryMarshaller.OBJ_ARR, val0.array(), val0.componentClassName());
        else
            writeArray(writer, GridBinaryMarshaller.OBJ_ARR, val0.array(), val0.componentTypeId());
        return;
    }
    if (val instanceof Object[]) {
        Class<?> compCls = ((Object[]) val).getClass().getComponentType();
        int compTypeId = writer.context().typeId(compCls.getName());
        if (BinaryEnumObjectImpl.class.isAssignableFrom(compCls) || val instanceof BinaryBuilderEnum[]) {
            writeArray(writer, GridBinaryMarshaller.ENUM_ARR, (Object[]) val, compTypeId);
            return;
        }
        if (compCls.isEnum()) {
            Enum[] enumArr = (Enum[]) val;
            writer.context().registerClass(compCls, true, false);
            writer.writeByte(GridBinaryMarshaller.ENUM_ARR);
            writer.writeInt(compTypeId);
            writer.writeInt(enumArr.length);
            for (Enum anEnum : enumArr) writeValue(writer, anEnum);
            return;
        }
        writeArray(writer, GridBinaryMarshaller.OBJ_ARR, (Object[]) val, compTypeId);
        return;
    }
    writer.doWriteObject(val);
}
Also used : BinaryArray(org.apache.ignite.internal.binary.BinaryArray) BinaryObjectExImpl(org.apache.ignite.internal.binary.BinaryObjectExImpl) IdentityHashMap(java.util.IdentityHashMap) BinaryObject(org.apache.ignite.binary.BinaryObject) Collection(java.util.Collection) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) BinaryEnumArray(org.apache.ignite.internal.binary.BinaryEnumArray)

Example 2 with BinaryEnumArray

use of org.apache.ignite.internal.binary.BinaryEnumArray 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 3 with BinaryEnumArray

use of org.apache.ignite.internal.binary.BinaryEnumArray in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method marshalToBinary.

/**
 * {@inheritDoc}
 */
@Override
public Object marshalToBinary(@Nullable Object obj, boolean failIfUnregistered) throws BinaryObjectException {
    if (obj == null)
        return null;
    if (BinaryUtils.isBinaryType(obj.getClass()))
        return obj;
    if (obj instanceof Object[]) {
        Object[] arr = (Object[]) obj;
        Object[] pArr = new Object[arr.length];
        for (int i = 0; i < arr.length; i++) pArr[i] = marshalToBinary(arr[i], failIfUnregistered);
        if (!BinaryArray.useBinaryArrays())
            return pArr;
        Class<?> compCls = obj.getClass().getComponentType();
        boolean isBinaryArr = BinaryObject.class.isAssignableFrom(compCls);
        String compClsName = isBinaryArr ? Object.class.getName() : compCls.getName();
        // In case of interface or multidimensional array rely on class name.
        // Interfaces and array not registered as binary types.
        BinaryClassDescriptor desc = binaryCtx.descriptorForClass(compCls);
        if (compCls.isEnum() || compCls == BinaryEnumObjectImpl.class) {
            return new BinaryEnumArray(binaryCtx, desc.registered() ? desc.typeId() : GridBinaryMarshaller.UNREGISTERED_TYPE_ID, compClsName, pArr);
        } else {
            return new BinaryArray(binaryCtx, desc.registered() ? desc.typeId() : GridBinaryMarshaller.UNREGISTERED_TYPE_ID, compClsName, pArr);
        }
    }
    if (obj instanceof IgniteBiTuple) {
        IgniteBiTuple tup = (IgniteBiTuple) obj;
        if (obj instanceof T2)
            return new T2<>(marshalToBinary(tup.get1(), failIfUnregistered), marshalToBinary(tup.get2(), failIfUnregistered));
        return new IgniteBiTuple<>(marshalToBinary(tup.get1(), failIfUnregistered), marshalToBinary(tup.get2(), failIfUnregistered));
    }
    {
        Collection<Object> pCol = BinaryUtils.newKnownCollection(obj);
        if (pCol != null) {
            Collection<?> col = (Collection<?>) obj;
            for (Object item : col) pCol.add(marshalToBinary(item, failIfUnregistered));
            return (pCol instanceof MutableSingletonList) ? U.convertToSingletonList(pCol) : pCol;
        }
    }
    {
        Map<Object, Object> pMap = BinaryUtils.newKnownMap(obj);
        if (pMap != null) {
            Map<?, ?> map = (Map<?, ?>) obj;
            for (Map.Entry<?, ?> e : map.entrySet()) pMap.put(marshalToBinary(e.getKey(), failIfUnregistered), marshalToBinary(e.getValue(), failIfUnregistered));
            return pMap;
        }
    }
    if (obj instanceof Map.Entry) {
        Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj;
        return new GridMapEntry<>(marshalToBinary(e.getKey(), failIfUnregistered), marshalToBinary(e.getValue(), failIfUnregistered));
    }
    if (binaryMarsh.mustDeserialize(obj))
        // No need to go through marshal-unmarshal because result will be the same as initial object.
        return obj;
    byte[] arr = binaryMarsh.marshal(obj, failIfUnregistered);
    assert arr.length > 0;
    Object obj0 = binaryMarsh.unmarshal(arr, null);
    // Possible if a class has writeObject method.
    if (obj0 instanceof BinaryObjectImpl)
        ((BinaryObjectImpl) obj0).detachAllowed(true);
    return obj0;
}
Also used : BinaryClassDescriptor(org.apache.ignite.internal.binary.BinaryClassDescriptor) BinaryArray(org.apache.ignite.internal.binary.BinaryArray) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridMapEntry(org.apache.ignite.internal.util.lang.GridMapEntry) GridMapEntry(org.apache.ignite.internal.util.lang.GridMapEntry) Collection(java.util.Collection) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) IncompleteCacheObject(org.apache.ignite.internal.processors.cache.IncompleteCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) MutableSingletonList(org.apache.ignite.internal.util.MutableSingletonList) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BinaryEnumArray(org.apache.ignite.internal.binary.BinaryEnumArray) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

BinaryArray (org.apache.ignite.internal.binary.BinaryArray)3 BinaryEnumArray (org.apache.ignite.internal.binary.BinaryEnumArray)3 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)3 Collection (java.util.Collection)2 Map (java.util.Map)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 BinaryClassDescriptor (org.apache.ignite.internal.binary.BinaryClassDescriptor)1 BinaryFieldMetadata (org.apache.ignite.internal.binary.BinaryFieldMetadata)1 BinaryObjectExImpl (org.apache.ignite.internal.binary.BinaryObjectExImpl)1 BinaryObjectImpl (org.apache.ignite.internal.binary.BinaryObjectImpl)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 IncompleteCacheObject (org.apache.ignite.internal.processors.cache.IncompleteCacheObject)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1