Search in sources :

Example 6 with BinaryMetadata

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

the class CacheObjectBinaryProcessorImpl method buildEnum.

/** {@inheritDoc} */
@Override
public BinaryObject buildEnum(String typeName, String name) throws BinaryObjectException {
    A.notNullOrEmpty(typeName, "enum type name");
    A.notNullOrEmpty(name, "enum name");
    int typeId = binaryCtx.typeId(typeName);
    BinaryMetadata metadata = metadata0(typeId);
    if (metadata == null)
        throw new BinaryObjectException("Failed to get metadata for type [typeId=" + typeId + ", typeName='" + typeName + "']");
    Integer ordinal = metadata.getEnumOrdinalByName(name);
    typeName = binaryCtx.userTypeName(typeName);
    if (ordinal == null)
        throw new BinaryObjectException("Failed to resolve enum ordinal by name [typeId=" + typeId + ", typeName='" + typeName + "', name='" + name + "']");
    return new BinaryEnumObjectImpl(binaryCtx, typeId, null, ordinal);
}
Also used : BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 7 with BinaryMetadata

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

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

the class PlatformContextImpl method processMetadata.

/** {@inheritDoc} */
@SuppressWarnings("ConstantConditions")
@Override
public void processMetadata(BinaryRawReaderEx reader) {
    Collection<BinaryMetadata> metas = PlatformUtils.readCollection(reader, new PlatformReaderClosure<BinaryMetadata>() {

        @Override
        public BinaryMetadata read(BinaryRawReaderEx reader) {
            int typeId = reader.readInt();
            String typeName = reader.readString();
            String affKey = reader.readString();
            Map<String, BinaryFieldMetadata> fields = PlatformUtils.readLinkedMap(reader, new PlatformReaderBiClosure<String, BinaryFieldMetadata>() {

                @Override
                public IgniteBiTuple<String, BinaryFieldMetadata> read(BinaryRawReaderEx reader) {
                    String name = reader.readString();
                    int typeId = reader.readInt();
                    int fieldId = reader.readInt();
                    return new IgniteBiTuple<String, BinaryFieldMetadata>(name, new BinaryFieldMetadata(typeId, fieldId));
                }
            });
            Map<String, Integer> enumMap = null;
            boolean isEnum = reader.readBoolean();
            if (isEnum) {
                int size = reader.readInt();
                enumMap = new LinkedHashMap<>(size);
                for (int idx = 0; idx < size; idx++) enumMap.put(reader.readString(), reader.readInt());
            }
            // Read schemas
            int schemaCnt = reader.readInt();
            List<BinarySchema> schemas = null;
            if (schemaCnt > 0) {
                schemas = new ArrayList<>(schemaCnt);
                for (int i = 0; i < schemaCnt; i++) {
                    int id = reader.readInt();
                    int fieldCnt = reader.readInt();
                    List<Integer> fieldIds = new ArrayList<>(fieldCnt);
                    for (int j = 0; j < fieldCnt; j++) fieldIds.add(reader.readInt());
                    schemas.add(new BinarySchema(id, fieldIds));
                }
            }
            return new BinaryMetadata(typeId, typeName, fields, affKey, schemas, isEnum, enumMap);
        }
    });
    BinaryContext binCtx = cacheObjProc.binaryContext();
    for (BinaryMetadata meta : metas) binCtx.updateMetadata(meta.typeId(), meta);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) PlatformReaderBiClosure(org.apache.ignite.internal.processors.platform.utils.PlatformReaderBiClosure) ArrayList(java.util.ArrayList) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) LinkedHashMap(java.util.LinkedHashMap) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) List(java.util.List) ArrayList(java.util.ArrayList) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)8 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 BinaryFieldMetadata (org.apache.ignite.internal.binary.BinaryFieldMetadata)3 BinaryTypeImpl (org.apache.ignite.internal.binary.BinaryTypeImpl)3 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)2 BinaryType (org.apache.ignite.binary.BinaryType)2 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)2 BinarySchema (org.apache.ignite.internal.binary.BinarySchema)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)1 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)1