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