BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration("org.gridgain.Class1"); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 100; customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 100;
/** * @throws Exception If failed. */ @Test public void testDefaultMapping() throws Exception { BinaryTypeConfiguration customMappingType = new BinaryTypeConfiguration(TestBinary.class.getName()); customMappingType.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { String typeName = BinaryContext.SIMPLE_NAME_LOWER_CASE_MAPPER.typeName(clsName); return typeName.toLowerCase().hashCode(); } @Override public int fieldId(int typeId, String fieldName) { return fieldName.toLowerCase().hashCode(); } }); BinaryMarshaller marsh1 = binaryMarshaller(Arrays.asList( new BinaryTypeConfiguration(SimpleObject.class.getName()), customMappingType )); TestBinary obj = binaryObject(); BinaryObjectImpl po = marshal(obj, marsh1); BinaryMarshaller marsh2 = binaryMarshaller(Arrays.asList( new BinaryTypeConfiguration(SimpleObject.class.getName()), new BinaryTypeConfiguration(TestBinary.class.getName()) )); po = marshal(obj, marsh2); assertEquals(obj, po.deserialize()); }
/** * * @param nameMapper Name mapper. * @param idMapper Mapper. * @throws IgniteCheckedException If failed. */ private void checkOverrideJar(BinaryNameMapper nameMapper, BinaryIdMapper idMapper) throws IgniteCheckedException { BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration( "org.apache.ignite.internal.binary.test.GridBinaryTestClass2"); 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, idMapper, Arrays.asList( new BinaryTypeConfiguration("org.apache.ignite.internal.binary.test.*"), typeCfg)); BinaryContext ctx = binaryContext(marsh); ConcurrentMap<Integer, BinaryInternalMapper> types = U.field(ctx, "typeId2Mapper"); assertEquals(3, types.size()); assertTrue(types.containsKey(typeId(CLASS1_FULL_NAME, nameMapper, idMapper))); Map<String, org.apache.ignite.internal.binary.BinaryInternalMapper> typeMappers = U.field(ctx, "cls2Mappers"); assertEquals(3, typeMappers.size()); assertEquals(nameMapper, typeMappers.get(CLASS2_FULL_NAME).nameMapper()); assertEquals(100, typeMappers.get(CLASS2_FULL_NAME).idMapper().typeId(CLASS2_FULL_NAME)); }
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400; customType3.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 500; customType4.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 0;
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400; customType3.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 500; customType4.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 0; customType5.setIdMapper(new BinaryBasicIdMapper(false));
/** * * @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)); }
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400; customType3.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 500; customType4.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 0;
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400;
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName()); customType1.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 300; customType2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 400; customType3.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 500; customType4.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 0; customType5.setIdMapper(new BinaryBasicIdMapper(false)); customType6.setIdMapper(new BinaryBasicIdMapper(true)); customType6.setNameMapper(new BinaryBasicNameMapper(true));
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); BinaryTypeConfiguration customTypeCfg = new BinaryTypeConfiguration(); customTypeCfg.setTypeName(CustomIdMapper.class.getName()); customTypeCfg.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return ~BinaryContext.defaultIdMapper().typeId(clsName); } @Override public int fieldId(int typeId, String fieldName) { return typeId + ~BinaryContext.defaultIdMapper().fieldId(typeId, fieldName); } }); BinaryConfiguration bCfg = new BinaryConfiguration(); bCfg.setCompactFooter(compactFooter()); bCfg.setTypeConfigurations(Arrays.asList( new BinaryTypeConfiguration(Key.class.getName()), new BinaryTypeConfiguration(Value.class.getName()), new BinaryTypeConfiguration("org.gridgain.grid.internal.util.binary.mutabletest.*"), customTypeCfg)); bCfg.setIdMapper(new BinaryBasicIdMapper(false)); bCfg.setNameMapper(new BinaryBasicNameMapper(false)); cfg.setBinaryConfiguration(bCfg); cfg.setMarshaller(new BinaryMarshaller()); this.cfg = cfg; return cfg; }
btc.setIdMapper(BinaryContext.defaultIdMapper()); btc.setEnum(false); btc.setSerializer(new BinarySerializer() {
/** * @throws Exception If failed. */ @Test 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); }
BinaryTypeConfiguration typeWithCustomMapper = new BinaryTypeConfiguration(CustomMappedObject2.class.getName()); typeWithCustomMapper.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 44444;
/** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(gridName); cfg.setConnectorConfiguration(new ConnectorConfiguration()); cfg.setMarshaller(new BinaryMarshaller()); BinaryConfiguration binaryCfg = new BinaryConfiguration(); BinaryTypeConfiguration btc = new BinaryTypeConfiguration("org.MyClass"); btc.setIdMapper(BinaryContext.defaultIdMapper()); btc.setEnum(false); // Set custom serializer that is unknown for Optimized marshaller. btc.setSerializer(new MyBinarySerializer()); binaryCfg.setTypeConfigurations(Collections.singletonList(btc)); cfg.setBinaryConfiguration(binaryCfg); // Set custom consistent ID that unknown for Optimized marshaller. cfg.setConsistentId(new MyConsistentId("test")); cfg.setCacheConfiguration(new CacheConfiguration("TEST")); return cfg; }
new BinaryTypeConfiguration(CustomMappedObject2.class.getName()); type2.setIdMapper(new BinaryIdMapper() { @Override public int typeId(String clsName) { return 44444;