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();
}
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\"");
}
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;
}
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);
}
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;
}
}
Aggregations