use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.
the class PortableSerializer method readPortableGenericRecord.
@SuppressWarnings({ "checkstyle:MethodLength", "checkstyle:CyclomaticComplexity" })
private <T> T readPortableGenericRecord(BufferObjectDataInput in, int factoryId, int classId) throws IOException {
int version = in.readInt();
ClassDefinition cd = setupPositionAndDefinition(in, factoryId, classId, version);
PortableInternalGenericRecord reader = new PortableInternalGenericRecord(this, in, cd, false);
GenericRecordBuilder genericRecordBuilder = GenericRecordBuilder.portable(cd);
for (String fieldName : cd.getFieldNames()) {
switch(cd.getFieldType(fieldName)) {
case PORTABLE:
genericRecordBuilder.setGenericRecord(fieldName, reader.getGenericRecord(fieldName));
break;
case BYTE:
genericRecordBuilder.setInt8(fieldName, reader.getInt8(fieldName));
break;
case BOOLEAN:
genericRecordBuilder.setBoolean(fieldName, reader.getBoolean(fieldName));
break;
case CHAR:
genericRecordBuilder.setChar(fieldName, reader.getChar(fieldName));
break;
case SHORT:
genericRecordBuilder.setInt16(fieldName, reader.getInt16(fieldName));
break;
case INT:
genericRecordBuilder.setInt32(fieldName, reader.getInt32(fieldName));
break;
case LONG:
genericRecordBuilder.setInt64(fieldName, reader.getInt64(fieldName));
break;
case FLOAT:
genericRecordBuilder.setFloat32(fieldName, reader.getFloat32(fieldName));
break;
case DOUBLE:
genericRecordBuilder.setFloat64(fieldName, reader.getFloat64(fieldName));
break;
case UTF:
genericRecordBuilder.setString(fieldName, reader.getString(fieldName));
break;
case DECIMAL:
genericRecordBuilder.setDecimal(fieldName, reader.getDecimal(fieldName));
break;
case TIME:
genericRecordBuilder.setTime(fieldName, reader.getTime(fieldName));
break;
case DATE:
genericRecordBuilder.setDate(fieldName, reader.getDate(fieldName));
break;
case TIMESTAMP:
genericRecordBuilder.setTimestamp(fieldName, reader.getTimestamp(fieldName));
break;
case TIMESTAMP_WITH_TIMEZONE:
genericRecordBuilder.setTimestampWithTimezone(fieldName, reader.getTimestampWithTimezone(fieldName));
break;
case PORTABLE_ARRAY:
genericRecordBuilder.setArrayOfGenericRecord(fieldName, reader.getArrayOfGenericRecord(fieldName));
break;
case BYTE_ARRAY:
genericRecordBuilder.setArrayOfInt8(fieldName, reader.getArrayOfInt8(fieldName));
break;
case BOOLEAN_ARRAY:
genericRecordBuilder.setArrayOfBoolean(fieldName, reader.getArrayOfBoolean(fieldName));
break;
case CHAR_ARRAY:
genericRecordBuilder.setArrayOfChar(fieldName, reader.getArrayOfChar(fieldName));
break;
case SHORT_ARRAY:
genericRecordBuilder.setArrayOfInt16(fieldName, reader.getArrayOfInt16(fieldName));
break;
case INT_ARRAY:
genericRecordBuilder.setArrayOfInt32(fieldName, reader.getArrayOfInt32(fieldName));
break;
case LONG_ARRAY:
genericRecordBuilder.setArrayOfInt64(fieldName, reader.getArrayOfInt64(fieldName));
break;
case FLOAT_ARRAY:
genericRecordBuilder.setArrayOfFloat32(fieldName, reader.getArrayOfFloat32(fieldName));
break;
case DOUBLE_ARRAY:
genericRecordBuilder.setArrayOfFloat64(fieldName, reader.getArrayOfFloat64(fieldName));
break;
case UTF_ARRAY:
genericRecordBuilder.setArrayOfString(fieldName, reader.getArrayOfString(fieldName));
break;
case DECIMAL_ARRAY:
genericRecordBuilder.setArrayOfDecimal(fieldName, reader.getArrayOfDecimal(fieldName));
break;
case TIME_ARRAY:
genericRecordBuilder.setArrayOfTime(fieldName, reader.getArrayOfTime(fieldName));
break;
case DATE_ARRAY:
genericRecordBuilder.setArrayOfDate(fieldName, reader.getArrayOfDate(fieldName));
break;
case TIMESTAMP_ARRAY:
genericRecordBuilder.setArrayOfTimestamp(fieldName, reader.getArrayOfTimestamp(fieldName));
break;
case TIMESTAMP_WITH_TIMEZONE_ARRAY:
genericRecordBuilder.setArrayOfTimestampWithTimezone(fieldName, reader.getArrayOfTimestampWithTimezone(fieldName));
break;
default:
throw new IllegalStateException("Unexpected value: " + cd.getFieldType(fieldName));
}
}
reader.end();
return (T) genericRecordBuilder.build();
}
use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.
the class PortableContextImpl method readClassDefinition.
ClassDefinition readClassDefinition(BufferObjectDataInput in, int factoryId, int classId, int version) throws IOException {
boolean register = true;
ClassDefinitionBuilder builder = new ClassDefinitionBuilder(factoryId, classId, version);
// final position after portable is read
in.readInt();
// field count
int fieldCount = in.readInt();
int offset = in.position();
for (int i = 0; i < fieldCount; i++) {
int pos = in.readInt(offset + i * Bits.INT_SIZE_IN_BYTES);
in.position(pos);
short len = in.readShort();
char[] chars = new char[len];
for (int k = 0; k < len; k++) {
chars[k] = (char) in.readUnsignedByte();
}
FieldType type = FieldType.get(in.readByte());
String name = new String(chars);
int fieldFactoryId = 0;
int fieldClassId = 0;
int fieldVersion = version;
if (type == FieldType.PORTABLE) {
// is null
if (in.readBoolean()) {
register = false;
}
fieldFactoryId = in.readInt();
fieldClassId = in.readInt();
// TODO: what there's a null inner Portable field
if (register) {
fieldVersion = in.readInt();
readClassDefinition(in, fieldFactoryId, fieldClassId, fieldVersion);
}
} else if (type == FieldType.PORTABLE_ARRAY) {
int k = in.readInt();
fieldFactoryId = in.readInt();
fieldClassId = in.readInt();
// TODO: what there's a null inner Portable field
if (k > 0) {
int p = in.readInt();
in.position(p);
fieldVersion = in.readInt();
readClassDefinition(in, fieldFactoryId, fieldClassId, fieldVersion);
} else {
register = false;
}
}
builder.addField(new FieldDefinitionImpl(i, name, type, fieldFactoryId, fieldClassId, fieldVersion));
}
ClassDefinition classDefinition = builder.build();
if (register) {
classDefinition = registerClassDefinition(classDefinition);
}
return classDefinition;
}
use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.
the class PortableContextImpl method lookupOrRegisterClassDefinition.
@Override
public ClassDefinition lookupOrRegisterClassDefinition(Portable p) throws IOException {
int portableVersion = SerializationUtil.getPortableVersion(p, version);
ClassDefinition cd = lookupClassDefinition(p.getFactoryId(), p.getClassId(), portableVersion);
if (cd == null) {
ClassDefinitionWriter writer = new ClassDefinitionWriter(this, p.getFactoryId(), p.getClassId(), portableVersion);
p.writePortable(writer);
cd = writer.registerAndGet();
}
return cd;
}
use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.
the class ClassDefinitionWriter method writeNullPortable.
@Override
public void writeNullPortable(@Nonnull String fieldName, int factoryId, int classId) {
final ClassDefinition nestedClassDef = context.lookupClassDefinition(factoryId, classId, context.getVersion());
if (nestedClassDef == null) {
throw new HazelcastSerializationException("Cannot write null portable without explicitly " + "registering class definition!");
}
builder.addPortableField(fieldName, nestedClassDef);
}
use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.
the class PortableTest method testClassDefinitionLookup.
static void testClassDefinitionLookup(InternalSerializationService ss) throws IOException {
NamedPortableV2 p = new NamedPortableV2("test-portable", 123456789, 500);
Data data = ss.toData(p);
PortableContext portableContext = ss.getPortableContext();
ClassDefinition cd = portableContext.lookupClassDefinition(data);
assertNotNull(cd);
assertEquals(p.getFactoryId(), cd.getFactoryId());
assertEquals(p.getClassId(), cd.getClassId());
assertEquals(p.getClassVersion(), cd.getVersion());
}
Aggregations