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