use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.
the class GenericRecordBuilderTest method testWritingSameFieldMultipleTimes.
@Test
public void testWritingSameFieldMultipleTimes() {
ClassDefinition namedPortableClassDefinition = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE).addStringField("name").addIntField("myint").build();
GenericRecordBuilder builder = GenericRecordBuilder.portable(namedPortableClassDefinition);
builder.setString("name", "foo");
builder.setInt32("myint", 123);
assertThrows(HazelcastSerializationException.class, () -> builder.setString("name", "foo2"));
}
use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.
the class GenericRecordBuilderTest method testUnwrittenFieldsThrowException.
@Test
public void testUnwrittenFieldsThrowException() {
ClassDefinition classDefinition = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE).addStringField("name").addIntField("myint").build();
GenericRecordBuilder builder = GenericRecordBuilder.portable(classDefinition);
builder.setInt32("myint", 1);
assertThrows(HazelcastSerializationException.class, builder::build);
}
use of com.hazelcast.nio.serialization.GenericRecordBuilder in project hazelcast by hazelcast.
the class GenericRecordBuilderTest method testWritingToNonExistingField.
@Test
public void testWritingToNonExistingField() {
ClassDefinition classDefinition = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE).addStringField("name").addIntField("myint").build();
GenericRecordBuilder builder = GenericRecordBuilder.portable(classDefinition);
assertThrows(HazelcastSerializationException.class, () -> builder.setString("nonExistingField", "foo3"));
}
use of com.hazelcast.nio.serialization.GenericRecordBuilder 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.GenericRecordBuilder in project hazelcast by hazelcast.
the class GenericRecordBuilderTest method testOverwritingSameFieldMultipleTimes.
@Test
public void testOverwritingSameFieldMultipleTimes() {
ClassDefinition namedPortableClassDefinition = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE).addStringField("name").addIntField("myint").build();
GenericRecord record = GenericRecordBuilder.portable(namedPortableClassDefinition).setString("name", "foo").setInt32("myint", 123).build();
GenericRecordBuilder builder = record.cloneWithBuilder().setString("name", "foo2");
assertThrows(HazelcastSerializationException.class, () -> builder.setString("name", "foo3"));
}
Aggregations