Search in sources :

Example 11 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testTypeNamesSimpleNameMapper.

/**
     * @throws Exception If failed.
     */
public void testTypeNamesSimpleNameMapper() throws Exception {
    BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName());
    customType1.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 300;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.NonExistentClass1");
    customType2.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 400;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryTypeConfiguration customType3 = new BinaryTypeConfiguration("NonExistentClass2");
    customType3.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 500;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryTypeConfiguration customType4 = new BinaryTypeConfiguration("NonExistentClass0");
    customType4.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 0;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(true), new BinaryBasicIdMapper(true), Arrays.asList(new BinaryTypeConfiguration(Key.class.getName()), new BinaryTypeConfiguration("org.gridgain.NonExistentClass3"), new BinaryTypeConfiguration("NonExistentClass4"), customType1, customType2, customType3, customType4));
    BinaryContext ctx = binaryContext(marsh);
    // Full name hashCode.
    assertEquals("notconfiguredclass".hashCode(), ctx.typeId("NotConfiguredClass"));
    assertEquals("key".hashCode(), ctx.typeId(Key.class.getName()));
    assertEquals("nonexistentclass3".hashCode(), ctx.typeId("org.gridgain.NonExistentClass3"));
    assertEquals("nonexistentclass4".hashCode(), ctx.typeId("NonExistentClass4"));
    assertEquals(300, ctx.typeId(Value.class.getName()));
    assertEquals(400, ctx.typeId("org.gridgain.NonExistentClass1"));
    assertEquals(500, ctx.typeId("NonExistentClass2"));
    // BinaryIdMapper.typeId() contract.
    assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
}
Also used : BinaryBasicNameMapper(org.apache.ignite.binary.BinaryBasicNameMapper) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryBasicIdMapper(org.apache.ignite.binary.BinaryBasicIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 12 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testCustomIdMapper.

/**
     * @throws Exception If failed.
     */
public void testCustomIdMapper() throws Exception {
    BinaryTypeConfiguration type = new BinaryTypeConfiguration(CustomMappedObject1.class.getName());
    type.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 11111;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            assert typeId == 11111;
            if ("val1".equals(fieldName))
                return 22222;
            else if ("val2".equals(fieldName))
                return 33333;
            assert false : "Unknown field: " + fieldName;
            return 0;
        }
    });
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(type));
    CustomMappedObject1 obj1 = new CustomMappedObject1(10, "str");
    BinaryObjectExImpl po1 = marshal(obj1, marsh);
    assertEquals(11111, po1.type().typeId());
    assertEquals((Integer) 10, po1.field(22222));
    assertEquals("str", po1.field(33333));
    assertEquals(10, po1.<CustomMappedObject1>deserialize().val1);
    assertEquals("str", po1.<CustomMappedObject1>deserialize().val2);
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 13 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testCustomIdMapperWithGlobal.

/**
     * @throws Exception If failed.
     */
public void testCustomIdMapperWithGlobal() throws Exception {
    BinaryTypeConfiguration type1 = new BinaryTypeConfiguration(CustomMappedObject1.class.getName());
    BinaryTypeConfiguration type2 = new BinaryTypeConfiguration(CustomMappedObject2.class.getName());
    type2.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 44444;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            assert typeId == 44444;
            if ("val1".equals(fieldName))
                return 55555;
            else if ("val2".equals(fieldName))
                return 66666;
            assert false : "Unknown field: " + fieldName;
            return 0;
        }
    });
    BinaryMarshaller marsh = binaryMarshaller(null, new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 11111;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            assert typeId == 11111;
            if ("val1".equals(fieldName))
                return 22222;
            else if ("val2".equals(fieldName))
                return 33333;
            assert false : "Unknown field: " + fieldName;
            return 0;
        }
    }, Arrays.asList(type1, type2));
    CustomMappedObject1 obj1 = new CustomMappedObject1(10, "str1");
    BinaryObjectExImpl po1 = marshal(obj1, marsh);
    assertEquals(11111, po1.type().typeId());
    assertEquals((Integer) 10, po1.field(22222));
    assertEquals("str1", po1.field(33333));
    assertEquals(10, po1.<CustomMappedObject1>deserialize().val1);
    assertEquals("str1", po1.<CustomMappedObject1>deserialize().val2);
    CustomMappedObject2 obj2 = new CustomMappedObject2(20, "str2");
    BinaryObjectExImpl po2 = marshal(obj2, marsh);
    assertEquals(44444, po2.type().typeId());
    assertEquals((Integer) 20, po2.field(55555));
    assertEquals("str2", po2.field(66666));
    assertEquals(20, po2.<CustomMappedObject2>deserialize().val1);
    assertEquals("str2", po2.<CustomMappedObject2>deserialize().val2);
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 14 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testDuplicateTypeId.

/**
     * @throws Exception If failed.
     */
public void testDuplicateTypeId() throws Exception {
    BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration("org.gridgain.Class1");
    customType1.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 100;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.Class2");
    customType2.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 100;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    try {
        binaryMarshaller(Arrays.asList(customType1, customType2));
    } catch (IgniteCheckedException e) {
        assertEquals("Duplicate type ID [clsName=org.gridgain.Class2, id=100]", e.getCause().getCause().getMessage());
        return;
    }
    assert false;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 15 with BinaryIdMapper

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

Aggregations

BinaryIdMapper (org.apache.ignite.binary.BinaryIdMapper)23 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)21 BinaryBasicNameMapper (org.apache.ignite.binary.BinaryBasicNameMapper)12 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)7 BinaryNameMapper (org.apache.ignite.binary.BinaryNameMapper)4 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)2 Method (java.lang.reflect.Method)1 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 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 BinarySerializer (org.apache.ignite.binary.BinarySerializer)1 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1 PlatformConfiguration (org.apache.ignite.configuration.PlatformConfiguration)1 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)1