use of org.apache.ignite.internal.binary.BinaryFieldMetadata 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();
}
}
use of org.apache.ignite.internal.binary.BinaryFieldMetadata in project ignite by apache.
the class PlatformContextImpl method writeMetadata0.
/**
* Write binary metadata.
*
* @param writer Writer.
* @param typeId Type id.
* @param meta Metadata.
*/
private void writeMetadata0(BinaryRawWriterEx writer, int typeId, BinaryType meta) {
if (meta == null)
writer.writeBoolean(false);
else {
writer.writeBoolean(true);
BinaryMetadata meta0 = ((BinaryTypeImpl) meta).metadata();
Map<String, BinaryFieldMetadata> fields = meta0.fieldsMap();
writer.writeInt(typeId);
writer.writeString(meta.typeName());
writer.writeString(meta.affinityKeyFieldName());
writer.writeInt(fields.size());
for (Map.Entry<String, BinaryFieldMetadata> e : fields.entrySet()) {
writer.writeString(e.getKey());
writer.writeInt(e.getValue().typeId());
writer.writeInt(e.getValue().fieldId());
}
if (meta.isEnum()) {
writer.writeBoolean(true);
Map<String, Integer> enumMap = meta0.enumMap();
writer.writeInt(enumMap.size());
for (Map.Entry<String, Integer> e : enumMap.entrySet()) {
writer.writeString(e.getKey());
writer.writeInt(e.getValue());
}
} else
writer.writeBoolean(false);
}
}
use of org.apache.ignite.internal.binary.BinaryFieldMetadata in project ignite by apache.
the class BinaryObjectBuilderImpl method checkMetadata.
/**
* Checks metadata when a BinaryObject is being serialized.
*
* @param meta Current metadata.
* @param fieldsMeta Map holding metadata information that has to be updated.
* @param newVal Field value being serialized.
* @param name Field name.
* @param fieldId Field ID.
*/
private Map<String, BinaryFieldMetadata> checkMetadata(BinaryType meta, Map<String, BinaryFieldMetadata> fieldsMeta, Object newVal, String name, int fieldId) {
String oldFldTypeName = meta == null ? null : meta.fieldTypeName(name);
boolean nullFieldVal = false;
int newFldTypeId;
if (newVal instanceof BinaryValueWithType) {
newFldTypeId = ((BinaryValueWithType) newVal).typeId();
if (((BinaryValueWithType) newVal).value() == null)
nullFieldVal = true;
} else // Detect Enum and Enum array type.
if (newVal instanceof BinaryEnumObjectImpl)
newFldTypeId = GridBinaryMarshaller.ENUM;
else if (newVal.getClass().isArray() && newVal.getClass().getComponentType() == BinaryObject.class) {
BinaryObject[] arr = (BinaryObject[]) newVal;
newFldTypeId = arr.length > 0 && arr[0] instanceof BinaryEnumObjectImpl ? GridBinaryMarshaller.ENUM_ARR : GridBinaryMarshaller.OBJ_ARR;
} else
newFldTypeId = BinaryUtils.typeByClass(newVal.getClass());
if (oldFldTypeName == null) {
// It's a new field, we have to add it to metadata.
if (fieldsMeta == null) {
if (BinaryUtils.FIELDS_SORTED_ORDER)
fieldsMeta = new TreeMap<>();
else
fieldsMeta = new LinkedHashMap<>();
}
fieldsMeta.put(name, new BinaryFieldMetadata(newFldTypeId, fieldId));
} else if (!nullFieldVal) {
String newFldTypeName = BinaryUtils.fieldTypeName(newFldTypeId);
if (!F.eq(newFldTypeName, oldFldTypeName)) {
throw new BinaryObjectException("Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + ", fieldName=" + name + ", fieldType=" + oldFldTypeName + ", assignedValueType=" + newFldTypeName + ']');
}
}
return fieldsMeta;
}
use of org.apache.ignite.internal.binary.BinaryFieldMetadata 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);
}
Aggregations