Search in sources :

Example 21 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class LocalRegion method remove.

/**
   * Same as {@link #remove(Object, Object)} except a callback argument is supplied to be passed on
   * to <tt>CacheListener</tt>s and/or <tt>CacheWriter</tt>s.
   */
public boolean remove(Object key, Object value, Object callbackArg) {
    checkIfConcurrentMapOpsAllowed();
    validateKey(key);
    validateCallbackArg(callbackArg);
    checkReadiness();
    checkForLimitedOrNoAccess();
    if (value == null) {
        value = Token.INVALID;
    }
    @Released EntryEventImpl event = EntryEventImpl.create(this, Operation.REMOVE, key, null, callbackArg, false, getMyId());
    try {
        if (generateEventID() && event.getEventId() == null) {
            event.setNewEventId(this.cache.getDistributedSystem());
        }
        discoverJTA();
        getDataView().destroyExistingEntry(event, true, value);
    } catch (EntryNotFoundException ignore) {
        return false;
    } catch (RegionDestroyedException rde) {
        if (!rde.getRegionFullPath().equals(getFullPath())) {
            // Handle when a bucket is destroyed
            throw new RegionDestroyedException(toString(), getFullPath(), rde);
        } else {
            throw rde;
        }
    } finally {
        event.release();
    }
    return true;
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException)

Example 22 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class LocalRegion method putIfAbsent.

/**
   * If the specified key is not already associated with a value, associate it with the given value.
   * This is equivalent to
   * 
   * <pre>
   * if (!region.containsKey(key))
   *   return region.put(key, value);
   * else
   *   return region.get(key);
   * </pre>
   * 
   * Except that the action is performed atomically.
   *
   * <i>Note that if this method returns null then there is no way to determine definitely whether
   * this operation succeeded and modified the region, or if the entry is in an invalidated state
   * and no modification occurred.</i>
   *
   * If this method does not modify the region then no listeners or other callbacks are executed. If
   * a modification does occur, then the behavior with respect to callbacks is the same as
   * {@link Region#create(Object, Object)}.
   *
   * @param key key with which the specified value is to be associated.
   * @param value the value for the new entry, which may be null meaning the new entry starts as if
   *        it had been locally invalidated.
   * @return previous value associated with specified key, or <tt>null</tt> if there was no mapping
   *         for key. A <tt>null</tt> return can also indicate that the entry in the region was
   *         previously in an invalidated state.
   *
   * @throws ClassCastException if key does not satisfy the keyConstraint
   * @throws IllegalArgumentException if the key or value is not serializable and this is a
   *         distributed region
   * @throws TimeoutException if timed out getting distributed lock for {@code Scope.GLOBAL}
   * @throws NullPointerException if key is <tt>null</tt>
   * @throws PartitionedRegionStorageException if the operation could not be completed.
   */
public Object putIfAbsent(Object key, Object value, Object callbackArgument) {
    long startPut = CachePerfStats.getStatTime();
    checkIfConcurrentMapOpsAllowed();
    validateArguments(key, value, callbackArgument);
    // TODO ConcurrentMap.putIfAbsent() treats null as an invalidation operation
    // BUT we need to return the old value, which Invalidate isn't currently doing
    checkReadiness();
    checkForLimitedOrNoAccess();
    discoverJTA();
    // This used to call the constructor which took the old value. It
    // was modified to call the other EntryEventImpl constructor so that
    // an id will be generated by default. Null was passed in anyway.
    // generate EventID
    @Released EntryEventImpl event = EntryEventImpl.create(this, Operation.PUT_IF_ABSENT, key, value, callbackArgument, false, getMyId());
    try {
        if (generateEventID()) {
            event.setNewEventId(this.cache.getDistributedSystem());
        }
        final Object oldValue = null;
        final boolean ifNew = true;
        final boolean ifOld = false;
        final boolean requireOldValue = true;
        if (!basicPut(event, ifNew, ifOld, oldValue, requireOldValue)) {
            return event.getOldValue();
        } else {
            if (!getDataView().isDeferredStats()) {
                getCachePerfStats().endPut(startPut, false);
            }
            return null;
        }
    } catch (EntryNotFoundException ignore) {
        return event.getOldValue();
    } finally {
        event.release();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 23 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class LocalRegion method localDestroyNoCallbacks.

/**
   * WARNING: this method is overridden in subclasses.
   */
protected void localDestroyNoCallbacks(Object key) {
    if (logger.isDebugEnabled()) {
        logger.debug("localDestroyNoCallbacks key={}", key);
    }
    checkReadiness();
    validateKey(key);
    @Released EntryEventImpl event = EntryEventImpl.create(this, Operation.LOCAL_DESTROY, key, false, getMyId(), false, /* generateCallbacks */
    true);
    try {
        // expectedOldValue
        basicDestroy(event, false, null);
    } catch (CacheWriterException e) {
        // cache writer not called
        throw new Error(LocalizedStrings.LocalRegion_CACHE_WRITER_SHOULD_NOT_HAVE_BEEN_CALLED_FOR_LOCALDESTROY.toLocalizedString(), e);
    } catch (TimeoutException e) {
        // no distributed lock
        throw new Error(LocalizedStrings.LocalRegion_NO_DISTRIBUTED_LOCK_SHOULD_HAVE_BEEN_ATTEMPTED_FOR_LOCALDESTROY.toLocalizedString(), e);
    } catch (EntryNotFoundException ignore) {
    // not a problem
    } finally {
        event.release();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InternalGemFireError(org.apache.geode.InternalGemFireError) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 24 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class LocalRegion method basicBridgeCreate.

public boolean basicBridgeCreate(final Object key, final byte[] value, boolean isObject, Object callbackArg, final ClientProxyMembershipID client, boolean fromClient, EntryEventImpl clientEvent, boolean throwEntryExists) throws TimeoutException, EntryExistsException, CacheWriterException {
    EventID eventId = clientEvent.getEventId();
    Object theCallbackArg = callbackArg;
    long startPut = CachePerfStats.getStatTime();
    if (fromClient) {
        // GatewayEventCallbackArgument to store the event id.
        if (isGatewaySenderEnabled()) {
            theCallbackArg = new GatewaySenderEventCallbackArgument(theCallbackArg);
        }
    }
    @Released final EntryEventImpl event = EntryEventImpl.create(this, Operation.CREATE, key, value, theCallbackArg, false, /* origin remote */
    client.getDistributedMember(), true, /* generateCallbacks */
    eventId);
    try {
        event.setContext(client);
        // if this is a replayed operation or WAN event we may already have a version tag
        event.setVersionTag(clientEvent.getVersionTag());
        // carry over the possibleDuplicate flag from clientEvent
        event.setPossibleDuplicate(clientEvent.isPossibleDuplicate());
        // normal regions. Otherwise, it will become a distributed invalidate.
        if (getDataPolicy() == DataPolicy.NORMAL) {
            event.setLocalInvalid(true);
        }
        // Set the new value to the input byte[] if it isn't null
        if (value != null) {
            // in a CachedDeserializable; otherwise store it directly as a byte[]
            if (isObject) {
                // The value represents an object
                event.setSerializedNewValue(value);
            } else {
                // The value does not represent an object
                event.setNewValue(value);
            }
        }
        // cannot overwrite an existing key
        boolean ifNew = true;
        // can create a new key
        boolean ifOld = false;
        // use now
        long lastModified = 0L;
        // not okay to overwrite the DESTROYED token
        boolean overwriteDestroyed = false;
        boolean success = basicUpdate(event, ifNew, ifOld, lastModified, overwriteDestroyed);
        clientEvent.isConcurrencyConflict(event.isConcurrencyConflict());
        if (success) {
            clientEvent.setVersionTag(event.getVersionTag());
            getCachePerfStats().endPut(startPut, event.isOriginRemote());
        } else {
            this.stopper.checkCancelInProgress(null);
            if (throwEntryExists) {
                throw new EntryExistsException("" + key, event.getOldValue());
            }
        }
        return success;
    } finally {
        event.release();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) StoredObject(org.apache.geode.internal.offheap.StoredObject) EntryExistsException(org.apache.geode.cache.EntryExistsException) GatewaySenderEventCallbackArgument(org.apache.geode.internal.cache.wan.GatewaySenderEventCallbackArgument)

Example 25 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class LocalRegion method basicBridgePut.

public boolean basicBridgePut(Object key, Object value, byte[] deltaBytes, boolean isObject, Object callbackArg, ClientProxyMembershipID memberId, boolean fromClient, EntryEventImpl clientEvent) throws TimeoutException, CacheWriterException {
    EventID eventID = clientEvent.getEventId();
    Object theCallbackArg = callbackArg;
    long startPut = CachePerfStats.getStatTime();
    if (fromClient) {
        // GatewayEventCallbackArgument to store the event id.
        if (isGatewaySenderEnabled()) {
            theCallbackArg = new GatewaySenderEventCallbackArgument(theCallbackArg);
        }
    }
    @Released final EntryEventImpl event = EntryEventImpl.create(this, Operation.UPDATE, key, null, /* new value */
    theCallbackArg, false, /* origin remote */
    memberId.getDistributedMember(), true, /* generateCallbacks */
    eventID);
    try {
        event.setContext(memberId);
        event.setDeltaBytes(deltaBytes);
        // if this is a replayed operation we may already have a version tag
        event.setVersionTag(clientEvent.getVersionTag());
        // carry over the possibleDuplicate flag from clientEvent
        event.setPossibleDuplicate(clientEvent.isPossibleDuplicate());
        // serialized in a CachedDeserializable; otherwise store it directly as a byte[].
        if (isObject && value instanceof byte[]) {
            event.setSerializedNewValue((byte[]) value);
        } else {
            event.setNewValue(value);
        }
        boolean success = false;
        try {
            // can overwrite an existing key
            boolean ifNew = false;
            // can create a new key
            boolean ifOld = false;
            // use now
            long lastModified = 0L;
            // not okay to overwrite the DESTROYED token
            boolean overwriteDestroyed = false;
            success = basicUpdate(event, ifNew, ifOld, lastModified, overwriteDestroyed);
        } catch (ConcurrentCacheModificationException ignore) {
            // thrown by WAN conflicts
            event.isConcurrencyConflict(true);
        }
        clientEvent.isConcurrencyConflict(event.isConcurrencyConflict());
        if (success) {
            clientEvent.setVersionTag(event.getVersionTag());
            getCachePerfStats().endPut(startPut, event.isOriginRemote());
        } else {
            this.stopper.checkCancelInProgress(null);
        }
        return success;
    } finally {
        event.release();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) StoredObject(org.apache.geode.internal.offheap.StoredObject) GatewaySenderEventCallbackArgument(org.apache.geode.internal.cache.wan.GatewaySenderEventCallbackArgument) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)

Aggregations

Released (org.apache.geode.internal.offheap.annotations.Released)57 StoredObject (org.apache.geode.internal.offheap.StoredObject)29 CacheWriterException (org.apache.geode.cache.CacheWriterException)13 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)13 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)12 ConcurrentCacheModificationException (org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)9 GatewaySenderEventCallbackArgument (org.apache.geode.internal.cache.wan.GatewaySenderEventCallbackArgument)9 Retained (org.apache.geode.internal.offheap.annotations.Retained)9 EntryEventImpl (org.apache.geode.internal.cache.EntryEventImpl)8 VersionedObjectList (org.apache.geode.internal.cache.tier.sockets.VersionedObjectList)8 DiskAccessException (org.apache.geode.cache.DiskAccessException)6 Operation (org.apache.geode.cache.Operation)6 ReplyException (org.apache.geode.distributed.internal.ReplyException)6 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)5 IndexManager (org.apache.geode.cache.query.internal.index.IndexManager)5 PartitionedRegionDataStore (org.apache.geode.internal.cache.PartitionedRegionDataStore)5 TimeoutException (org.apache.geode.cache.TimeoutException)4 EventID (org.apache.geode.internal.cache.EventID)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3