Search in sources :

Example 61 with ClassDefinition

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

Example 62 with ClassDefinition

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

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

Example 64 with ClassDefinition

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

Example 65 with ClassDefinition

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

Aggregations

ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)70 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)37 Test (org.junit.Test)25 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)16 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)15 QuickTest (com.hazelcast.test.annotation.QuickTest)15 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)10 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)10 FieldDefinition (com.hazelcast.nio.serialization.FieldDefinition)7 GenericRecordBuilder (com.hazelcast.nio.serialization.GenericRecordBuilder)7 Parameters (junitparams.Parameters)7 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)6 MappingField (com.hazelcast.sql.impl.schema.MappingField)6 Portable (com.hazelcast.nio.serialization.Portable)5 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 SerializationConfig (com.hazelcast.config.SerializationConfig)3 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)3 Data (com.hazelcast.internal.serialization.Data)3 BufferObjectDataInput (com.hazelcast.nio.BufferObjectDataInput)3