Search in sources :

Example 1 with BinaryNameMapper

use of org.apache.ignite.binary.BinaryNameMapper 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 BinaryNameMapper

use of org.apache.ignite.binary.BinaryNameMapper 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 BinaryNameMapper

use of org.apache.ignite.binary.BinaryNameMapper in project ignite by apache.

the class GridCacheBinaryObjectsAbstractSelfTest method testCircularReference.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
public void testCircularReference() throws Exception {
    IgniteCache c = keepBinaryCache();
    TestReferenceObject obj1 = new TestReferenceObject();
    obj1.obj = new TestReferenceObject(obj1);
    c.put(1, obj1);
    BinaryObject po = (BinaryObject) c.get(1);
    String str = po.toString();
    log.info("toString: " + str);
    assertNotNull(str);
    BinaryNameMapper nameMapper = BinaryContext.defaultNameMapper();
    if (cfg.getBinaryConfiguration() != null && cfg.getBinaryConfiguration().getNameMapper() != null)
        nameMapper = cfg.getBinaryConfiguration().getNameMapper();
    String typeName = nameMapper.typeName(TestReferenceObject.class.getName());
    assertTrue("Unexpected toString: " + str, S.INCLUDE_SENSITIVE ? str.startsWith(typeName) && str.contains("obj=" + typeName + " [") : str.startsWith("BinaryObject") && str.contains("idHash=") && str.contains("hash="));
    TestReferenceObject obj1_r = po.deserialize();
    assertNotNull(obj1_r);
    TestReferenceObject obj2_r = obj1_r.obj;
    assertNotNull(obj2_r);
    assertSame(obj1_r, obj2_r.obj);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) BinaryNameMapper(org.apache.ignite.binary.BinaryNameMapper)

Example 4 with BinaryNameMapper

use of org.apache.ignite.binary.BinaryNameMapper in project ignite by apache.

the class PlatformCppConfigurationClosure method apply0.

/** {@inheritDoc} */
@SuppressWarnings("deprecation")
@Override
protected void apply0(IgniteConfiguration igniteCfg) {
    // 3. Validate and copy Interop configuration setting environment pointer along the way.
    PlatformConfiguration interopCfg = igniteCfg.getPlatformConfiguration();
    if (interopCfg != null && !(interopCfg instanceof PlatformCppConfiguration))
        throw new IgniteException("Illegal interop configuration (must be of type " + PlatformCppConfiguration.class.getName() + "): " + interopCfg.getClass().getName());
    PlatformCppConfiguration cppCfg = interopCfg != null ? (PlatformCppConfiguration) interopCfg : null;
    if (cppCfg == null)
        cppCfg = new PlatformCppConfiguration();
    PlatformMemoryManagerImpl memMgr = new PlatformMemoryManagerImpl(gate, 1024);
    PlatformCppConfigurationEx cppCfg0 = new PlatformCppConfigurationEx(cppCfg, gate, memMgr);
    igniteCfg.setPlatformConfiguration(cppCfg0);
    // Check marshaller
    Marshaller marsh = igniteCfg.getMarshaller();
    if (marsh == null) {
        igniteCfg.setMarshaller(new BinaryMarshaller());
        cppCfg0.warnings(Collections.singleton("Marshaller is automatically set to " + BinaryMarshaller.class.getName() + " (other nodes must have the same marshaller type)."));
    } else if (!(marsh instanceof BinaryMarshaller))
        throw new IgniteException("Unsupported marshaller (only " + BinaryMarshaller.class.getName() + " can be used when running Apache Ignite C++): " + marsh.getClass().getName());
    BinaryConfiguration bCfg = igniteCfg.getBinaryConfiguration();
    if (bCfg == null) {
        bCfg = new BinaryConfiguration();
        bCfg.setCompactFooter(false);
        bCfg.setNameMapper(new BinaryBasicNameMapper(true));
        bCfg.setIdMapper(new BinaryBasicIdMapper(true));
        igniteCfg.setBinaryConfiguration(bCfg);
        cppCfg0.warnings(Collections.singleton("Binary configuration is automatically initiated, " + "note that binary name mapper is set to " + bCfg.getNameMapper() + " and binary ID mapper is set to " + bCfg.getIdMapper() + " (other nodes must have the same binary name and ID mapper types)."));
    } else {
        BinaryNameMapper nameMapper = bCfg.getNameMapper();
        if (nameMapper == null) {
            bCfg.setNameMapper(new BinaryBasicNameMapper(true));
            cppCfg0.warnings(Collections.singleton("Binary name mapper is automatically set to " + bCfg.getNameMapper() + " (other nodes must have the same binary name mapper type)."));
        }
        BinaryIdMapper idMapper = bCfg.getIdMapper();
        if (idMapper == null) {
            bCfg.setIdMapper(new BinaryBasicIdMapper(true));
            cppCfg0.warnings(Collections.singleton("Binary ID mapper is automatically set to " + bCfg.getIdMapper() + " (other nodes must have the same binary ID mapper type)."));
        }
    }
    if (bCfg.isCompactFooter())
        throw new IgniteException("Unsupported " + BinaryMarshaller.class.getName() + " \"compactFooter\" flag: must be false when running Apache Ignite C++.");
    // Set Ignite home so that marshaller context works.
    String ggHome = igniteCfg.getIgniteHome();
    if (ggHome != null)
        U.setIgniteHome(ggHome);
}
Also used : PlatformConfiguration(org.apache.ignite.configuration.PlatformConfiguration) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Marshaller(org.apache.ignite.marshaller.Marshaller) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) BinaryBasicNameMapper(org.apache.ignite.binary.BinaryBasicNameMapper) BinaryBasicIdMapper(org.apache.ignite.binary.BinaryBasicIdMapper) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) PlatformCppConfiguration(org.apache.ignite.platform.cpp.PlatformCppConfiguration) IgniteException(org.apache.ignite.IgniteException) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryNameMapper(org.apache.ignite.binary.BinaryNameMapper) PlatformMemoryManagerImpl(org.apache.ignite.internal.processors.platform.memory.PlatformMemoryManagerImpl)

Example 5 with BinaryNameMapper

use of org.apache.ignite.binary.BinaryNameMapper in project ignite by apache.

the class BinaryObjectBuilderDefaultMappersSelfTest method expectedHashCode.

/**
     * @return Expected hash code.
     * @param fullName Full name of type.
     */
private int expectedHashCode(String fullName) {
    BinaryIdMapper idMapper = cfg.getBinaryConfiguration().getIdMapper();
    BinaryNameMapper nameMapper = cfg.getBinaryConfiguration().getNameMapper();
    if (idMapper == null)
        idMapper = BinaryContext.defaultIdMapper();
    if (nameMapper == null)
        nameMapper = BinaryContext.defaultNameMapper();
    return idMapper.typeId(nameMapper.typeName(fullName));
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryNameMapper(org.apache.ignite.binary.BinaryNameMapper)

Aggregations

BinaryNameMapper (org.apache.ignite.binary.BinaryNameMapper)6 BinaryIdMapper (org.apache.ignite.binary.BinaryIdMapper)4 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)3 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)1 BinaryBasicNameMapper (org.apache.ignite.binary.BinaryBasicNameMapper)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 BinarySerializer (org.apache.ignite.binary.BinarySerializer)1 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)1 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)1 PlatformConfiguration (org.apache.ignite.configuration.PlatformConfiguration)1 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)1 IgfsFileMap (org.apache.ignite.internal.processors.igfs.IgfsFileMap)1