Search in sources :

Example 1 with BinarySchema

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

the class BinaryObjectBuilderImpl method serializeTo.

/**
     * @param writer Writer.
     * @param serializer Serializer.
     */
@SuppressWarnings("ResultOfMethodCallIgnored")
void serializeTo(BinaryWriterExImpl writer, BinaryBuilderSerializer serializer) {
    try {
        writer.preWrite(registeredType ? null : clsNameToWrite);
        Set<Integer> remainsFlds = null;
        BinaryType meta = ctx.metadata(typeId);
        Map<String, BinaryFieldMetadata> fieldsMeta = null;
        if (reader != null && BinaryUtils.hasSchema(flags)) {
            BinarySchema schema = reader.schema();
            Map<Integer, Object> assignedFldsById;
            if (assignedVals != null) {
                assignedFldsById = U.newHashMap(assignedVals.size());
                for (Map.Entry<String, Object> entry : assignedVals.entrySet()) {
                    String name = entry.getKey();
                    Object val = entry.getValue();
                    int fieldId = ctx.fieldId(typeId, name);
                    assignedFldsById.put(fieldId, val);
                    if (val != REMOVED_FIELD_MARKER)
                        fieldsMeta = checkMetadata(meta, fieldsMeta, val, name, fieldId);
                }
                remainsFlds = assignedFldsById.keySet();
            } else
                assignedFldsById = Collections.emptyMap();
            // Get footer details.
            int fieldIdLen = BinaryUtils.fieldIdLength(flags);
            int fieldOffsetLen = BinaryUtils.fieldOffsetLength(flags);
            IgniteBiTuple<Integer, Integer> footer = BinaryUtils.footerAbsolute(reader, start);
            int footerPos = footer.get1();
            int footerEnd = footer.get2();
            // Get raw position.
            int rawPos = BinaryUtils.rawOffsetAbsolute(reader, start);
            // Position reader on data.
            reader.position(start + hdrLen);
            int idx = 0;
            while (reader.position() < rawPos) {
                int fieldId = schema.fieldId(idx++);
                int fieldLen = fieldPositionAndLength(footerPos, footerEnd, rawPos, fieldIdLen, fieldOffsetLen).get2();
                // Position where reader will be placed afterwards.
                int postPos = reader.position() + fieldLen;
                footerPos += fieldIdLen + fieldOffsetLen;
                if (assignedFldsById.containsKey(fieldId)) {
                    Object assignedVal = assignedFldsById.remove(fieldId);
                    if (assignedVal != REMOVED_FIELD_MARKER) {
                        writer.writeFieldId(fieldId);
                        serializer.writeValue(writer, assignedVal);
                    }
                } else {
                    int type = fieldLen != 0 ? reader.readByte(0) : 0;
                    if (fieldLen != 0 && !BinaryUtils.isPlainArrayType(type) && BinaryUtils.isPlainType(type)) {
                        writer.writeFieldId(fieldId);
                        writer.write(reader.array(), reader.position(), fieldLen);
                    } else {
                        writer.writeFieldId(fieldId);
                        Object val;
                        if (fieldLen == 0)
                            val = null;
                        else if (readCache == null) {
                            val = reader.parseValue();
                            assert reader.position() == postPos;
                        } else
                            val = readCache.get(fieldId);
                        serializer.writeValue(writer, val);
                    }
                }
                reader.position(postPos);
            }
        }
        if (assignedVals != null && (remainsFlds == null || !remainsFlds.isEmpty())) {
            for (Map.Entry<String, Object> entry : assignedVals.entrySet()) {
                Object val = entry.getValue();
                if (val == REMOVED_FIELD_MARKER)
                    continue;
                String name = entry.getKey();
                int fieldId = ctx.fieldId(typeId, name);
                if (remainsFlds != null && !remainsFlds.contains(fieldId))
                    continue;
                writer.writeFieldId(fieldId);
                serializer.writeValue(writer, val);
                if (reader == null)
                    // Metadata has already been checked.
                    fieldsMeta = checkMetadata(meta, fieldsMeta, val, name, fieldId);
            }
        }
        if (reader != null) {
            // Write raw data if any.
            int rawOff = BinaryUtils.rawOffsetAbsolute(reader, start);
            int footerStart = BinaryUtils.footerStartAbsolute(reader, start);
            if (rawOff < footerStart) {
                writer.rawWriter();
                writer.write(reader.array(), rawOff, footerStart - rawOff);
            }
            // Shift reader to the end of the object.
            reader.position(start + BinaryUtils.length(reader, start));
        }
        //noinspection NumberEquality
        writer.postWrite(true, registeredType);
        // Update metadata if needed.
        int schemaId = writer.schemaId();
        BinarySchemaRegistry schemaReg = ctx.schemaRegistry(typeId);
        if (schemaReg.schema(schemaId) == null) {
            String typeName = this.typeName;
            if (typeName == null) {
                assert meta != null;
                typeName = meta.typeName();
            }
            BinarySchema curSchema = writer.currentSchema();
            ctx.updateMetadata(typeId, new BinaryMetadata(typeId, typeName, fieldsMeta, ctx.affinityKeyFieldName(typeId), Collections.singleton(curSchema), false, null));
            schemaReg.addSchema(curSchema.schemaId(), curSchema);
        }
        // Update hash code after schema is written.
        writer.postWriteHashCode(registeredType ? null : clsNameToWrite);
    } finally {
        writer.popSchema();
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) BinarySchemaRegistry(org.apache.ignite.internal.binary.BinarySchemaRegistry) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinaryObject(org.apache.ignite.binary.BinaryObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with BinarySchema

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

the class BinaryObjectBuilderImpl method ensureReadCacheInit.

/**
     * Initialize read cache if needed.
     */
private void ensureReadCacheInit() {
    assert reader != null;
    if (readCache == null) {
        int fieldIdLen = BinaryUtils.fieldIdLength(flags);
        int fieldOffsetLen = BinaryUtils.fieldOffsetLength(flags);
        BinarySchema schema = reader.schema();
        Map<Integer, Object> readCache = new HashMap<>();
        IgniteBiTuple<Integer, Integer> footer = BinaryUtils.footerAbsolute(reader, start);
        int footerPos = footer.get1();
        int footerEnd = footer.get2();
        int rawPos = BinaryUtils.rawOffsetAbsolute(reader, start);
        int idx = 0;
        while (footerPos + fieldIdLen < footerEnd) {
            int fieldId = schema.fieldId(idx++);
            IgniteBiTuple<Integer, Integer> posAndLen = fieldPositionAndLength(footerPos, footerEnd, rawPos, fieldIdLen, fieldOffsetLen);
            Object val = reader.getValueQuickly(posAndLen.get1(), posAndLen.get2());
            readCache.put(fieldId, val);
            // Shift current footer position.
            footerPos += fieldIdLen + fieldOffsetLen;
        }
        this.readCache = readCache;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 3 with BinarySchema

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

the class PlatformContextImpl method writeSchema.

/** {@inheritDoc} */
@Override
public void writeSchema(BinaryRawWriterEx writer, int typeId, int schemaId) {
    BinarySchemaRegistry schemaReg = cacheObjProc.binaryContext().schemaRegistry(typeId);
    BinarySchema schema = schemaReg.schema(schemaId);
    if (schema == null) {
        BinaryTypeImpl meta = (BinaryTypeImpl) cacheObjProc.metadata(typeId);
        if (meta != null) {
            for (BinarySchema typeSchema : meta.metadata().schemas()) {
                if (schemaId == typeSchema.schemaId()) {
                    schema = typeSchema;
                    break;
                }
            }
        }
        if (schema != null)
            schemaReg.addSchema(schemaId, schema);
    }
    int[] fieldIds = schema == null ? null : schema.fieldIds();
    writer.writeIntArray(fieldIds);
}
Also used : BinaryTypeImpl(org.apache.ignite.internal.binary.BinaryTypeImpl) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinarySchemaRegistry(org.apache.ignite.internal.binary.BinarySchemaRegistry)

Example 4 with BinarySchema

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

BinarySchema (org.apache.ignite.internal.binary.BinarySchema)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 BinaryFieldMetadata (org.apache.ignite.internal.binary.BinaryFieldMetadata)2 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)2 BinarySchemaRegistry (org.apache.ignite.internal.binary.BinarySchemaRegistry)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BinaryType (org.apache.ignite.binary.BinaryType)1 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)1 BinaryRawReaderEx (org.apache.ignite.internal.binary.BinaryRawReaderEx)1 BinaryTypeImpl (org.apache.ignite.internal.binary.BinaryTypeImpl)1 PlatformReaderBiClosure (org.apache.ignite.internal.processors.platform.utils.PlatformReaderBiClosure)1 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)1