Search in sources :

Example 41 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testBinaryCopyShortArray.

/**
 * @throws Exception If failed.
 */
public void testBinaryCopyShortArray() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
    SimpleObject obj = simpleObject();
    BinaryObject po = marshal(obj, marsh);
    BinaryObject copy = copy(po, F.<String, Object>asMap("sArr", new short[] { 1, 2, 3 }));
    assertArrayEquals(new short[] { 1, 2, 3 }, copy.<short[]>field("sArr"));
    SimpleObject obj0 = copy.deserialize();
    assertArrayEquals(new short[] { 1, 2, 3 }, obj0.sArr);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 42 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testDateAndTimestampInSingleObject.

/**
 * @throws Exception If failed.
 */
public void testDateAndTimestampInSingleObject() throws Exception {
    BinaryTypeConfiguration cfg1 = new BinaryTypeConfiguration(DateClass1.class.getName());
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(cfg1));
    Date date = new Date();
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    Time time = new Time(System.currentTimeMillis());
    Time[] timeArr = new Time[] { time, new Time(date.getTime()), new Time(System.currentTimeMillis()) };
    DateClass1 obj1 = new DateClass1();
    obj1.date = date;
    obj1.ts = ts;
    obj1.time = time;
    obj1.timeArr = timeArr;
    BinaryObject po1 = marshal(obj1, marsh);
    assertEquals(date, po1.field("date"));
    assertEquals(Date.class, po1.field("date").getClass());
    assertEquals(ts, po1.field("ts"));
    assertEquals(Timestamp.class, po1.field("ts").getClass());
    assertEquals(time, po1.field("time"));
    assertEquals(Time.class, po1.field("time").getClass());
    assertArrayEquals(timeArr, (Object[]) po1.field("timeArr"));
    assertEquals(Time[].class, po1.field("timeArr").getClass());
    obj1 = po1.deserialize();
    assertEquals(date, obj1.date);
    assertEquals(ts, obj1.ts);
    assertEquals(time, obj1.time);
    assertArrayEquals(timeArr, obj1.timeArr);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Time(java.sql.Time) BinaryObject(org.apache.ignite.binary.BinaryObject) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 43 with BinaryTypeConfiguration

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

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

the class BinaryMarshallerSelfTest method testBinaryCopyByteArray.

/**
 * @throws Exception If failed.
 */
public void testBinaryCopyByteArray() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
    SimpleObject obj = simpleObject();
    BinaryObject po = marshal(obj, marsh);
    BinaryObject copy = copy(po, F.<String, Object>asMap("bArr", new byte[] { 1, 2, 3 }));
    assertArrayEquals(new byte[] { 1, 2, 3 }, copy.<byte[]>field("bArr"));
    SimpleObject obj0 = copy.deserialize();
    assertArrayEquals(new byte[] { 1, 2, 3 }, obj0.bArr);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 45 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testBinaryCopyNonPrimitives.

/**
 * @throws Exception If failed.
 */
public void testBinaryCopyNonPrimitives() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
    SimpleObject obj = simpleObject();
    BinaryObject po = marshal(obj, marsh);
    Map<String, Object> map = new HashMap<>(3, 1.0f);
    SimpleObject newObj = new SimpleObject();
    newObj.i = 12345;
    newObj.fArr = new float[] { 5, 8, 0 };
    newObj.str = "newStr";
    map.put("str", "str555");
    map.put("inner", newObj);
    map.put("bArr", new byte[] { 6, 7, 9 });
    BinaryObject copy = copy(po, map);
    assertEquals("str555", copy.<String>field("str"));
    assertEquals(newObj, copy.<BinaryObject>field("inner").deserialize());
    assertArrayEquals(new byte[] { 6, 7, 9 }, copy.<byte[]>field("bArr"));
    SimpleObject obj0 = copy.deserialize();
    assertEquals("str555", obj0.str);
    assertEquals(newObj, obj0.inner);
    assertArrayEquals(new byte[] { 6, 7, 9 }, obj0.bArr);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryObject(org.apache.ignite.binary.BinaryObject)

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