Search in sources :

Example 56 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class SerializationServiceV1 method registerClassDefinition.

private void registerClassDefinition(ClassDefinition cd, Map<Integer, Map<Integer, ClassDefinition>> factoryMap) {
    Set<String> fieldNames = cd.getFieldNames();
    for (String fieldName : fieldNames) {
        FieldDefinition fd = cd.getField(fieldName);
        if (fd.getType() == FieldType.PORTABLE || fd.getType() == FieldType.PORTABLE_ARRAY) {
            int factoryId = fd.getFactoryId();
            int classId = fd.getClassId();
            Map<Integer, ClassDefinition> classDefinitionMap = factoryMap.get(factoryId);
            if (classDefinitionMap != null) {
                ClassDefinition nestedCd = classDefinitionMap.get(classId);
                if (nestedCd != null) {
                    registerClassDefinition(nestedCd, factoryMap);
                    portableContext.registerClassDefinition(nestedCd);
                    continue;
                }
            }
            if (portableContext.shouldCheckClassDefinitionErrors()) {
                throw new HazelcastSerializationException("Could not find registered ClassDefinition for factory-id : " + factoryId + ", class-id " + classId);
            }
        }
    }
    portableContext.registerClassDefinition(cd);
}
Also used : BigInteger(java.math.BigInteger) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 57 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class PortableHookLoader method load.

private void load() {
    try {
        final Iterator<PortableHook> hooks = ServiceLoader.iterator(PortableHook.class, FACTORY_ID, classLoader);
        while (hooks.hasNext()) {
            PortableHook hook = hooks.next();
            final PortableFactory factory = hook.createFactory();
            if (factory != null) {
                register(hook.getFactoryId(), factory);
            }
            final Collection<ClassDefinition> defs = hook.getBuiltinDefinitions();
            if (defs != null && !defs.isEmpty()) {
                definitions.addAll(defs);
            }
        }
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
    if (configuredFactories != null) {
        for (Map.Entry<Integer, ? extends PortableFactory> entry : configuredFactories.entrySet()) {
            register(entry.getKey(), entry.getValue());
        }
    }
}
Also used : ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) Map(java.util.Map) HashMap(java.util.HashMap) PortableFactory(com.hazelcast.nio.serialization.PortableFactory)

Example 58 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class PortableSerializer method readAsInternalGenericRecord.

public InternalGenericRecord readAsInternalGenericRecord(ObjectDataInput in) throws IOException {
    int factoryId = in.readInt();
    int classId = in.readInt();
    int version = in.readInt();
    BufferObjectDataInput input = (BufferObjectDataInput) in;
    ClassDefinition cd = setupPositionAndDefinition(input, factoryId, classId, version);
    return new PortableInternalGenericRecord(this, input, cd, true);
}
Also used : ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) BufferObjectDataInput(com.hazelcast.internal.nio.BufferObjectDataInput)

Example 59 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class PortableSerializer method writePortableGenericRecord.

// Portable Generic Record
private void writePortableGenericRecord(ObjectDataOutput out, PortableGenericRecord record) throws IOException {
    ClassDefinition cd = record.getClassDefinition();
    out.writeInt(cd.getFactoryId());
    out.writeInt(cd.getClassId());
    writePortableGenericRecordInternal(out, record);
}
Also used : ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 60 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class PortableSerializer method writePortableGenericRecordInternal.

@SuppressWarnings({ "checkstyle:MethodLength", "checkstyle:CyclomaticComplexity" })
void writePortableGenericRecordInternal(ObjectDataOutput out, PortableGenericRecord record) throws IOException {
    ClassDefinition cd = record.getClassDefinition();
    // Class definition compatibility will be checked implicitly on the
    // register call below.
    context.registerClassDefinition(cd, context.shouldCheckClassDefinitionErrors());
    out.writeInt(cd.getVersion());
    BufferObjectDataOutput output = (BufferObjectDataOutput) out;
    DefaultPortableWriter writer = new DefaultPortableWriter(this, output, cd);
    Set<String> fieldNames = cd.getFieldNames();
    for (String fieldName : fieldNames) {
        switch(cd.getFieldType(fieldName)) {
            case PORTABLE:
                writer.writeGenericRecord(fieldName, record.getGenericRecord(fieldName));
                break;
            case BYTE:
                writer.writeByte(fieldName, record.getInt8(fieldName));
                break;
            case BOOLEAN:
                writer.writeBoolean(fieldName, record.getBoolean(fieldName));
                break;
            case CHAR:
                writer.writeChar(fieldName, record.getChar(fieldName));
                break;
            case SHORT:
                writer.writeShort(fieldName, record.getInt16(fieldName));
                break;
            case INT:
                writer.writeInt(fieldName, record.getInt32(fieldName));
                break;
            case LONG:
                writer.writeLong(fieldName, record.getInt64(fieldName));
                break;
            case FLOAT:
                writer.writeFloat(fieldName, record.getFloat32(fieldName));
                break;
            case DOUBLE:
                writer.writeDouble(fieldName, record.getFloat64(fieldName));
                break;
            case UTF:
                writer.writeString(fieldName, record.getString(fieldName));
                break;
            case DECIMAL:
                writer.writeDecimal(fieldName, record.getDecimal(fieldName));
                break;
            case TIME:
                writer.writeTime(fieldName, record.getTime(fieldName));
                break;
            case DATE:
                writer.writeDate(fieldName, record.getDate(fieldName));
                break;
            case TIMESTAMP:
                writer.writeTimestamp(fieldName, record.getTimestamp(fieldName));
                break;
            case TIMESTAMP_WITH_TIMEZONE:
                writer.writeTimestampWithTimezone(fieldName, record.getTimestampWithTimezone(fieldName));
                break;
            case PORTABLE_ARRAY:
                writer.writeGenericRecordArray(fieldName, record.getArrayOfGenericRecord(fieldName));
                break;
            case BYTE_ARRAY:
                writer.writeByteArray(fieldName, record.getArrayOfInt8(fieldName));
                break;
            case BOOLEAN_ARRAY:
                writer.writeBooleanArray(fieldName, record.getArrayOfBoolean(fieldName));
                break;
            case CHAR_ARRAY:
                writer.writeCharArray(fieldName, record.getArrayOfChar(fieldName));
                break;
            case SHORT_ARRAY:
                writer.writeShortArray(fieldName, record.getArrayOfInt16(fieldName));
                break;
            case INT_ARRAY:
                writer.writeIntArray(fieldName, record.getArrayOfInt32(fieldName));
                break;
            case LONG_ARRAY:
                writer.writeLongArray(fieldName, record.getArrayOfInt64(fieldName));
                break;
            case FLOAT_ARRAY:
                writer.writeFloatArray(fieldName, record.getArrayOfFloat32(fieldName));
                break;
            case DOUBLE_ARRAY:
                writer.writeDoubleArray(fieldName, record.getArrayOfFloat64(fieldName));
                break;
            case UTF_ARRAY:
                writer.writeStringArray(fieldName, record.getArrayOfString(fieldName));
                break;
            case DECIMAL_ARRAY:
                writer.writeDecimalArray(fieldName, record.getArrayOfDecimal(fieldName));
                break;
            case TIME_ARRAY:
                writer.writeTimeArray(fieldName, record.getArrayOfTime(fieldName));
                break;
            case DATE_ARRAY:
                writer.writeDateArray(fieldName, record.getArrayOfDate(fieldName));
                break;
            case TIMESTAMP_ARRAY:
                writer.writeTimestampArray(fieldName, record.getArrayOfTimestamp(fieldName));
                break;
            case TIMESTAMP_WITH_TIMEZONE_ARRAY:
                writer.writeTimestampWithTimezoneArray(fieldName, record.getArrayOfTimestampWithTimezone(fieldName));
                break;
            default:
                throw new IllegalStateException("Unexpected field type: " + cd.getFieldType(fieldName));
        }
    }
    writer.end();
}
Also used : BufferObjectDataOutput(com.hazelcast.internal.nio.BufferObjectDataOutput) 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