use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryContext method resolveMapper.
/**
* @param clsName Type name.
* @param cfg Binary configuration.
* @return Mapper according to configuration.
*/
private static BinaryInternalMapper resolveMapper(String clsName, BinaryConfiguration cfg) {
assert clsName != null;
if (cfg == null)
return DFLT_MAPPER;
BinaryIdMapper globalIdMapper = cfg.getIdMapper();
BinaryNameMapper globalNameMapper = cfg.getNameMapper();
Collection<BinaryTypeConfiguration> typeCfgs = cfg.getTypeConfigurations();
if (typeCfgs != null) {
for (BinaryTypeConfiguration typeCfg : typeCfgs) {
String typeCfgName = typeCfg.getTypeName();
// Pattern.
if (typeCfgName != null && typeCfgName.endsWith(".*")) {
String pkgName = typeCfgName.substring(0, typeCfgName.length() - 2);
int dotIndex = clsName.lastIndexOf('.');
if (dotIndex > 0) {
String typePkgName = clsName.substring(0, dotIndex);
if (pkgName.equals(typePkgName)) {
// Resolve mapper.
BinaryIdMapper idMapper = globalIdMapper;
if (typeCfg.getIdMapper() != null)
idMapper = typeCfg.getIdMapper();
BinaryNameMapper nameMapper = globalNameMapper;
if (typeCfg.getNameMapper() != null)
nameMapper = typeCfg.getNameMapper();
return resolveMapper(nameMapper, idMapper);
}
}
}
}
}
return resolveMapper(globalNameMapper, globalIdMapper);
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryContext method configure.
/**
* @param globalIdMapper ID mapper.
* @param globalSerializer Serializer.
* @param typeCfgs Type configurations.
* @throws BinaryObjectException In case of error.
*/
private void configure(BinaryNameMapper globalNameMapper, BinaryIdMapper globalIdMapper, BinarySerializer globalSerializer, Collection<BinaryTypeConfiguration> typeCfgs) throws BinaryObjectException {
TypeDescriptors descs = new TypeDescriptors();
Map<String, String> affFields = new HashMap<>();
if (!F.isEmpty(igniteCfg.getCacheKeyConfiguration())) {
for (CacheKeyConfiguration keyCfg : igniteCfg.getCacheKeyConfiguration()) affFields.put(keyCfg.getTypeName(), keyCfg.getAffinityKeyFieldName());
}
if (typeCfgs != null) {
for (BinaryTypeConfiguration typeCfg : typeCfgs) {
String clsName = typeCfg.getTypeName();
if (clsName == null)
throw new BinaryObjectException("Class name is required for binary type configuration.");
// Resolve mapper.
BinaryIdMapper idMapper = U.firstNotNull(typeCfg.getIdMapper(), globalIdMapper);
BinaryNameMapper nameMapper = U.firstNotNull(typeCfg.getNameMapper(), globalNameMapper);
BinarySerializer serializer = U.firstNotNull(typeCfg.getSerializer(), globalSerializer);
BinaryIdentityResolver identity = BinaryArrayIdentityResolver.instance();
BinaryInternalMapper mapper = resolveMapper(nameMapper, idMapper);
if (clsName.endsWith(".*")) {
String pkgName = clsName.substring(0, clsName.length() - 2);
for (String clsName0 : classesInPackage(pkgName)) {
String affField = affFields.remove(clsName0);
descs.add(clsName0, mapper, serializer, identity, affField, typeCfg.isEnum(), typeCfg.getEnumValues(), true);
}
} else {
String affField = affFields.remove(clsName);
descs.add(clsName, mapper, serializer, identity, affField, typeCfg.isEnum(), typeCfg.getEnumValues(), false);
}
}
}
for (TypeDescriptor desc : descs.descriptors()) registerUserType(desc.clsName, desc.mapper, desc.serializer, desc.identity, desc.affKeyFieldName, desc.isEnum, desc.enumMap);
BinaryInternalMapper globalMapper = resolveMapper(globalNameMapper, globalIdMapper);
// Put affinity field names for unconfigured types.
for (Map.Entry<String, String> entry : affFields.entrySet()) {
String typeName = entry.getKey();
int typeId = globalMapper.typeId(typeName);
affKeyFieldNames.putIfAbsent(typeId, entry.getValue());
}
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class CacheObjectBinaryProcessorImpl method start.
/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
if (marsh instanceof BinaryMarshaller) {
if (ctx.clientNode())
ctx.event().addLocalEventListener(clientDisconLsnr, EVT_CLIENT_NODE_DISCONNECTED);
transport = new BinaryMetadataTransport(metadataLocCache, ctx, log);
BinaryMetadataHandler metaHnd = new BinaryMetadataHandler() {
@Override
public void addMeta(int typeId, BinaryType newMeta) throws BinaryObjectException {
assert newMeta != null;
assert newMeta instanceof BinaryTypeImpl;
if (!discoveryStarted) {
BinaryMetadataHolder holder = metadataLocCache.get(typeId);
BinaryMetadata oldMeta = holder != null ? holder.metadata() : null;
BinaryMetadata mergedMeta = BinaryUtils.mergeMetadata(oldMeta, ((BinaryTypeImpl) newMeta).metadata());
if (oldMeta != mergedMeta)
metadataLocCache.putIfAbsent(typeId, new BinaryMetadataHolder(mergedMeta, 0, 0));
return;
}
BinaryMetadata newMeta0 = ((BinaryTypeImpl) newMeta).metadata();
CacheObjectBinaryProcessorImpl.this.addMeta(typeId, newMeta0.wrap(binaryCtx));
}
@Override
public BinaryType metadata(int typeId) throws BinaryObjectException {
return CacheObjectBinaryProcessorImpl.this.metadata(typeId);
}
@Override
public BinaryMetadata metadata0(int typeId) throws BinaryObjectException {
return CacheObjectBinaryProcessorImpl.this.metadata0(typeId);
}
@Override
public BinaryType metadata(int typeId, int schemaId) throws BinaryObjectException {
return CacheObjectBinaryProcessorImpl.this.metadata(typeId, schemaId);
}
};
BinaryMarshaller bMarsh0 = (BinaryMarshaller) marsh;
binaryCtx = new BinaryContext(metaHnd, ctx.config(), ctx.log(BinaryContext.class));
IgniteUtils.invoke(BinaryMarshaller.class, bMarsh0, "setBinaryContext", binaryCtx, ctx.config());
binaryMarsh = new GridBinaryMarshaller(binaryCtx);
binaries = new IgniteBinaryImpl(ctx, this);
if (!getBoolean(IGNITE_SKIP_CONFIGURATION_CONSISTENCY_CHECK)) {
BinaryConfiguration bCfg = ctx.config().getBinaryConfiguration();
if (bCfg != null) {
Map<String, Object> map = new HashMap<>();
map.put("globIdMapper", bCfg.getIdMapper() != null ? bCfg.getIdMapper().getClass().getName() : null);
map.put("globSerializer", bCfg.getSerializer() != null ? bCfg.getSerializer().getClass() : null);
map.put("compactFooter", bCfg.isCompactFooter());
if (bCfg.getTypeConfigurations() != null) {
Map<Object, Object> typeCfgsMap = new HashMap<>();
for (BinaryTypeConfiguration c : bCfg.getTypeConfigurations()) {
typeCfgsMap.put(c.getTypeName() != null, Arrays.asList(c.getIdMapper() != null ? c.getIdMapper().getClass() : null, c.getSerializer() != null ? c.getSerializer().getClass() : null, c.isEnum()));
if (c.isEnum())
BinaryUtils.validateEnumValues(c.getTypeName(), c.getEnumValues());
}
map.put("typeCfgs", typeCfgsMap);
}
ctx.addNodeAttribute(IgniteNodeAttributes.ATTR_BINARY_CONFIGURATION, map);
}
}
}
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method _testDefaultMapping.
/**
* @throws Exception If failed.
*/
public void _testDefaultMapping() throws Exception {
BinaryTypeConfiguration customMappingType = new BinaryTypeConfiguration(TestBinary.class.getName());
customMappingType.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
String typeName;
try {
Method mtd = BinaryContext.class.getDeclaredMethod("typeName", String.class);
mtd.setAccessible(true);
typeName = (String) mtd.invoke(null, clsName);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return typeName.toLowerCase().hashCode();
}
@Override
public int fieldId(int typeId, String fieldName) {
return fieldName.toLowerCase().hashCode();
}
});
BinaryMarshaller marsh1 = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName()), customMappingType));
TestBinary obj = binaryObject();
BinaryObjectImpl po = marshal(obj, marsh1);
BinaryMarshaller marsh2 = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName()), new BinaryTypeConfiguration(TestBinary.class.getName())));
po = marshal(obj, marsh2);
assertEquals(obj, po.deserialize());
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testCustomSerializerWithGlobal.
/**
* @throws Exception If failed.
*/
public void testCustomSerializerWithGlobal() throws Exception {
BinaryTypeConfiguration type1 = new BinaryTypeConfiguration(CustomSerializedObject1.class.getName());
BinaryTypeConfiguration type2 = new BinaryTypeConfiguration(CustomSerializedObject2.class.getName());
type2.setSerializer(new CustomSerializer2());
BinaryMarshaller marsh = binaryMarshaller(new CustomSerializer1(), Arrays.asList(type1, type2));
CustomSerializedObject1 obj1 = new CustomSerializedObject1(10);
BinaryObject po1 = marshal(obj1, marsh);
assertEquals(20, po1.<CustomSerializedObject1>deserialize().val);
CustomSerializedObject2 obj2 = new CustomSerializedObject2(10);
BinaryObject po2 = marshal(obj2, marsh);
assertEquals(30, po2.<CustomSerializedObject2>deserialize().val);
}
Aggregations