Search in sources :

Example 1 with BinarySchemaRegistry

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

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

the class BinaryObjectBuilderImpl method serializeTo.

/**
 * @param writer Writer.
 * @param serializer Serializer.
 */
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));
        }
        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();
            String affFieldName0 = affFieldName;
            if (affFieldName0 == null)
                affFieldName0 = ctx.affinityKeyFieldName(typeId);
            ctx.registerUserClassName(typeId, typeName, writer.failIfUnregistered(), false, JAVA_ID);
            ctx.updateMetadata(typeId, new BinaryMetadata(typeId, typeName, fieldsMeta, affFieldName0, Collections.singleton(curSchema), false, null), writer.failIfUnregistered());
            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 3 with BinarySchemaRegistry

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

the class PlatformUtils method getSchema.

/**
 * Gets the schema.
 *
 * @param cacheObjProc Cache object processor.
 * @param typeId Type id.
 * @param schemaId Schema id.
 */
public static int[] getSchema(CacheObjectBinaryProcessorImpl cacheObjProc, int typeId, int schemaId) {
    assert cacheObjProc != null;
    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);
        }
    }
    return schema == null ? null : schema.fieldIds();
}
Also used : BinaryTypeImpl(org.apache.ignite.internal.binary.BinaryTypeImpl) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinarySchemaRegistry(org.apache.ignite.internal.binary.BinarySchemaRegistry)

Aggregations

BinarySchema (org.apache.ignite.internal.binary.BinarySchema)3 BinarySchemaRegistry (org.apache.ignite.internal.binary.BinarySchemaRegistry)3 BinaryTypeImpl (org.apache.ignite.internal.binary.BinaryTypeImpl)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 BinaryType (org.apache.ignite.binary.BinaryType)1 BinaryFieldMetadata (org.apache.ignite.internal.binary.BinaryFieldMetadata)1 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)1