Search in sources :

Example 1 with BinaryObjectImpl

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

the class PlatformCompute method executeJavaTask.

/**
 * Execute task taking arguments from the given reader.
 *
 * @param reader Reader.
 * @param async Execute asynchronously flag.
 * @return Task result.
 * @throws IgniteCheckedException On error.
 */
protected Object executeJavaTask(BinaryRawReaderEx reader, boolean async) throws IgniteCheckedException {
    String taskName = reader.readString();
    boolean keepBinary = reader.readBoolean();
    Object arg = reader.readObjectDetached();
    Collection<UUID> nodeIds = readNodeIds(reader);
    IgniteCompute compute0 = computeForTask(nodeIds);
    if (!keepBinary && (arg instanceof BinaryObjectImpl || arg instanceof BinaryArray))
        arg = ((BinaryObject) arg).deserialize();
    if (async)
        return readAndListenFuture(reader, new ComputeConvertingFuture(compute0.executeAsync(taskName, arg)));
    else
        return toBinary(compute0.execute(taskName, arg));
}
Also used : BinaryArray(org.apache.ignite.internal.binary.BinaryArray) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) UUID(java.util.UUID) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 2 with BinaryObjectImpl

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

the class IgniteBinaryTest method testBinaryTypeWithIdOfMarshallerHeader.

/**
 * The purpose of this test is to check that message which begins with the same byte as marshaller header can
 * be correctly unmarshalled.
 */
@Test
public void testBinaryTypeWithIdOfMarshallerHeader() throws Exception {
    try (Ignite ignite = Ignition.start(Config.getServerConfiguration())) {
        try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
            int typeId = GridBinaryMarshaller.OBJ;
            BinaryObjectImpl binObj = (BinaryObjectImpl) ignite.binary().builder(Character.toString((char) typeId)).setField("dummy", "dummy").build();
            assertEquals(typeId, binObj.typeId());
            BinaryType type = client.binary().type(typeId);
            assertEquals(binObj.type().typeName(), type.typeName());
        }
    }
}
Also used : BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinaryType(org.apache.ignite.binary.BinaryType) Ignite(org.apache.ignite.Ignite) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 3 with BinaryObjectImpl

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

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

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

the class BinaryObjectBuilderImpl method build.

/**
 * {@inheritDoc}
 */
@Override
public BinaryObject build() {
    try (BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx)) {
        Thread curThread = Thread.currentThread();
        if (curThread instanceof IgniteThread)
            writer.failIfUnregistered(((IgniteThread) curThread).isForbiddenToRequestBinaryMetadata());
        writer.typeId(typeId);
        BinaryBuilderSerializer serializationCtx = new BinaryBuilderSerializer();
        serializationCtx.registerObjectWriting(this, 0);
        serializeTo(writer, serializationCtx);
        byte[] arr = writer.array();
        return new BinaryObjectImpl(ctx, arr, 0);
    }
}
Also used : BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) IgniteThread(org.apache.ignite.thread.IgniteThread) IgniteThread(org.apache.ignite.thread.IgniteThread)

Aggregations

BinaryObjectImpl (org.apache.ignite.internal.binary.BinaryObjectImpl)9 BinaryObject (org.apache.ignite.binary.BinaryObject)4 Test (org.junit.Test)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 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 BinaryArray (org.apache.ignite.internal.binary.BinaryArray)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 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1