Search in sources :

Example 41 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 : PortableHook(com.hazelcast.internal.serialization.PortableHook) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) Map(java.util.Map) HashMap(java.util.HashMap) PortableFactory(com.hazelcast.nio.serialization.PortableFactory)

Example 42 with ClassDefinition

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

the class PortableUtilsTest method validateArrayType_array.

@Test
public void validateArrayType_array() {
    // GIVEN
    ClassDefinition cd = mock(ClassDefinition.class);
    FieldDefinition fd = mock(FieldDefinition.class);
    // WHEN
    when(fd.getType()).thenReturn(FieldType.BOOLEAN_ARRAY);
    // THEN - nothing thrown
    PortableUtils.validateArrayType(cd, fd, "person.brain");
}
Also used : FieldDefinition(com.hazelcast.nio.serialization.FieldDefinition) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 43 with ClassDefinition

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

the class MetadataPortableResolverTest method test_resolveFieldsFromClassDefinition.

@Test
@Parameters({ "true, __key", "false, this" })
public void test_resolveFieldsFromClassDefinition(boolean key, String prefix) {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addStringField("string").addCharField("character").addBooleanField("boolean").addByteField("byte").addShortField("short").addIntField("int").addLongField("long").addFloatField("float").addDoubleField("double").addDecimalField("decimal").addTimeField("time").addDateField("date").addTimestampField("timestamp").addTimestampWithTimezoneField("timestampTz").addPortableField("object", new ClassDefinitionBuilder(4, 5, 6).build()).build();
    ss.getPortableContext().registerClassDefinition(classDefinition);
    Map<String, String> options = ImmutableMap.of((key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID), String.valueOf(classDefinition.getFactoryId()), (key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID), String.valueOf(classDefinition.getClassId()), (key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION), String.valueOf(classDefinition.getVersion()));
    Stream<MappingField> resolvedFields = INSTANCE.resolveAndValidateFields(key, emptyList(), options, ss);
    assertThat(resolvedFields).containsExactly(field("string", QueryDataType.VARCHAR, prefix + ".string"), field("character", QueryDataType.VARCHAR_CHARACTER, prefix + ".character"), field("boolean", QueryDataType.BOOLEAN, prefix + ".boolean"), field("byte", QueryDataType.TINYINT, prefix + ".byte"), field("short", QueryDataType.SMALLINT, prefix + ".short"), field("int", QueryDataType.INT, prefix + ".int"), field("long", QueryDataType.BIGINT, prefix + ".long"), field("float", QueryDataType.REAL, prefix + ".float"), field("double", QueryDataType.DOUBLE, prefix + ".double"), field("decimal", QueryDataType.DECIMAL, prefix + ".decimal"), field("time", QueryDataType.TIME, prefix + ".time"), field("date", QueryDataType.DATE, prefix + ".date"), field("timestamp", QueryDataType.TIMESTAMP, prefix + ".timestamp"), field("timestampTz", QueryDataType.TIMESTAMP_WITH_TZ_OFFSET_DATE_TIME, prefix + ".timestampTz"), field("object", QueryDataType.OBJECT, prefix + ".object"));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 44 with ClassDefinition

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

the class MetadataPortableResolverTest method when_typeMismatchBetweenDeclaredAndClassDefinitionField_then_throws.

@Test
@Parameters({ "true, __key", "false, this" })
public void when_typeMismatchBetweenDeclaredAndClassDefinitionField_then_throws(boolean key, String prefix) {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addIntField("field").build();
    ss.getPortableContext().registerClassDefinition(classDefinition);
    Map<String, String> options = ImmutableMap.of((key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID), String.valueOf(classDefinition.getFactoryId()), (key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID), String.valueOf(classDefinition.getClassId()), (key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION), String.valueOf(classDefinition.getVersion()));
    assertThatThrownBy(() -> INSTANCE.resolveAndValidateFields(key, singletonList(field("field", QueryDataType.VARCHAR, prefix + ".field")), options, ss)).isInstanceOf(QueryException.class).hasMessageContaining("Mismatch between declared and resolved type: field");
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) QueryException(com.hazelcast.sql.impl.QueryException) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 45 with ClassDefinition

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

the class MetadataPortableResolverTest method when_userDeclaresField_then_itsNameHasPrecedenceOverClassDefinitionOne.

@Test
@Parameters({ "true, __key", "false, this" })
public void when_userDeclaresField_then_itsNameHasPrecedenceOverClassDefinitionOne(boolean key, String prefix) {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addIntField("field").build();
    ss.getPortableContext().registerClassDefinition(classDefinition);
    Map<String, String> options = ImmutableMap.of((key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID), String.valueOf(classDefinition.getFactoryId()), (key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID), String.valueOf(classDefinition.getClassId()), (key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION), String.valueOf(classDefinition.getVersion()));
    Stream<MappingField> resolvedFields = INSTANCE.resolveAndValidateFields(key, singletonList(field("renamed_field", QueryDataType.INT, prefix + ".field")), options, ss);
    assertThat(resolvedFields).containsExactly(field("renamed_field", QueryDataType.INT, prefix + ".field"));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Parameters(junitparams.Parameters) Test(org.junit.Test)

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