Search in sources :

Example 1 with BinarySerializer

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);
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryNameMapper(org.apache.ignite.binary.BinaryNameMapper) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) IgfsFileMap(org.apache.ignite.internal.processors.igfs.IgfsFileMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Example 2 with BinarySerializer

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;
}
Also used : BinaryReflectiveSerializer(org.apache.ignite.binary.BinaryReflectiveSerializer) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Example 3 with BinarySerializer

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;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Example 4 with BinarySerializer

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;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Example 5 with BinarySerializer

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;
}
Also used : BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryBasicIdMapper(org.apache.ignite.binary.BinaryBasicIdMapper) BinaryWriter(org.apache.ignite.binary.BinaryWriter) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryReader(org.apache.ignite.binary.BinaryReader) BinarySerializer(org.apache.ignite.binary.BinarySerializer) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BinarySerializer (org.apache.ignite.binary.BinarySerializer)6 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)4 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 BinaryReader (org.apache.ignite.binary.BinaryReader)2 BinaryWriter (org.apache.ignite.binary.BinaryWriter)2 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)1 BinaryIdMapper (org.apache.ignite.binary.BinaryIdMapper)1 BinaryNameMapper (org.apache.ignite.binary.BinaryNameMapper)1 BinaryReflectiveSerializer (org.apache.ignite.binary.BinaryReflectiveSerializer)1 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)1