Search in sources :

Example 1 with MutableSingletonList

use of org.apache.ignite.internal.util.MutableSingletonList in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method marshalToBinary.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public Object marshalToBinary(@Nullable Object obj) 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]);
        return pArr;
    }
    if (obj instanceof IgniteBiTuple) {
        IgniteBiTuple tup = (IgniteBiTuple) obj;
        if (obj instanceof T2)
            return new T2<>(marshalToBinary(tup.get1()), marshalToBinary(tup.get2()));
        return new IgniteBiTuple<>(marshalToBinary(tup.get1()), marshalToBinary(tup.get2()));
    }
    {
        Collection<Object> pCol = BinaryUtils.newKnownCollection(obj);
        if (pCol != null) {
            Collection<?> col = (Collection<?>) obj;
            for (Object item : col) pCol.add(marshalToBinary(item));
            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()), marshalToBinary(e.getValue()));
            return pMap;
        }
    }
    if (obj instanceof Map.Entry) {
        Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj;
        return new GridMapEntry<>(marshalToBinary(e.getKey()), marshalToBinary(e.getValue()));
    }
    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);
    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 : 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) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) MutableSingletonList(org.apache.ignite.internal.util.MutableSingletonList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 2 with MutableSingletonList

use of org.apache.ignite.internal.util.MutableSingletonList 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

Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 BinaryObjectImpl (org.apache.ignite.internal.binary.BinaryObjectImpl)2 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 MutableSingletonList (org.apache.ignite.internal.util.MutableSingletonList)2 GridMapEntry (org.apache.ignite.internal.util.lang.GridMapEntry)2 T2 (org.apache.ignite.internal.util.typedef.T2)2 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)2 BinaryArray (org.apache.ignite.internal.binary.BinaryArray)1 BinaryClassDescriptor (org.apache.ignite.internal.binary.BinaryClassDescriptor)1 BinaryEnumArray (org.apache.ignite.internal.binary.BinaryEnumArray)1 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)1 IncompleteCacheObject (org.apache.ignite.internal.processors.cache.IncompleteCacheObject)1