Search in sources :

Example 1 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)) 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 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 3 with BinaryTypeConfiguration

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;
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 4 with BinaryTypeConfiguration

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

Example 5 with BinaryTypeConfiguration

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

Aggregations

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