Search in sources :

Example 6 with BinaryObjectImpl

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;
}
Also used : BinaryClassDescriptor(org.apache.ignite.internal.binary.BinaryClassDescriptor) BinaryArray(org.apache.ignite.internal.binary.BinaryArray) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridMapEntry(org.apache.ignite.internal.util.lang.GridMapEntry) GridMapEntry(org.apache.ignite.internal.util.lang.GridMapEntry) Collection(java.util.Collection) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) IncompleteCacheObject(org.apache.ignite.internal.processors.cache.IncompleteCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) MutableSingletonList(org.apache.ignite.internal.util.MutableSingletonList) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BinaryEnumArray(org.apache.ignite.internal.binary.BinaryEnumArray) T2(org.apache.ignite.internal.util.typedef.T2)

Example 7 with BinaryObjectImpl

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]");
}
Also used : CacheObjectBinaryProcessorImpl(org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Path(java.nio.file.Path) IgniteSnapshotManager.databaseRelativePath(org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) CheckpointListener(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteDataIntegrityViolationException(org.apache.ignite.internal.processors.cache.persistence.wal.crc.IgniteDataIntegrityViolationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteEx(org.apache.ignite.internal.IgniteEx) GridIterator(org.apache.ignite.internal.util.lang.GridIterator) IdleVerifyResultV2(org.apache.ignite.internal.processors.cache.verify.IdleVerifyResultV2) Test(org.junit.Test)

Example 8 with BinaryObjectImpl

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));
}
Also used : BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 9 with BinaryObjectImpl

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());
}
Also used : PlainJavaObjectIndexKey(org.apache.ignite.internal.cache.query.index.sorted.keys.PlainJavaObjectIndexKey) IntegerIndexKey(org.apache.ignite.internal.cache.query.index.sorted.keys.IntegerIndexKey) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

BinaryObjectImpl (org.apache.ignite.internal.binary.BinaryObjectImpl)9 BinaryObject (org.apache.ignite.binary.BinaryObject)4 Test (org.junit.Test)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 BinaryArray (org.apache.ignite.internal.binary.BinaryArray)2 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 MutableSingletonList (org.apache.ignite.internal.util.MutableSingletonList)2 GridMapEntry (org.apache.ignite.internal.util.lang.GridMapEntry)2 T2 (org.apache.ignite.internal.util.typedef.T2)2 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1