Search in sources :

Example 16 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testTypeNamesCustomIdMapper.

/**
 * @throws Exception If failed.
 */
public void testTypeNamesCustomIdMapper() 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;
        }
    });
    BinaryTypeConfiguration customType5 = new BinaryTypeConfiguration(DateClass1.class.getName());
    customType5.setIdMapper(new BinaryBasicIdMapper(false));
    BinaryTypeConfiguration customType6 = new BinaryTypeConfiguration(MyTestClass.class.getName());
    customType6.setIdMapper(new BinaryBasicIdMapper(true));
    customType6.setNameMapper(new BinaryBasicNameMapper(true));
    BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(false), new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            if ("org.blabla.NotConfiguredSpecialClass".equals(clsName))
                return 0;
            else if (Key.class.getName().equals(clsName))
                return 991;
            else if ("org.gridgain.NonExistentClass3".equals(clsName))
                return 992;
            else if ("NonExistentClass4".equals(clsName))
                return 993;
            return 999;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    }, Arrays.asList(new BinaryTypeConfiguration(Key.class.getName()), new BinaryTypeConfiguration("org.gridgain.NonExistentClass3"), new BinaryTypeConfiguration("NonExistentClass4"), customType1, customType2, customType3, customType4, customType5, customType6));
    BinaryContext ctx = binaryContext(marsh);
    assertEquals(999, ctx.typeId("NotConfiguredClass"));
    assertEquals(999, ctx.typeId("org.blabla.NotConfiguredClass"));
    // BinaryIdMapper.typeId() contract.
    assertEquals("notconfiguredspecialclass".hashCode(), ctx.typeId("org.blabla.NotConfiguredSpecialClass"));
    assertEquals(991, ctx.typeId(Key.class.getName()));
    assertEquals(992, ctx.typeId("org.gridgain.NonExistentClass3"));
    assertEquals(993, ctx.typeId("NonExistentClass4"));
    // Custom types.
    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"));
    assertEquals(DateClass1.class.getName().hashCode(), ctx.typeId(DateClass1.class.getName()));
    assertEquals("mytestclass".hashCode(), ctx.typeId(MyTestClass.class.getName()));
}
Also used : BinaryBasicNameMapper(org.apache.ignite.binary.BinaryBasicNameMapper) BinaryBasicIdMapper(org.apache.ignite.binary.BinaryBasicIdMapper) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 17 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testFieldIdMapping.

/**
 * @throws Exception If failed.
 */
public void testFieldIdMapping() 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) {
            switch(fieldName) {
                case "val1":
                    return 301;
                case "val2":
                    return 302;
                default:
                    return 0;
            }
        }
    });
    BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("NonExistentClass1");
    customType2.setIdMapper(new BinaryIdMapper() {

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

        @Override
        public int fieldId(int typeId, String fieldName) {
            switch(fieldName) {
                case "val1":
                    return 401;
                case "val2":
                    return 402;
                default:
                    return 0;
            }
        }
    });
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(Key.class.getName()), new BinaryTypeConfiguration("NonExistentClass2"), customType1, customType2));
    BinaryContext ctx = binaryContext(marsh);
    assertEquals("val".hashCode(), ctx.fieldId("key".hashCode(), "val"));
    assertEquals("val".hashCode(), ctx.fieldId("nonexistentclass2".hashCode(), "val"));
    assertEquals("val".hashCode(), ctx.fieldId("notconfiguredclass".hashCode(), "val"));
    assertEquals(301, ctx.fieldId(300, "val1"));
    assertEquals(302, ctx.fieldId(300, "val2"));
    assertEquals("val3".hashCode(), ctx.fieldId(300, "val3"));
    assertEquals(401, ctx.fieldId(400, "val1"));
    assertEquals(402, ctx.fieldId(400, "val2"));
    assertEquals("val3".hashCode(), ctx.fieldId(400, "val3"));
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 18 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testSimpleNameLowerCaseMappers.

/**
 * @throws Exception If failed.
 */
public void testSimpleNameLowerCaseMappers() throws Exception {
    BinaryTypeConfiguration innerClassType = new BinaryTypeConfiguration(InnerMappedObject.class.getName());
    BinaryTypeConfiguration publicClassType = new BinaryTypeConfiguration(TestMappedObject.class.getName());
    BinaryTypeConfiguration typeWithCustomMapper = new BinaryTypeConfiguration(CustomMappedObject2.class.getName());
    typeWithCustomMapper.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(new BinaryBasicNameMapper(true), new BinaryBasicIdMapper(true), Arrays.asList(innerClassType, publicClassType, typeWithCustomMapper));
    InnerMappedObject innerObj = new InnerMappedObject(10, "str1");
    BinaryObjectExImpl innerBo = marshal(innerObj, marsh);
    assertEquals("InnerMappedObject".toLowerCase().hashCode(), innerBo.type().typeId());
    assertEquals(10, innerBo.<CustomMappedObject1>deserialize().val1);
    assertEquals("str1", innerBo.<CustomMappedObject1>deserialize().val2);
    TestMappedObject publicObj = new TestMappedObject();
    BinaryObjectExImpl publicBo = marshal(publicObj, marsh);
    assertEquals("TestMappedObject".toLowerCase().hashCode(), publicBo.type().typeId());
    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 : BinaryBasicNameMapper(org.apache.ignite.binary.BinaryBasicNameMapper) BinaryBasicIdMapper(org.apache.ignite.binary.BinaryBasicIdMapper) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 19 with BinaryIdMapper

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

the class BinaryMarshallerSelfTest method testTypeNamesSimpleNameMappers.

/**
 * @throws Exception If failed.
 */
public void testTypeNamesSimpleNameMappers() 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;
        }
    });
    BinaryTypeConfiguration customType5 = new BinaryTypeConfiguration(DateClass1.class.getName());
    customType5.setNameMapper(new BinaryBasicNameMapper(false));
    customType5.setIdMapper(new BinaryBasicIdMapper(false));
    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, customType5));
    BinaryContext ctx = binaryContext(marsh);
    assertEquals("notconfiguredclass".hashCode(), ctx.typeId("NotConfiguredClass"));
    assertEquals("notconfiguredclass".hashCode(), ctx.typeId("org.blabla.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"));
    assertEquals(DateClass1.class.getName().hashCode(), ctx.typeId(DateClass1.class.getName()));
    // 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 20 with BinaryIdMapper

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

the class GridBinaryWildcardsSelfTest method checkOverrideIdMapper.

/**
 * @param nameMapper Name mapper.
 * @param mapper Mapper.
 * @throws IgniteCheckedException If failed.
 */
private void checkOverrideIdMapper(BinaryNameMapper nameMapper, BinaryIdMapper mapper) throws IgniteCheckedException {
    BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration();
    typeCfg.setTypeName(CLASS2_FULL_NAME);
    typeCfg.setIdMapper(new BinaryIdMapper() {

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

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryMarshaller marsh = binaryMarshaller(nameMapper, mapper, Arrays.asList(new BinaryTypeConfiguration("org.apache.ignite.internal.binary.test.*"), typeCfg));
    BinaryContext ctx = binaryContext(marsh);
    Map<Integer, Class> typeIds = U.field(ctx, "userTypes");
    assertEquals(3, typeIds.size());
    assertTrue(typeIds.containsKey(typeId(CLASS1_FULL_NAME, nameMapper, mapper)));
    assertTrue(typeIds.containsKey(typeId(INNER_CLASS_FULL_NAME, nameMapper, mapper)));
    assertTrue(typeIds.containsKey(100));
    Map<String, org.apache.ignite.internal.binary.BinaryInternalMapper> typeMappers = U.field(ctx, "cls2Mappers");
    assertEquals(100, typeMappers.get(CLASS2_FULL_NAME).idMapper().typeId(CLASS2_FULL_NAME));
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

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 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)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