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)) descs.add(clsName0, mapper, serializer, identity, affFields.get(clsName0), typeCfg.isEnum(), typeCfg.getEnumValues(), true);
} else
descs.add(clsName, mapper, serializer, identity, affFields.get(clsName), 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());
}
addSystemClassAffinityKey(CollocatedSetItemKey.class);
addSystemClassAffinityKey(CollocatedQueueItemKey.class);
}
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 GridBinaryAffinityKeySelfTest method getConfiguration.
/** {@inheritDoc} */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration();
typeCfg.setTypeName(TestObject.class.getName());
BinaryConfiguration bCfg = new BinaryConfiguration();
bCfg.setTypeConfigurations(Collections.singleton(typeCfg));
cfg.setBinaryConfiguration(bCfg);
CacheKeyConfiguration keyCfg = new CacheKeyConfiguration(TestObject.class.getName(), "affKey");
CacheKeyConfiguration keyCfg2 = new CacheKeyConfiguration("TestObject2", "affKey");
cfg.setCacheKeyConfiguration(keyCfg, keyCfg2);
cfg.setMarshaller(new BinaryMarshaller());
if (!igniteInstanceName.equals(getTestIgniteInstanceName(GRID_CNT))) {
CacheConfiguration cacheCfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
cacheCfg.setCacheMode(PARTITIONED);
cfg.setCacheConfiguration(cacheCfg);
}
((TcpDiscoverySpi) cfg.getDiscoverySpi()).setIpFinder(ipFinder);
return cfg;
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class GridBinaryWildcardsSelfTest method testTypeConfigurationsWithNonGlobalMapperJar.
/**
* @throws Exception If failed.
*/
public void testTypeConfigurationsWithNonGlobalMapperJar() throws Exception {
BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(false), new BinaryIdMapper() {
@SuppressWarnings("IfMayBeConditional")
@Override
public int typeId(String clsName) {
if (clsName.endsWith("1"))
return 300;
else if (clsName.endsWith("2"))
return 400;
else
return -500;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
}, Arrays.asList(new BinaryTypeConfiguration("org.apache.ignite.internal.binary.test.*"), new BinaryTypeConfiguration("unknown.*")));
BinaryContext ctx = binaryContext(marsh);
Map<String, org.apache.ignite.internal.binary.BinaryInternalMapper> typeMappers = U.field(ctx, "cls2Mappers");
assertEquals(3, typeMappers.size());
assertFalse(((BinaryBasicNameMapper) typeMappers.get(CLASS1_FULL_NAME).nameMapper()).isSimpleName());
assertEquals(300, typeMappers.get(CLASS1_FULL_NAME).idMapper().typeId(CLASS1_FULL_NAME));
assertFalse(((BinaryBasicNameMapper) typeMappers.get(CLASS2_FULL_NAME).nameMapper()).isSimpleName());
assertEquals(400, typeMappers.get(CLASS2_FULL_NAME).idMapper().typeId(CLASS2_FULL_NAME));
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class GridBinaryWildcardsSelfTest method testTypeConfigurationsWithGlobalMapper.
/**
* @throws Exception If failed.
*/
public void testTypeConfigurationsWithGlobalMapper() throws Exception {
BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(false), new BinaryIdMapper() {
@SuppressWarnings("IfMayBeConditional")
@Override
public int typeId(String clsName) {
if (clsName.endsWith("1"))
return 300;
else if (clsName.endsWith("2"))
return 400;
else if (clsName.endsWith("InnerClass"))
return 500;
else
return -500;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
}, Arrays.asList(new BinaryTypeConfiguration("org.apache.ignite.internal.binary.test.*"), new BinaryTypeConfiguration("unknown.*")));
BinaryContext ctx = binaryContext(marsh);
Map<String, org.apache.ignite.internal.binary.BinaryInternalMapper> typeMappers = U.field(ctx, "cls2Mappers");
assertEquals(3, typeMappers.size());
assertEquals(300, typeMappers.get(CLASS1_FULL_NAME).idMapper().typeId(CLASS1_FULL_NAME));
assertEquals(400, typeMappers.get(CLASS2_FULL_NAME).idMapper().typeId(CLASS2_FULL_NAME));
assertEquals(500, typeMappers.get(INNER_CLASS_FULL_NAME).idMapper().typeId(INNER_CLASS_FULL_NAME));
}
Aggregations