Search in sources :

Example 51 with ClassDefinition

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

the class PortableUpsertTargetTest method when_injectNonExistingPropertyNullValue_then_succeeds.

@Test
public void when_injectNonExistingPropertyNullValue_then_succeeds() throws IOException {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).build();
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    UpsertInjector injector = target.createInjector("field", QueryDataType.INT);
    target.init();
    injector.set(null);
    Object portable = target.conclude();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    InternalGenericRecord record = ss.readAsInternalGenericRecord(ss.toData(portable));
    assertThat(record).isNotNull();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Test(org.junit.Test)

Example 52 with ClassDefinition

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

the class PortableUpsertTargetTest method when_injectNonExistingPropertyValue_then_throws.

@Test
public void when_injectNonExistingPropertyValue_then_throws() {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).build();
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    UpsertInjector injector = target.createInjector("field", QueryDataType.INT);
    target.init();
    assertThatThrownBy(() -> injector.set("1")).isInstanceOf(QueryException.class).hasMessageContaining("Unable to inject a non-null value to \"field\"");
}
Also used : QueryException(com.hazelcast.sql.impl.QueryException) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Test(org.junit.Test)

Example 53 with ClassDefinition

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

the class SqlBasicTest method serializationConfig.

public static SerializationConfig serializationConfig() {
    SerializationConfig serializationConfig = new SerializationConfig();
    ClassDefinition nestedClassDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_NESTED_CLASS_ID, 0).addIntField("val").build();
    ClassDefinition valueClassDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_VALUE_CLASS_ID, 0).addBooleanField(portableFieldName("booleanVal")).addByteField(portableFieldName("tinyIntVal")).addShortField(portableFieldName("smallIntVal")).addIntField(portableFieldName("intVal")).addLongField(portableFieldName("bigIntVal")).addFloatField(portableFieldName("realVal")).addDoubleField(portableFieldName("doubleVal")).addDecimalField(portableFieldName("decimalVal")).addCharField(portableFieldName("charVal")).addStringField(portableFieldName("varcharVal")).addDateField(portableFieldName("dateVal")).addTimeField(portableFieldName("timeVal")).addTimestampField(portableFieldName("timestampVal")).addTimestampWithTimezoneField(portableFieldName("tsTzOffsetDateTimeVal")).addPortableField(portableFieldName("portableVal"), nestedClassDefinition).addStringField(portableFieldName("nullVal")).build();
    ClassDefinition keyClassDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_KEY_CLASS_ID, 0).addLongField(portableFieldName("key")).build();
    serializationConfig.addClassDefinition(nestedClassDefinition);
    serializationConfig.addClassDefinition(valueClassDefinition);
    serializationConfig.addClassDefinition(keyClassDefinition);
    serializationConfig.addPortableFactory(PORTABLE_FACTORY_ID, classId -> {
        if (classId == PORTABLE_KEY_CLASS_ID) {
            return new PortablePojoKey();
        } else if (classId == PORTABLE_VALUE_CLASS_ID) {
            return new PortablePojo();
        } else if (classId == PORTABLE_NESTED_CLASS_ID) {
            return new PortablePojoNested();
        }
        throw new IllegalArgumentException("Unsupported class ID: " + classId);
    });
    serializationConfig.addDataSerializableFactory(IDS_FACTORY_ID, classId -> {
        if (classId == IDS_KEY_CLASS_ID) {
            return new IdentifiedDataSerializablePojoKey();
        } else if (classId == IDS_VALUE_CLASS_ID) {
            return new IdentifiedDataSerializablePojo();
        }
        throw new IllegalArgumentException("Unsupported class ID: " + classId);
    });
    return serializationConfig;
}
Also used : SerializationConfig(com.hazelcast.config.SerializationConfig) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 54 with ClassDefinition

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

the class MetadataPortableResolver method resolveAndValidateFields.

@Override
public Stream<MappingField> resolveAndValidateFields(boolean isKey, List<MappingField> userFields, Map<String, String> options, InternalSerializationService serializationService) {
    Map<QueryPath, MappingField> userFieldsByPath = extractFields(userFields, isKey);
    ClassDefinition classDefinition = findClassDefinition(isKey, options, serializationService);
    return userFields.isEmpty() ? resolveFields(isKey, classDefinition) : resolveAndValidateFields(isKey, userFieldsByPath, classDefinition);
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition)

Example 55 with ClassDefinition

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

the class SampleMetadataResolver method resolve.

@Nullable
@SuppressWarnings("checkstyle:returncount")
static Metadata resolve(InternalSerializationService ss, Object target, boolean key) {
    try {
        if (target instanceof Data) {
            Data data = (Data) target;
            if (data.isPortable()) {
                ClassDefinition classDefinition = ss.getPortableContext().lookupClassDefinition(data);
                return resolvePortable(classDefinition, key);
            } else if (data.isCompact()) {
                return resolveCompact(ss.extractSchemaFromData(data), key);
            } else if (data.isJson()) {
                return null;
            } else {
                return resolveJava(ss.toObject(data).getClass(), key);
            }
        } else if (target instanceof VersionedPortable) {
            VersionedPortable portable = (VersionedPortable) target;
            ClassDefinition classDefinition = ss.getPortableContext().lookupClassDefinition(portable.getFactoryId(), portable.getClassId(), portable.getClassVersion());
            return resolvePortable(classDefinition, key);
        } else if (target instanceof Portable) {
            Portable portable = (Portable) target;
            ClassDefinition classDefinition = ss.getPortableContext().lookupClassDefinition(portable.getFactoryId(), portable.getClassId(), 0);
            return resolvePortable(classDefinition, key);
        } else if (target instanceof PortableGenericRecord) {
            return resolvePortable(((PortableGenericRecord) target).getClassDefinition(), key);
        } else if (target instanceof CompactGenericRecord) {
            return resolveCompact(((CompactGenericRecord) target).getSchema(), key);
        } else if (ss.isCompactSerializable(target)) {
            Schema schema = ss.extractSchemaFromObject(target);
            return resolveCompact(schema, key);
        } else if (target instanceof HazelcastJsonValue) {
            return null;
        } else {
            return resolveJava(target.getClass(), key);
        }
    } catch (Exception e) {
        return null;
    }
}
Also used : VersionedPortable(com.hazelcast.nio.serialization.VersionedPortable) Portable(com.hazelcast.nio.serialization.Portable) PortableGenericRecord(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecord) Schema(com.hazelcast.internal.serialization.impl.compact.Schema) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) CompactGenericRecord(com.hazelcast.internal.serialization.impl.compact.CompactGenericRecord) Data(com.hazelcast.internal.serialization.Data) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) VersionedPortable(com.hazelcast.nio.serialization.VersionedPortable) Nullable(javax.annotation.Nullable)

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