Search in sources :

Example 46 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testWriteReplaceInheritable.

/**
 * @throws Exception If failed.
 */
public void testWriteReplaceInheritable() throws Exception {
    ImmutableList<String> obj = ImmutableList.of("This is a test");
    BinaryMarshaller marsh = binaryMarshaller(Collections.singleton(new BinaryTypeConfiguration(obj.getClass().getName())));
    BinaryObject po = marshal(obj, marsh);
    Object des = po.deserialize();
    assertEquals(obj, des);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 47 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testCustomTypeRegistration.

/**
 * @throws Exception If failed.
 */
public void testCustomTypeRegistration() throws Exception {
    BinaryTypeConfiguration customType = new BinaryTypeConfiguration(Value.class.getName());
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(customType));
    BinaryContext ctx = binaryContext(marsh);
    int typeId = ctx.typeId(Value.class.getName());
    BinaryClassDescriptor descriptor = ctx.descriptorForTypeId(true, typeId, null, false);
    assertEquals(Value.class, descriptor.describedClass());
    assertEquals(true, descriptor.registered());
    assertEquals(true, descriptor.userType());
    // Custom explicit types must be registered in 'predefinedTypes' in order not to break the interoperability.
    Field field = ctx.getClass().getDeclaredField("predefinedTypes");
    field.setAccessible(true);
    Map<Integer, BinaryClassDescriptor> map = (Map<Integer, BinaryClassDescriptor>) field.get(ctx);
    assertTrue(map.size() > 0);
    assertNotNull(map.get(typeId));
    // Custom explicit types must NOT be registered in 'predefinedTypeNames'.
    field = ctx.getClass().getDeclaredField("predefinedTypeNames");
    field.setAccessible(true);
    Map<String, Integer> map2 = (Map<String, Integer>) field.get(ctx);
    assertTrue(map2.size() > 0);
    assertNull(map2.get(ctx.userTypeName(Value.class.getName())));
}
Also used : BigInteger(java.math.BigInteger) Field(java.lang.reflect.Field) BinaryField(org.apache.ignite.binary.BinaryField) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 48 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration 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 49 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration 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 50 with BinaryTypeConfiguration

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

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