Search in sources :

Example 6 with GenericRecordBuilder

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"));
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with GenericRecordBuilder

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);
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with GenericRecordBuilder

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"));
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with GenericRecordBuilder

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();
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 10 with GenericRecordBuilder

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"));
}
Also used : GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

GenericRecordBuilder (com.hazelcast.nio.serialization.GenericRecordBuilder)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)15 QuickTest (com.hazelcast.test.annotation.QuickTest)15 Test (org.junit.Test)15 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)11 Data (com.hazelcast.internal.serialization.Data)7 SerializationService (com.hazelcast.internal.serialization.SerializationService)7 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)7 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)6 CompactTestUtil.createCompactGenericRecord (com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createCompactGenericRecord)5 InternalGenericRecord (com.hazelcast.internal.serialization.impl.InternalGenericRecord)4 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)3