Search in sources :

Example 61 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testTypeNamesFullNameMappers.

/**
 * @throws Exception If failed.
 */
public void testTypeNamesFullNameMappers() 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(false), new BinaryBasicIdMapper(false), 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.class.getName().hashCode(), ctx.typeId(Key.class.getName()));
    assertEquals("org.gridgain.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 62 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testCycleLink.

/**
 * @throws Exception If failed.
 */
public void testCycleLink() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(CycleLinkObject.class.getName())));
    CycleLinkObject obj = new CycleLinkObject();
    obj.self = obj;
    BinaryObject po = marshal(obj, marsh);
    CycleLinkObject obj0 = po.deserialize();
    assert obj0.self == obj0;
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 63 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testDecimalFields.

/**
 */
public void testDecimalFields() throws Exception {
    Collection<BinaryTypeConfiguration> clsNames = new ArrayList<>();
    clsNames.add(new BinaryTypeConfiguration(DecimalReflective.class.getName()));
    clsNames.add(new BinaryTypeConfiguration(DecimalMarshalAware.class.getName()));
    BinaryMarshaller marsh = binaryMarshaller(clsNames);
    // 1. Test reflective stuff.
    DecimalReflective obj1 = new DecimalReflective();
    BigDecimal[] valArr = new BigDecimal[] { BigDecimal.ONE, BigDecimal.TEN, new BigDecimal("-100.5"), BigDecimal.valueOf(Long.MAX_VALUE, 0), BigDecimal.valueOf(Long.MIN_VALUE, 0), BigDecimal.valueOf(Long.MAX_VALUE, 8), BigDecimal.valueOf(Long.MIN_VALUE, 8) };
    obj1.val = BigDecimal.ZERO;
    obj1.valArr = valArr;
    BinaryObjectImpl portObj = marshal(obj1, marsh);
    assertArrayEquals(obj1.valArr, portObj.<BigDecimal[]>field("valArr"));
    assertArrayEquals(obj1.valArr, portObj.<DecimalReflective>deserialize().valArr);
    assertArrayEquals(obj1.valArr, (BigDecimal[]) portObj.type().field("valArr").value(portObj));
    obj1.valArr = null;
    for (BigDecimal v : valArr) {
        obj1.val = v;
        portObj = marshal(obj1, marsh);
        assertEquals(obj1.val, portObj.field("val"));
        assertEquals(obj1.val, portObj.<DecimalReflective>deserialize().val);
        assertEquals(obj1.val, portObj.type().field("val").value(portObj));
    }
    // 2. Test marshal aware stuff.
    DecimalMarshalAware obj2 = new DecimalMarshalAware();
    obj2.val = BigDecimal.ZERO;
    obj2.valArr = new BigDecimal[] { BigDecimal.ONE, BigDecimal.TEN.negate() };
    obj2.rawVal = BigDecimal.TEN;
    obj2.rawValArr = new BigDecimal[] { BigDecimal.ZERO, BigDecimal.ONE };
    portObj = marshal(obj2, marsh);
    assertEquals(obj2.val, portObj.field("val"));
    assertArrayEquals(obj2.valArr, portObj.<BigDecimal[]>field("valArr"));
    assertEquals(obj2.val, portObj.<DecimalMarshalAware>deserialize().val);
    assertArrayEquals(obj2.valArr, portObj.<DecimalMarshalAware>deserialize().valArr);
    assertEquals(obj2.rawVal, portObj.<DecimalMarshalAware>deserialize().rawVal);
    assertArrayEquals(obj2.rawValArr, portObj.<DecimalMarshalAware>deserialize().rawValArr);
    assertEquals(obj2.val, portObj.type().field("val").value(portObj));
    assertArrayEquals(obj2.valArr, (BigDecimal[]) portObj.type().field("valArr").value(portObj));
    for (BigDecimal v : valArr) {
        obj2.val = v;
        portObj = marshal(obj2, marsh);
        assertEquals(obj2.val, portObj.field("val"));
        assertEquals(obj2.val, portObj.<DecimalMarshalAware>deserialize().val);
        assertEquals(obj2.val, portObj.type().field("val").value(portObj));
    }
}
Also used : ArrayList(java.util.ArrayList) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BigDecimal(java.math.BigDecimal)

Example 64 with BinaryTypeConfiguration

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

the class BinaryMarshallerSelfTest method testBinaryCopyIntArray.

/**
 * @throws Exception If failed.
 */
public void testBinaryCopyIntArray() 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("iArr", new int[] { 1, 2, 3 }));
    assertArrayEquals(new int[] { 1, 2, 3 }, copy.<int[]>field("iArr"));
    SimpleObject obj0 = copy.deserialize();
    assertArrayEquals(new int[] { 1, 2, 3 }, obj0.iArr);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 65 with BinaryTypeConfiguration

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

the class GridCacheBinaryObjectUserClassloaderSelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    TcpDiscoverySpi disco = new TcpDiscoverySpi();
    disco.setIpFinder(ipFinder);
    cfg.setDiscoverySpi(disco);
    cfg.setCacheConfiguration(cacheConfiguration(igniteInstanceName));
    cfg.setMarshaller(new BinaryMarshaller());
    cfg.setClassLoader(useWrappingLoader ? new WrappingClassLoader(getExternalClassLoader()) : getExternalClassLoader());
    if (customBinaryConf) {
        BinarySerializer bs = new BinarySerializer() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
            // No-op.
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void readBinary(Object obj, BinaryReader reader) throws BinaryObjectException {
                deserialized = true;
            }
        };
        BinaryTypeConfiguration btcfg1 = new BinaryTypeConfiguration();
        btcfg1.setTypeName("org.apache.ignite.tests.p2p.CacheDeploymentTestValue");
        btcfg1.setSerializer(bs);
        BinaryTypeConfiguration btcfg2 = new BinaryTypeConfiguration();
        btcfg2.setTypeName("org.apache.ignite.internal.processors.cache.binary." + "GridCacheBinaryObjectUserClassloaderSelfTest$TestValue1");
        btcfg2.setSerializer(bs);
        BinaryConfiguration bcfg = new BinaryConfiguration();
        Set<BinaryTypeConfiguration> set = new HashSet<>();
        set.add(btcfg1);
        set.add(btcfg2);
        bcfg.setTypeConfigurations(set);
        cfg.setBinaryConfiguration(bcfg);
    }
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) BinaryWriter(org.apache.ignite.binary.BinaryWriter) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryReader(org.apache.ignite.binary.BinaryReader) BinarySerializer(org.apache.ignite.binary.BinarySerializer) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) HashSet(java.util.HashSet)

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