use of org.apache.ignite.internal.binary.BinaryObjectImpl in project ignite by apache.
the class CacheObjectBinaryProcessorImpl method marshalToBinary.
/**
* {@inheritDoc}
*/
@Override
public Object marshalToBinary(@Nullable Object obj, boolean failIfUnregistered) throws BinaryObjectException {
if (obj == null)
return null;
if (BinaryUtils.isBinaryType(obj.getClass()))
return obj;
if (obj instanceof Object[]) {
Object[] arr = (Object[]) obj;
Object[] pArr = new Object[arr.length];
for (int i = 0; i < arr.length; i++) pArr[i] = marshalToBinary(arr[i], failIfUnregistered);
if (!BinaryArray.useBinaryArrays())
return pArr;
Class<?> compCls = obj.getClass().getComponentType();
boolean isBinaryArr = BinaryObject.class.isAssignableFrom(compCls);
String compClsName = isBinaryArr ? Object.class.getName() : compCls.getName();
// In case of interface or multidimensional array rely on class name.
// Interfaces and array not registered as binary types.
BinaryClassDescriptor desc = binaryCtx.descriptorForClass(compCls);
if (compCls.isEnum() || compCls == BinaryEnumObjectImpl.class) {
return new BinaryEnumArray(binaryCtx, desc.registered() ? desc.typeId() : GridBinaryMarshaller.UNREGISTERED_TYPE_ID, compClsName, pArr);
} else {
return new BinaryArray(binaryCtx, desc.registered() ? desc.typeId() : GridBinaryMarshaller.UNREGISTERED_TYPE_ID, compClsName, pArr);
}
}
if (obj instanceof IgniteBiTuple) {
IgniteBiTuple tup = (IgniteBiTuple) obj;
if (obj instanceof T2)
return new T2<>(marshalToBinary(tup.get1(), failIfUnregistered), marshalToBinary(tup.get2(), failIfUnregistered));
return new IgniteBiTuple<>(marshalToBinary(tup.get1(), failIfUnregistered), marshalToBinary(tup.get2(), failIfUnregistered));
}
{
Collection<Object> pCol = BinaryUtils.newKnownCollection(obj);
if (pCol != null) {
Collection<?> col = (Collection<?>) obj;
for (Object item : col) pCol.add(marshalToBinary(item, failIfUnregistered));
return (pCol instanceof MutableSingletonList) ? U.convertToSingletonList(pCol) : pCol;
}
}
{
Map<Object, Object> pMap = BinaryUtils.newKnownMap(obj);
if (pMap != null) {
Map<?, ?> map = (Map<?, ?>) obj;
for (Map.Entry<?, ?> e : map.entrySet()) pMap.put(marshalToBinary(e.getKey(), failIfUnregistered), marshalToBinary(e.getValue(), failIfUnregistered));
return pMap;
}
}
if (obj instanceof Map.Entry) {
Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj;
return new GridMapEntry<>(marshalToBinary(e.getKey(), failIfUnregistered), marshalToBinary(e.getValue(), failIfUnregistered));
}
if (binaryMarsh.mustDeserialize(obj))
// No need to go through marshal-unmarshal because result will be the same as initial object.
return obj;
byte[] arr = binaryMarsh.marshal(obj, failIfUnregistered);
assert arr.length > 0;
Object obj0 = binaryMarsh.unmarshal(arr, null);
// Possible if a class has writeObject method.
if (obj0 instanceof BinaryObjectImpl)
((BinaryObjectImpl) obj0).detachAllowed(true);
return obj0;
}
use of org.apache.ignite.internal.binary.BinaryObjectImpl in project ignite by apache.
the class IgniteClusterSnapshotCheckTest method testClusterSnapshotCheckFailsOnPartitionDataDiffers.
/**
* @throws Exception If fails.
*/
@Test
public void testClusterSnapshotCheckFailsOnPartitionDataDiffers() throws Exception {
CacheConfiguration<Integer, Value> ccfg = txCacheConfig(new CacheConfiguration<Integer, Value>(DEFAULT_CACHE_NAME)).setAffinity(new RendezvousAffinityFunction(false, 1));
IgniteEx ignite = startGridsWithoutCache(2);
ignite.getOrCreateCache(ccfg).put(1, new Value(new byte[2000]));
forceCheckpoint(ignite);
GridCacheSharedContext<?, ?> cctx = ignite.context().cache().context();
GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) cctx.database();
BinaryContext binCtx = ((CacheObjectBinaryProcessorImpl) ignite.context().cacheObjects()).binaryContext();
GridCacheAdapter<?, ?> cache = ignite.context().cache().internalCache(dfltCacheCfg.getName());
long partCtr = cache.context().topology().localPartition(PART_ID, NONE, false).dataStore().updateCounter();
AtomicBoolean done = new AtomicBoolean();
db.addCheckpointListener(new CheckpointListener() {
@Override
public void onMarkCheckpointBegin(Context ctx) throws IgniteCheckedException {
// Change the cache value only at on of the cluster node to get hash conflict when the check command ends.
if (!done.compareAndSet(false, true))
return;
GridIterator<CacheDataRow> it = cache.context().offheap().partitionIterator(PART_ID);
assertTrue(it.hasNext());
CacheDataRow row0 = it.nextX();
AffinityTopologyVersion topVer = cctx.exchange().readyAffinityVersion();
GridCacheEntryEx cached = cache.entryEx(row0.key(), topVer);
byte[] bytes = new byte[2000];
new Random().nextBytes(bytes);
try {
BinaryObjectImpl newVal = new BinaryObjectImpl(binCtx, binCtx.marshaller().marshal(new Value(bytes)), 0);
boolean success = cached.initialValue(newVal, new GridCacheVersion(row0.version().topologyVersion(), row0.version().nodeOrder(), row0.version().order() + 1), null, null, TxState.NA, TxState.NA, TTL_ETERNAL, row0.expireTime(), true, topVer, DR_NONE, false, false, null);
assertTrue(success);
long newPartCtr = cache.context().topology().localPartition(PART_ID, NONE, false).dataStore().updateCounter();
assertEquals(newPartCtr, partCtr);
} catch (Exception e) {
throw new IgniteCheckedException(e);
}
}
@Override
public void onCheckpointBegin(Context ctx) throws IgniteCheckedException {
}
@Override
public void beforeCheckpointBegin(Context ctx) throws IgniteCheckedException {
}
});
db.waitForCheckpoint("test-checkpoint");
ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get();
Path part0 = U.searchFileRecursively(snp(ignite).snapshotLocalDir(SNAPSHOT_NAME).toPath(), getPartitionFileName(PART_ID));
assertNotNull(part0);
assertTrue(part0.toString(), part0.toFile().exists());
IdleVerifyResultV2 res = snp(ignite).checkSnapshot(SNAPSHOT_NAME).get();
StringBuilder b = new StringBuilder();
res.print(b::append, true);
assertTrue(F.isEmpty(res.exceptions()));
assertContains(log, b.toString(), "The check procedure has failed, conflict partitions has been found: [counterConflicts=0, hashConflicts=1]");
}
use of org.apache.ignite.internal.binary.BinaryObjectImpl in project ignite by apache.
the class IgniteCachePutKeyAttachedBinaryObjectTest method testAttachedBinaryKeyStoredSuccessfullyToNotEmptyCache.
/**
* @throws Exception If failed.
*/
@Test
public void testAttachedBinaryKeyStoredSuccessfullyToNotEmptyCache() throws Exception {
startGrid(0);
IgniteCache<Object, Object> binCache = grid(0).cache(CACHE_NAME);
// Ensure that cache not empty.
AttachedKey ordinaryKey = new AttachedKey(0);
binCache.put(ordinaryKey, 1);
BinaryObjectBuilder holdBuilder = grid(0).binary().builder(HolderKey.class.getName());
// Creating attached key which stores as byte array.
BinaryObjectImpl attachedKey = holdBuilder.setField("id", new AttachedKey(1)).build().field("id");
// Put data with attached key.
binCache.put(attachedKey, 2);
assertEquals(1, binCache.get(ordinaryKey));
assertEquals(2, binCache.get(attachedKey));
}
use of org.apache.ignite.internal.binary.BinaryObjectImpl in project ignite by apache.
the class InlineIndexKeyTypeRegistryTest method testObjectCheck.
/**
*/
@Test
public void testObjectCheck() {
InlineIndexKeyType t = InlineIndexKeyTypeRegistry.get(new IntegerIndexKey(3), IndexKeyTypes.JAVA_OBJECT, pojoArrayKeyTypeSettings);
assertEquals(IndexKeyTypes.INT, t.type());
t = InlineIndexKeyTypeRegistry.get(new PlainJavaObjectIndexKey(new BinaryObjectImpl(), null), IndexKeyTypes.JAVA_OBJECT, pojoArrayKeyTypeSettings);
assertEquals(IndexKeyTypes.JAVA_OBJECT, t.type());
t = InlineIndexKeyTypeRegistry.get(new PlainJavaObjectIndexKey(new BinaryObjectImpl(), null), IndexKeyTypes.INT, pojoArrayKeyTypeSettings);
assertEquals(IndexKeyTypes.JAVA_OBJECT, t.type());
t = InlineIndexKeyTypeRegistry.get(new IntegerIndexKey(3), IndexKeyTypes.JAVA_OBJECT, pojoHashKeyTypeSettings);
assertEquals(IndexKeyTypes.INT, t.type());
}
Aggregations