use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class GridBinaryAffinityKeySelfTest method checkAffinity.
/**
* @param ignite Ignite.
* @throws Exception If failed.
*/
private void checkAffinity(Ignite ignite) throws Exception {
Affinity<Object> aff = ignite.affinity(DEFAULT_CACHE_NAME);
GridAffinityProcessor affProc = ((IgniteKernal) ignite).context().affinity();
IgniteCacheObjectProcessor cacheObjProc = ((IgniteKernal) ignite).context().cacheObjects();
CacheObjectContext cacheObjCtx = cacheObjProc.contextForCache(ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class));
for (int i = 0; i < 1000; i++) {
assertEquals(i, aff.affinityKey(i));
assertEquals(i, aff.affinityKey(new TestObject(i)));
assertEquals(i, aff.affinityKey(ignite.binary().toBinary(new TestObject(i))));
assertEquals(i, aff.affinityKey(new AffinityKey(0, i)));
BinaryObjectBuilder bldr = ignite.binary().builder("TestObject2");
bldr.setField("affKey", i);
assertEquals(i, aff.affinityKey(bldr.build()));
CacheObject cacheObj = cacheObjProc.toCacheObject(cacheObjCtx, new TestObject(i), true);
assertEquals(i, aff.affinityKey(cacheObj));
assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(new TestObject(i)));
assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(cacheObj));
assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, i));
assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, new TestObject(i)));
assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, cacheObj));
assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new TestObject(i)));
assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, cacheObj));
assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new AffinityKey(0, i)), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i));
}
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class GridLocalCacheStoreManagerDeserializationTest method testBinaryUpdate.
/**
* Checks no additional unmarshalling happens in calling
* {@link GridCacheMapEntry#innerUpdateLocal(GridCacheVersion, GridCacheOperation, Object, Object[],
* boolean, boolean, boolean, boolean, ExpiryPolicy, boolean, boolean, CacheEntryPredicate[],
* boolean, UUID, String)} for binary objects.
*
* @throws Exception
*/
public void testBinaryUpdate() throws Exception {
// Goal is to check correct saving to store (no exception must be thrown)
final Ignite grid = startGrid("binaryGrid");
final IgniteCache<BinaryObject, BinaryObject> cache = grid.createCache(CACHE_NAME).withKeepBinary();
final BinaryObjectBuilder builder = grid.binary().builder("custom_type");
final BinaryObject entity = builder.setField("id", 0).build();
cache.put(entity, entity);
assert entity.equals(cache.get(entity));
assert store.map.containsKey(entity);
cache.remove(entity);
assert cache.get(entity) == null;
assert !store.map.containsKey(entity);
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderDefaultMappersSelfTest method testCharField.
/**
* @throws Exception If failed.
*/
public void testCharField() throws Exception {
BinaryObjectBuilder builder = builder("Class");
builder.setField("charField", (char) 1);
BinaryObject po = builder.build();
assertEquals(expectedHashCode("Class"), po.type().typeId());
assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());
assertEquals((char) 1, po.<Character>field("charField").charValue());
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderDefaultMappersSelfTest method testOffheapBinary.
/**
* @throws Exception If failed.
*/
public void testOffheapBinary() throws Exception {
BinaryObjectBuilder builder = builder("Class");
builder.setField("i", 111);
builder.setField("f", 111.111f);
builder.setField("iArr", new int[] { 1, 2, 3 });
builder.setField("obj", new Key(1));
builder.setField("col", Arrays.asList(new Value(1), new Value(2)), Collection.class);
BinaryObject po = builder.build();
byte[] arr = ((CacheObjectBinaryProcessorImpl) (grid(0)).context().cacheObjects()).marshal(po);
long ptr = GridUnsafe.allocateMemory(arr.length + 5);
try {
long ptr0 = ptr;
GridUnsafe.putBoolean(null, ptr0++, false);
int len = arr.length;
if (BIG_ENDIAN)
GridUnsafe.putIntLE(ptr0, len);
else
GridUnsafe.putInt(ptr0, len);
GridUnsafe.copyHeapOffheap(arr, GridUnsafe.BYTE_ARR_OFF, ptr0 + 4, arr.length);
BinaryObject offheapObj = (BinaryObject) ((CacheObjectBinaryProcessorImpl) (grid(0)).context().cacheObjects()).unmarshal(ptr, false);
assertEquals(BinaryObjectOffheapImpl.class, offheapObj.getClass());
assertEquals(expectedHashCode("Class"), offheapObj.type().typeId());
assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), offheapObj.hashCode());
assertEquals(111, offheapObj.<Integer>field("i").intValue());
assertEquals(111.111f, offheapObj.<Float>field("f").floatValue(), 0);
assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, offheapObj.<int[]>field("iArr")));
assertEquals(1, offheapObj.<BinaryObject>field("obj").<Key>deserialize().i);
List<BinaryObject> list = offheapObj.field("col");
assertEquals(2, list.size());
assertEquals(1, list.get(0).<Value>deserialize().i);
assertEquals(2, list.get(1).<Value>deserialize().i);
assertEquals(po, offheapObj);
assertEquals(offheapObj, po);
} finally {
GridUnsafe.freeMemory(ptr);
}
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderDefaultMappersSelfTest method testMetaData.
/**
* @throws Exception If failed.
*/
public void testMetaData() throws Exception {
BinaryObjectBuilder builder = builder("org.test.MetaTest");
builder.setField("intField", 1);
builder.setField("byteArrayField", new byte[] { 1, 2, 3 });
BinaryObject po = builder.build();
BinaryType meta = po.type();
assertEquals(expectedTypeName("org.test.MetaTest"), meta.typeName());
Collection<String> fields = meta.fieldNames();
assertEquals(2, fields.size());
assertTrue(fields.contains("intField"));
assertTrue(fields.contains("byteArrayField"));
assertEquals("int", meta.fieldTypeName("intField"));
assertEquals("byte[]", meta.fieldTypeName("byteArrayField"));
builder = builder("org.test.MetaTest");
builder.setField("intField", 2);
builder.setField("uuidField", UUID.randomUUID());
po = builder.build();
meta = po.type();
assertEquals(expectedTypeName("org.test.MetaTest"), meta.typeName());
fields = meta.fieldNames();
assertEquals(3, fields.size());
assertTrue(fields.contains("intField"));
assertTrue(fields.contains("byteArrayField"));
assertTrue(fields.contains("uuidField"));
assertEquals("int", meta.fieldTypeName("intField"));
assertEquals("byte[]", meta.fieldTypeName("byteArrayField"));
assertEquals("UUID", meta.fieldTypeName("uuidField"));
}
Aggregations