use of org.apache.ignite.binary.BinarySerializer 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.BinarySerializer in project ignite by apache.
the class BinaryContext method registerClassDescriptor.
/**
* Creates and registers {@link BinaryClassDescriptor} for the given {@code class}.
*
* @param cls Class.
* @return Class descriptor.
*/
private BinaryClassDescriptor registerClassDescriptor(Class<?> cls, boolean deserialize) {
BinaryClassDescriptor desc;
String clsName = cls.getName();
if (marshCtx.isSystemType(clsName)) {
BinarySerializer serializer = null;
if (BINARYLIZABLE_SYS_CLSS.contains(clsName))
serializer = new BinaryReflectiveSerializer();
desc = new BinaryClassDescriptor(this, cls, false, clsName.hashCode(), clsName, null, SIMPLE_NAME_LOWER_CASE_MAPPER, serializer, false, true);
BinaryClassDescriptor old = descByCls.putIfAbsent(cls, desc);
if (old != null)
desc = old;
} else
desc = registerUserClassDescriptor(cls, deserialize);
return desc;
}
use of org.apache.ignite.binary.BinarySerializer in project ignite by apache.
the class BinaryContext method registerUserClassDescriptor.
/**
* Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
*
* @param desc Old descriptor that should be re-registered.
* @return Class descriptor.
*/
private BinaryClassDescriptor registerUserClassDescriptor(BinaryClassDescriptor desc) {
boolean registered;
try {
registered = marshCtx.registerClassName(JAVA_ID, desc.typeId(), desc.describedClass().getName());
} catch (IgniteCheckedException e) {
throw new BinaryObjectException("Failed to register class.", e);
}
if (registered) {
BinarySerializer serializer = desc.initialSerializer();
if (serializer == null)
serializer = serializerForClass(desc.describedClass());
desc = new BinaryClassDescriptor(this, desc.describedClass(), true, desc.typeId(), desc.typeName(), desc.affFieldKeyName(), desc.mapper(), serializer, true, true);
descByCls.put(desc.describedClass(), desc);
}
return desc;
}
use of org.apache.ignite.binary.BinarySerializer in project ignite by apache.
the class BinaryContext method registerUserClassDescriptor.
/**
* Creates and registers {@link BinaryClassDescriptor} for the given user {@code class}.
*
* @param cls Class.
* @return Class descriptor.
*/
private BinaryClassDescriptor registerUserClassDescriptor(Class<?> cls, boolean deserialize) {
boolean registered;
final String clsName = cls.getName();
BinaryInternalMapper mapper = userTypeMapper(clsName);
final String typeName = mapper.typeName(clsName);
final int typeId = mapper.typeId(clsName);
try {
registered = marshCtx.registerClassName(JAVA_ID, typeId, cls.getName());
} catch (IgniteCheckedException e) {
throw new BinaryObjectException("Failed to register class.", e);
}
BinarySerializer serializer = serializerForClass(cls);
String affFieldName = affinityFieldName(cls);
BinaryClassDescriptor desc = new BinaryClassDescriptor(this, cls, true, typeId, typeName, affFieldName, mapper, serializer, true, registered);
if (!deserialize)
metaHnd.addMeta(typeId, new BinaryMetadata(typeId, typeName, desc.fieldsMeta(), affFieldName, null, desc.isEnum(), cls.isEnum() ? enumMap(cls) : null).wrap(this));
descByCls.put(cls, desc);
typeId2Mapper.putIfAbsent(typeId, mapper);
return desc;
}
use of org.apache.ignite.binary.BinarySerializer in project ignite by apache.
the class BinaryConfigurationConsistencySelfTest method customConfig.
/**
* @return Custom BinaryConfiguration.
* @param compactFooter Compact footer.
*/
private BinaryConfiguration customConfig(boolean compactFooter) {
BinaryConfiguration c = new BinaryConfiguration();
c.setIdMapper(new BinaryBasicIdMapper(true));
c.setSerializer(new BinarySerializer() {
@Override
public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
// No-op.
}
@Override
public void readBinary(Object obj, BinaryReader reader) throws BinaryObjectException {
// No-op.
}
});
c.setCompactFooter(compactFooter);
BinaryTypeConfiguration btc = new BinaryTypeConfiguration("org.MyClass");
btc.setIdMapper(BinaryContext.defaultIdMapper());
btc.setEnum(false);
btc.setSerializer(new BinarySerializer() {
@Override
public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
// No-op.
}
@Override
public void readBinary(Object obj, BinaryReader reader) throws BinaryObjectException {
// No-op.
}
});
c.setTypeConfigurations(Arrays.asList(btc));
return c;
}
Aggregations