use of org.apache.ignite.binary.BinaryIdMapper in project ignite by apache.
the class BinaryObjectBuilderDefaultMappersSelfTest method getConfiguration.
/**
* {@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;
}
use of org.apache.ignite.binary.BinaryIdMapper in project ignite by apache.
the class BinaryMarshallerSelfTest method testDuplicateTypeId.
/**
* @throws Exception If failed.
*/
public void testDuplicateTypeId() throws Exception {
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration("org.gridgain.Class1");
customType1.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 100;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
});
BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.Class2");
customType2.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 100;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
});
try {
binaryMarshaller(Arrays.asList(customType1, customType2));
} catch (IgniteCheckedException e) {
assertEquals("Duplicate type ID [clsName=org.gridgain.Class2, id=100]", e.getCause().getCause().getMessage());
return;
}
assert false;
}
use of org.apache.ignite.binary.BinaryIdMapper in project ignite by apache.
the class BinaryMarshallerSelfTest method testTypeNamesSimpleNameMapper.
/**
* @throws Exception If failed.
*/
public void testTypeNamesSimpleNameMapper() 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(true), new BinaryBasicIdMapper(true), 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".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"));
// BinaryIdMapper.typeId() contract.
assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
}
use of org.apache.ignite.binary.BinaryIdMapper in project ignite by apache.
the class BinaryMarshallerSelfTest method testCustomIdMapperWithGlobal.
/**
* @throws Exception If failed.
*/
public void testCustomIdMapperWithGlobal() throws Exception {
BinaryTypeConfiguration type1 = new BinaryTypeConfiguration(CustomMappedObject1.class.getName());
BinaryTypeConfiguration type2 = new BinaryTypeConfiguration(CustomMappedObject2.class.getName());
type2.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(null, 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;
}
}, Arrays.asList(type1, type2));
CustomMappedObject1 obj1 = new CustomMappedObject1(10, "str1");
BinaryObjectExImpl po1 = marshal(obj1, marsh);
assertEquals(11111, po1.type().typeId());
assertEquals((Integer) 10, po1.field(22222));
assertEquals("str1", po1.field(33333));
assertEquals(10, po1.<CustomMappedObject1>deserialize().val1);
assertEquals("str1", po1.<CustomMappedObject1>deserialize().val2);
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);
}
use of org.apache.ignite.binary.BinaryIdMapper in project ignite by apache.
the class BinaryMarshallerSelfTest method testCustomIdMapper.
/**
* @throws Exception If failed.
*/
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);
}
Aggregations