Search in sources :

Example 36 with ClassDefinition

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

the class PortableSerializer method createReader.

public DefaultPortableReader createReader(BufferObjectDataInput in, int factoryId, int classId, int version, int portableVersion) throws IOException {
    ClassDefinition cd = setupPositionAndDefinition(in, factoryId, classId, version);
    DefaultPortableReader reader;
    if (portableVersion == cd.getVersion()) {
        reader = new DefaultPortableReader(this, in, cd);
    } else {
        reader = new MorphingPortableReader(this, in, cd);
    }
    return reader;
}
Also used : ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 37 with ClassDefinition

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

the class SerializationServiceV1 method registerClassDefinition.

protected void registerClassDefinition(ClassDefinition cd, Map<Integer, ClassDefinition> classDefMap, boolean checkClassDefErrors) {
    final 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 classId = fd.getClassId();
            ClassDefinition nestedCd = classDefMap.get(classId);
            if (nestedCd != null) {
                registerClassDefinition(nestedCd, classDefMap, checkClassDefErrors);
                portableContext.registerClassDefinition(nestedCd);
            } else if (checkClassDefErrors) {
                throw new HazelcastSerializationException("Could not find registered ClassDefinition for class-id: " + classId);
            }
        }
    }
    portableContext.registerClassDefinition(cd);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 38 with ClassDefinition

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

the class ClassDefinitionWriter method writePortable.

@Override
public void writePortable(String fieldName, Portable portable) throws IOException {
    if (portable == null) {
        throw new HazelcastSerializationException("Cannot write null portable without explicitly " + "registering class definition!");
    }
    int version = SerializationUtil.getPortableVersion(portable, context.getVersion());
    ClassDefinition nestedClassDef = createNestedClassDef(portable, new ClassDefinitionBuilder(portable.getFactoryId(), portable.getClassId(), version));
    builder.addPortableField(fieldName, nestedClassDef);
}
Also used : HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 39 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 = 0;
        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 40 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)

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