Search in sources :

Example 21 with ClassDefinition

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

the class PortableSerializer method writeInternal.

void writeInternal(BufferObjectDataOutput out, Portable p) throws IOException {
    ClassDefinition cd = context.lookupOrRegisterClassDefinition(p);
    out.writeInt(cd.getVersion());
    DefaultPortableWriter writer = new DefaultPortableWriter(this, out, cd);
    p.writePortable(writer);
    writer.end();
}
Also used : ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 22 with ClassDefinition

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

the class ClassDefinitionWriter method writePortable.

@Override
public void writePortable(@Nonnull String fieldName, @Nullable 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 23 with ClassDefinition

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

the class ClassDefinitionWriter method writePortableArray.

@Override
public void writePortableArray(@Nonnull String fieldName, Portable[] portables) throws IOException {
    if (portables == null || portables.length == 0) {
        throw new HazelcastSerializationException("Cannot write null portable array without explicitly " + "registering class definition!");
    }
    Portable p = portables[0];
    int classId = p.getClassId();
    for (int i = 1; i < portables.length; i++) {
        if (portables[i].getClassId() != classId) {
            throw new IllegalArgumentException("Detected different class-ids in portable array!");
        }
    }
    int version = SerializationUtil.getPortableVersion(p, context.getVersion());
    ClassDefinition nestedClassDef = createNestedClassDef(p, new ClassDefinitionBuilder(p.getFactoryId(), classId, version));
    builder.addPortableArrayField(fieldName, nestedClassDef);
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 24 with ClassDefinition

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

the class SerializationServiceV1 method registerClassDefinitions.

public void registerClassDefinitions(Collection<ClassDefinition> classDefinitions) {
    Map<Integer, Map<Integer, ClassDefinition>> factoryMap = createHashMap(classDefinitions.size());
    for (ClassDefinition cd : classDefinitions) {
        int factoryId = cd.getFactoryId();
        Map<Integer, ClassDefinition> classDefMap = factoryMap.computeIfAbsent(factoryId, k -> new HashMap<>());
        int classId = cd.getClassId();
        if (classDefMap.containsKey(classId)) {
            throw new HazelcastSerializationException("Duplicate registration found for factory-id : " + factoryId + ", class-id " + classId);
        }
        classDefMap.put(classId, cd);
    }
    for (ClassDefinition classDefinition : classDefinitions) {
        registerClassDefinition(classDefinition, factoryMap);
    }
}
Also used : BigInteger(java.math.BigInteger) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) AbstractMap(java.util.AbstractMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 25 with ClassDefinition

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

the class SqlClientPortableQueryTest method setup.

@Before
public void setup() {
    MapConfig mapConfig = new MapConfig("default").setInMemoryFormat(inMemoryFormat);
    Config config = smallInstanceConfig().addMapConfig(mapConfig);
    if (clusterHasPortableConfig) {
        config.getSerializationConfig().addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory());
    }
    ClassDefinition childClassDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_CHILD_CLASS_ID, 0).addIntField("i").addIntArrayField("ia").build();
    ClassDefinition parentClassDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_PARENT_CLASS_ID, 0).addPortableField("child", childClassDefinition).addIntField("id").build();
    config.getSerializationConfig().addClassDefinition(childClassDefinition);
    config.getSerializationConfig().addClassDefinition(parentClassDefinition);
    factory.newHazelcastInstance(config);
}
Also used : MapConfig(com.hazelcast.config.MapConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) MapConfig(com.hazelcast.config.MapConfig) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Before(org.junit.Before)

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