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();
}
}
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;
}
}
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);
}
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);
}
Aggregations