Search in sources :

Example 1 with BinaryObjectExImpl

use of org.apache.ignite.internal.binary.BinaryObjectExImpl 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.getClass().isEnum()) {
        String clsName = val.getClass().getName();
        int typeId = writer.context().typeId(clsName);
        String typeName = writer.context().userTypeName(clsName);
        Object[] enumVals = val.getClass().getEnumConstants();
        Map<String, Integer> enumMap = new LinkedHashMap<>(enumVals.length);
        for (Object enumVal : enumVals) enumMap.put(((Enum) enumVal).name(), ((Enum) enumVal).ordinal());
        BinaryMetadata meta = new BinaryMetadata(typeId, typeName, null, null, null, true, enumMap);
        writer.context().updateMetadata(typeId, meta);
        // Need register class for marshaller to be able to deserialize enum value.
        writer.context().descriptorForClass(val.getClass(), 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 Object[]) {
        int compTypeId = writer.context().typeId(((Object[]) val).getClass().getComponentType().getName());
        if (val instanceof BinaryBuilderEnum[]) {
            writeArray(writer, GridBinaryMarshaller.ENUM_ARR, (Object[]) val, compTypeId);
            return;
        }
        if (((Object[]) val).getClass().getComponentType().isEnum()) {
            Enum[] enumArr = (Enum[]) val;
            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 : BinaryObjectExImpl(org.apache.ignite.internal.binary.BinaryObjectExImpl) IdentityHashMap(java.util.IdentityHashMap) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) LinkedHashMap(java.util.LinkedHashMap) BinaryObject(org.apache.ignite.binary.BinaryObject) Collection(java.util.Collection) BinaryObject(org.apache.ignite.binary.BinaryObject) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map)

Example 2 with BinaryObjectExImpl

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

the class QueryBinaryProperty method setValue.

/** {@inheritDoc} */
@Override
public void setValue(Object key, Object val, Object propVal) throws IgniteCheckedException {
    Object obj = key() ? key : val;
    if (obj == null)
        return;
    Object srcObj = obj;
    if (!(srcObj instanceof BinaryObjectBuilder))
        throw new UnsupportedOperationException("Individual properties can be set for binary builders only");
    if (parent != null)
        obj = parent.value(key, val);
    boolean needsBuild = false;
    if (obj instanceof BinaryObjectExImpl) {
        if (parent == null)
            throw new UnsupportedOperationException("Individual properties can be set for binary builders only");
        needsBuild = true;
        obj = ((BinaryObjectExImpl) obj).toBuilder();
    }
    if (!(obj instanceof BinaryObjectBuilder))
        throw new UnsupportedOperationException("Individual properties can be set for binary builders only");
    setValue0((BinaryObjectBuilder) obj, propName, propVal, type());
    if (needsBuild) {
        obj = ((BinaryObjectBuilder) obj).build();
        assert parent != null;
        // And now let's set this newly constructed object to parent
        setValue0((BinaryObjectBuilder) srcObj, parent.propName, obj, obj.getClass());
    }
}
Also used : BinaryObjectExImpl(org.apache.ignite.internal.binary.BinaryObjectExImpl) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Aggregations

BinaryObject (org.apache.ignite.binary.BinaryObject)2 BinaryObjectExImpl (org.apache.ignite.internal.binary.BinaryObjectExImpl)2 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)1 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)1