Search in sources :

Example 36 with BinaryTypeConfiguration

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

Example 37 with BinaryTypeConfiguration

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());
    }
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) BinarySerializer(org.apache.ignite.binary.BinarySerializer)

Example 38 with BinaryTypeConfiguration

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);
            }
        }
    }
}
Also used : BinaryTypeImpl(org.apache.ignite.internal.binary.BinaryTypeImpl) BinaryType(org.apache.ignite.binary.BinaryType) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BinaryMetadataHandler(org.apache.ignite.internal.binary.BinaryMetadataHandler) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 39 with BinaryTypeConfiguration

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());
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Method(java.lang.reflect.Method)

Example 40 with BinaryTypeConfiguration

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

Aggregations

BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)71 BinaryObject (org.apache.ignite.binary.BinaryObject)28 BinaryIdMapper (org.apache.ignite.binary.BinaryIdMapper)21 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)20 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)17 BinaryBasicNameMapper (org.apache.ignite.binary.BinaryBasicNameMapper)11 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)9 HashMap (java.util.HashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)8 LinkedHashMap (java.util.LinkedHashMap)7 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)7 BigInteger (java.math.BigInteger)6 ArrayList (java.util.ArrayList)6 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)6 UUID (java.util.UUID)4 Date (java.util.Date)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3