Search in sources :

Example 1 with GridCacheVersionedEntryEx

use of org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx in project ignite by apache.

the class IgniteTxAdapter method conflictResolve.

/**
     * Resolve DR conflict.
     *
     * @param op Initially proposed operation.
     * @param txEntry TX entry being updated.
     * @param newVal New value.
     * @param newVer New version.
     * @param old Old entry.
     * @return Tuple with adjusted operation type and conflict context.
     * @throws IgniteCheckedException In case of eny exception.
     * @throws GridCacheEntryRemovedException If entry got removed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
protected IgniteBiTuple<GridCacheOperation, GridCacheVersionConflictContext> conflictResolve(GridCacheOperation op, IgniteTxEntry txEntry, CacheObject newVal, GridCacheVersion newVer, GridCacheEntryEx old) throws IgniteCheckedException, GridCacheEntryRemovedException {
    assert newVer != null;
    // 1. Calculate TTL and expire time.
    long newTtl = txEntry.ttl();
    long newExpireTime = txEntry.conflictExpireTime();
    // 1.1. If TTL is not changed, then calculate it based on expiry.
    if (newTtl == CU.TTL_NOT_CHANGED) {
        ExpiryPolicy expiry = txEntry.context().expiryForTxEntry(txEntry);
        if (expiry != null) {
            if (op == CREATE)
                newTtl = CU.toTtl(expiry.getExpiryForCreation());
            else if (op == UPDATE)
                newTtl = CU.toTtl(expiry.getExpiryForUpdate());
        }
    }
    // 1.2. If TTL is set to zero, then mark operation as "DELETE".
    if (newTtl == CU.TTL_ZERO) {
        op = DELETE;
        newTtl = CU.TTL_ETERNAL;
    }
    // 1.3. If TTL is still not changed, then either use old entry TTL or set it to "ETERNAL".
    if (newTtl == CU.TTL_NOT_CHANGED) {
        if (old.isNewLocked())
            newTtl = CU.TTL_ETERNAL;
        else {
            newTtl = old.rawTtl();
            newExpireTime = old.rawExpireTime();
        }
    }
    // TTL must be resolved at this point.
    assert newTtl != CU.TTL_ZERO && newTtl != CU.TTL_NOT_CHANGED;
    // 1.4 If expire time was not set explicitly, then calculate it.
    if (newExpireTime == CU.EXPIRE_TIME_CALCULATE)
        newExpireTime = CU.toExpireTime(newTtl);
    // Expire time must be resolved at this point.
    assert newExpireTime != CU.EXPIRE_TIME_CALCULATE;
    // Construct old entry info.
    GridCacheVersionedEntryEx oldEntry = old.versionedEntry(txEntry.keepBinary());
    // Construct new entry info.
    GridCacheContext entryCtx = txEntry.context();
    GridCacheVersionedEntryEx newEntry = new GridCacheLazyPlainVersionedEntry(entryCtx, txEntry.key(), newVal, newTtl, newExpireTime, newVer, false, txEntry.keepBinary());
    GridCacheVersionConflictContext ctx = old.context().conflictResolve(oldEntry, newEntry, false);
    if (ctx.isMerge()) {
        Object resVal = ctx.mergeValue();
        if ((op == CREATE || op == UPDATE) && resVal == null)
            op = DELETE;
        else if (op == DELETE && resVal != null)
            op = old.isNewLocked() ? CREATE : UPDATE;
    }
    return F.t(op, ctx);
}
Also used : GridCacheVersionConflictContext(org.apache.ignite.internal.processors.cache.version.GridCacheVersionConflictContext) GridCacheLazyPlainVersionedEntry(org.apache.ignite.internal.processors.cache.version.GridCacheLazyPlainVersionedEntry) GridCacheVersionedEntryEx(org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 GridCacheLazyPlainVersionedEntry (org.apache.ignite.internal.processors.cache.version.GridCacheLazyPlainVersionedEntry)1 GridCacheVersionConflictContext (org.apache.ignite.internal.processors.cache.version.GridCacheVersionConflictContext)1 GridCacheVersionedEntryEx (org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx)1