Search in sources :

Example 11 with ClassDefinition

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

the class MetadataPortableResolver method resolveClassDefinition.

@Nonnull
private static ClassDefinition resolveClassDefinition(boolean isKey, Map<String, String> options, Collection<MappingField> fields, InternalSerializationService serializationService) {
    Tuple3<Integer, Integer, Integer> settings = settings(isKey, options);
    // noinspection ConstantConditions
    ClassDefinition classDefinition = serializationService.getPortableContext().lookupClassDefinition(settings.f0(), settings.f1(), settings.f2());
    if (classDefinition != null) {
        return classDefinition;
    }
    ClassDefinitionBuilder classDefinitionBuilder = new ClassDefinitionBuilder(settings.f0(), settings.f1(), settings.f2());
    for (MappingField field : fields) {
        String name = field.name();
        QueryDataType type = field.type();
        switch(type.getTypeFamily()) {
            case BOOLEAN:
                classDefinitionBuilder.addBooleanField(name);
                break;
            case TINYINT:
                classDefinitionBuilder.addByteField(name);
                break;
            case SMALLINT:
                classDefinitionBuilder.addShortField(name);
                break;
            case INTEGER:
                classDefinitionBuilder.addIntField(name);
                break;
            case BIGINT:
                classDefinitionBuilder.addLongField(name);
                break;
            case REAL:
                classDefinitionBuilder.addFloatField(name);
                break;
            case DOUBLE:
                classDefinitionBuilder.addDoubleField(name);
                break;
            case DECIMAL:
                classDefinitionBuilder.addDecimalField(name);
                break;
            case VARCHAR:
                classDefinitionBuilder.addStringField(name);
                break;
            case TIME:
                classDefinitionBuilder.addTimeField(name);
                break;
            case DATE:
                classDefinitionBuilder.addDateField(name);
                break;
            case TIMESTAMP:
                classDefinitionBuilder.addTimestampField(name);
                break;
            case TIMESTAMP_WITH_TIME_ZONE:
                classDefinitionBuilder.addTimestampWithTimezoneField(name);
                break;
            default:
        }
    }
    return classDefinitionBuilder.build();
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Nonnull(javax.annotation.Nonnull)

Example 12 with ClassDefinition

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

the class MetadataPortableResolver method resolveMetadata.

@Override
public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Map<String, String> options, InternalSerializationService serializationService) {
    Map<QueryPath, MappingField> fieldsByPath = extractFields(resolvedFields, isKey);
    ClassDefinition clazz = resolveClassDefinition(isKey, options, fieldsByPath.values(), serializationService);
    return resolveMetadata(isKey, resolvedFields, fieldsByPath, clazz);
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 13 with ClassDefinition

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

the class MetadataPortableResolverTest method when_objectUsedForCurrentlyUnknownType_then_allowed.

@Test
@Parameters({ "true, __key", "false, this" })
public void when_objectUsedForCurrentlyUnknownType_then_allowed(boolean key, String prefix) {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    ClassDefinition nestedClassDef = new ClassDefinitionBuilder(1, 3, 4).addIntField("intField").build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addIntArrayField("intArrayField").addPortableField("nestedPortableField", nestedClassDef).build();
    ss.getPortableContext().registerClassDefinition(nestedClassDef);
    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()));
    // We normally don't allow mapping e.g. INT field as OBJECT. But we need to allow it for arrays and nested objects
    // due to backwards-compatibility.
    INSTANCE.resolveAndValidateFields(key, asList(field("intArrayField", QueryDataType.OBJECT, prefix + ".intArrayField"), field("nestedPortableField", QueryDataType.OBJECT, prefix + ".nestedPortableField")), options, ss);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) 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 14 with ClassDefinition

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

the class MetadataPortableResolverTest method test_resolveMetadataWithExistingClassDefinition.

@Test
@Parameters({ "true, __key", "false, this" })
public void test_resolveMetadataWithExistingClassDefinition(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()));
    KvMetadata metadata = INSTANCE.resolveMetadata(key, singletonList(field("field", QueryDataType.INT, prefix + ".field")), options, ss);
    assertThat(metadata.getFields()).containsExactly(new MapTableField("field", QueryDataType.INT, false, QueryPath.create(prefix + ".field")), new MapTableField(prefix, QueryDataType.OBJECT, true, QueryPath.create(prefix)));
    assertThat(metadata.getQueryTargetDescriptor()).isEqualTo(GenericQueryTargetDescriptor.DEFAULT);
    assertThat(metadata.getUpsertTargetDescriptor()).isEqualToComparingFieldByField(new PortableUpsertTargetDescriptor(classDefinition));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) PortableUpsertTargetDescriptor(com.hazelcast.jet.sql.impl.inject.PortableUpsertTargetDescriptor) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) KvMetadata(com.hazelcast.jet.sql.impl.connector.keyvalue.KvMetadata) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 15 with ClassDefinition

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

the class SampleMetadataResolverTest method test_versionedPortable.

@Test
public void test_versionedPortable() {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_CLASS_ID, PORTABLE_CLASS_VERSION).build();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().addClassDefinition(classDefinition).build();
    Metadata metadata = SampleMetadataResolver.resolve(ss, new VersionedPortableClass(), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, String.valueOf(PORTABLE_CLASS_VERSION)));
    metadata = SampleMetadataResolver.resolve(ss, ss.toData(new VersionedPortableClass()), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, String.valueOf(PORTABLE_CLASS_VERSION)));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) 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