Package org.apache.ignite.internal.processors.cache.distributed.near

## Class GridNearCacheAdapter<K,​V>

- java.lang.Object
- - org.apache.ignite.internal.processors.cache.GridCacheAdapter<K,​V>
  - - org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheAdapter<K,​V>
    - - org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter<K,​V>

All Implemented Interfaces:
  `Externalizable`, `Serializable`, `Iterable<javax.cache.Cache.Entry<K,​V>>`, `IgniteInternalCache<K,​V>`

Direct Known Subclasses:
  `GridNearAtomicCache`, `GridNearTransactionalCache`

    public abstract class GridNearCacheAdapter<K,​V>
    extends GridDistributedCacheAdapter<K,​V>

Common logic for near caches (smaller local cache that stores most recently or most frequently accessed data).

See Also:
  Serialized Form

### Nested Class Summary

### Nested classes/interfaces inherited from class org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheAdapter

`GridDistributedCacheAdapter.GlobalRemoveAllJob<K,​V>`

### Nested classes/interfaces inherited from class org.apache.ignite.internal.processors.cache.GridCacheAdapter

`GridCacheAdapter.AsyncOp<T>, GridCacheAdapter.AtomicReadRepairEntryProcessor<K,​V>, GridCacheAdapter.BulkOperation, GridCacheAdapter.CacheExpiryPolicy, GridCacheAdapter.FutureHolder, GridCacheAdapter.InvokeAllTimeStatClosure<T>, GridCacheAdapter.PeekModes, GridCacheAdapter.TopologyVersionAwareJob, GridCacheAdapter.UpdateGetAllTimeStatClosure<T>, GridCacheAdapter.UpdateGetAndRemoveTimeStatClosure<T>, GridCacheAdapter.UpdateGetTimeStatClosure<T>, GridCacheAdapter.UpdatePutAllConflictTimeStatClosure<T>, GridCacheAdapter.UpdatePutAllTimeStatClosure<T>, GridCacheAdapter.UpdatePutAndGetTimeStatClosure<T>, GridCacheAdapter.UpdatePutTimeStatClosure<T>, GridCacheAdapter.UpdateRemoveAllConflictTimeStatClosure<T>, GridCacheAdapter.UpdateRemoveAllTimeStatClosure<T>, GridCacheAdapter.UpdateRemoveTimeStatClosure, GridCacheAdapter.UpdateTimeStatClosure<T>`

### Field Summary

### Fields inherited from class org.apache.ignite.internal.processors.cache.GridCacheAdapter

`cacheCfg, CLEAR_ALL_SPLIT_THRESHOLD, ctx, DFLT_CACHE_RETRIES_COUNT, DFLT_CACHE_START_SIZE, DFLT_START_CACHE_SIZE, lastFut, locNodeId, log, map, MAX_RETRIES, metrics, NON_TRANSACTIONAL_IGNITE_CACHE_CLEAR_IN_TX_ERROR_MESSAGE, RET2NULL, txLockMsgLog`

### Constructor Summary

Constructors
`protected` `GridNearCacheAdapter()` Empty constructor required for `Externalizable`.
`protected` `GridNearCacheAdapter​(GridCacheContext<K,​V> ctx)`

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods
`boolean` `clearLocally​(K key)` Clears an entry from this cache and swap storage only if the entry is not currently locked, and is not participating in a transaction.
`void` `clearLocallyAll​(Set<? extends K> keys, boolean srv, boolean near, boolean readers)` Clears entries from this cache and swap storage only if the entry is not currently locked, and is not participating in a transaction.
`abstract GridDhtCacheAdapter<K,​V>` `dht()`
`GridCacheMapEntry` `entryEx​(KeyCacheObject key, AffinityTopologyVersion topVer)`
`GridNearCacheEntry` `entryExx​(KeyCacheObject key, AffinityTopologyVersion topVer)`
`Set<javax.cache.Cache.Entry<K,​V>>` `entrySet​(@Nullable CacheEntryPredicate... filter)`
`boolean` `evict​(K key)` Evicts entry associated with given key from cache.
`void` `evictAll​(Collection<? extends K> keys)` Attempts to evict all entries associated with keys.
`boolean` `isAllLockedNearOnly​(Iterable<? extends K> keys)`
`boolean` `isLocked​(K key)` Checks if any node owns a lock for this key.
`boolean` `isLockedNearOnly​(K key)`
`boolean` `isNear()`
`IgniteInternalFuture<Map<K,​V>>` `loadAsync​(@Nullable IgniteInternalTx tx, @Nullable Collection<KeyCacheObject> keys, boolean forcePrimary, String taskName, boolean deserializeBinary, boolean recovery, @Nullable javax.cache.expiry.ExpiryPolicy expiryPlc, boolean skipVal, boolean skipStore, boolean needVer)`
`void` `localLoad​(Collection<? extends K> keys, javax.cache.expiry.ExpiryPolicy plc, boolean keepBinary)`
`void` `localLoadCache​(IgniteBiPredicate<K,​V> p, Object[] args)` Delegates to `CacheStore.loadCache(IgniteBiInClosure, Object...)` method to load state from the underlying persistent storage.
`IgniteInternalFuture<?>` `localLoadCacheAsync​(IgniteBiPredicate<K,​V> p, Object[] args)` Asynchronously delegates to `CacheStore.loadCache(IgniteBiInClosure, Object...)` method to reload state from the underlying persistent storage.
`Set<javax.cache.Cache.Entry<K,​V>>` `nearEntries()`
`int` `nearSize()` Gets size of near cache key set.
`long` `offHeapAllocatedSize()` Gets memory size allocated in off-heap.
`long` `offHeapEntriesCount()` Gets number of cache entries stored in off-heap memory.
`void` `onReconnected()`
`@Nullable GridNearCacheEntry` `peekExx​(KeyCacheObject key)`
`GridCachePreloader` `preloader()`
`int` `primarySize()` Gets the number of all primary entries cached on this node.
`long` `primarySizeLong()` Gets the number of all primary entries cached on this node as a long value.
`protected void` `processGetResponse​(UUID nodeId, GridNearGetResponse res)`
`int` `size()` Gets the number of all entries cached on this node.
`long` `sizeLong()` Gets the number of all entries cached on this node as a long value.
`List<GridCacheClearAllRunnable<K,​V>>` `splitClearLocally​(boolean srv, boolean near, boolean readers)` Split clearLocally all task into multiple runnables.
`void` `start()` Starts this cache.
`String` `toString()`

### Methods inherited from class org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheAdapter

`localSizeLong, localSizeLong, lockAllAsync, lockAllAsync, removeAll, removeAllAsync, removeVersionedEntry, txLockAsync`

### Methods inherited from class org.apache.ignite.internal.processors.cache.GridCacheAdapter

`active, active, affinity, allEntries, asyncOp, asyncOpAcquire, asyncOpRelease, cache, checkJta, clear, clear, clearAll, clearAllAsync, clearAsync, clearAsync, clearLocally, clearLocally, clusterMetrics, clusterMetrics, configuration, containsKey, containsKeyAsync, containsKeys, containsKeysAsync, context, decrementSize, entries, entryEx, entryEx, entrySet, expiry, expiryPolicy, get, get, getAll, getAll, getAllAsync, getAllAsync, getAllOutTx, getAllOutTxAsync, getAndPut, getAndPut, getAndPut0, getAndPutAsync, getAndPutAsync, getAndPutAsync0, getAndPutIfAbsent, getAndPutIfAbsentAsync, getAndRemove, getAndRemove0, getAndRemoveAsync, getAndRemoveAsync0, getAndReplace, getAndReplaceAsync, getAsync, getAsync, getEntries, getEntriesAsync, getEntry, getEntryAsync, getForcePrimary, getForcePrimaryAsync, igniteIterator, igniteIterator, incrementSize, init, invoke, invoke, invokeAll, invokeAll, invokeAllAsync, invokeAllAsync, invokeAsync, isColocated, isDht, isDhtAtomic, isEmpty, isLocal, isLockedByThread, iterator, iterator, keepBinary, keySet, lastAsyncFuture, loadAll, localEntries, localMetrics, localPeek, localPreloadPartition, localSize, lock, lockAll, lockAsync, log, lostPartitions, map, metrics0, name, nextVersion, nextVersion, onDeferredDelete, onKernalStart, onKernalStop, onUndeploy, parsePeekModes, peekEx, peekEx, preloadPartition, preloadPartitionAsync, printMemoryStats, put, put, put0, putAll, putAll0, putAllAsync, putAllAsync0, putAllConflict, putAllConflictAsync, putAsync, putAsync, putAsync0, putIfAbsent, putIfAbsentAsync, readExternal, readResolve, rebalance, remove, remove, remove, remove0, removeAll, removeAll0, removeAllAsync, removeAllAsync0, removeAllConflict, removeAllConflictAsync, removeAsync, removeAsync, removeAsync, removeAsync0, removeEntry, removeIfObsolete, removeMetrics, repairableGet, repairableGetAll, repairableGetAllAsync, repairableGetAsync, replace, replace, replaceAsync, replaceAsync, scanIterator, setSkipStore, size, sizeAsync, sizeLong, sizeLong, sizeLongAsync, sizeLongAsync, skipStore, startInfo, stop, stopInfo, tx, txStart, txStart, txStartEx, unlock, warnIfUnordered, warnIfUnordered, withExpiryPolicy, withNoRetries, writeExternal`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.apache.ignite.internal.processors.cache.IgniteInternalCache

`unlockAll`

### Methods inherited from interface java.lang.Iterable

`forEach, spliterator`

### Constructor Detail

#### GridNearCacheAdapter

    protected GridNearCacheAdapter()

Empty constructor required for `Externalizable`.

#### GridNearCacheAdapter

    protected GridNearCacheAdapter​(GridCacheContext<K,​V> ctx)

Parameters:
  `ctx` - Context.

### Method Detail

#### start

    public void start()
               throws IgniteCheckedException

Starts this cache. Child classes should override this method to provide custom start-up behavior.

Specified by:
  `start` in class `GridCacheAdapter<K,​V>`
Throws:
  `IgniteCheckedException` - If start failed.

#### dht

    public abstract GridDhtCacheAdapter<K,​V> dht()

Returns:
  DHT cache.

#### onReconnected

    public void onReconnected()

Overrides:
  `onReconnected` in class `GridCacheAdapter<K,​V>`

#### isNear

    public boolean isNear()

Overrides:
  `isNear` in class `GridCacheAdapter<K,​V>`
Returns:
  `True` if this is near cache.

#### preloader

    public GridCachePreloader preloader()

Specified by:
  `preloader` in class `GridCacheAdapter<K,​V>`
Returns:
  Preloader.

#### entryEx

    public GridCacheMapEntry entryEx​(KeyCacheObject key,
                                     AffinityTopologyVersion topVer)

Overrides:
  `entryEx` in class `GridCacheAdapter<K,​V>`
Parameters:
  `key` - Entry key.
  `topVer` - Topology version.
Returns:
  Entry (never `null`).

#### entryExx

    public GridNearCacheEntry entryExx​(KeyCacheObject key,
                                       AffinityTopologyVersion topVer)

Parameters:
  `key` - Key.
  `topVer` - Topology version.
Returns:
  Entry.

#### peekExx

    @Nullable
    public @Nullable GridNearCacheEntry peekExx​(KeyCacheObject key)

Parameters:
  `key` - Key.
Returns:
  Entry.

#### isLocked

    public boolean isLocked​(K key)

Checks if any node owns a lock for this key.

This is a local in-VM operation and does not involve any network trips or access to persistent storage in any way.

Specified by:
  `isLocked` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `isLocked` in class `GridCacheAdapter<K,​V>`
Parameters:
  `key` - Key to check.
Returns:
  `True` if lock is owned by some node.

#### isLockedNearOnly

    public boolean isLockedNearOnly​(K key)

Parameters:
  `key` - Key.
Returns:
  If near entry is locked.

#### isAllLockedNearOnly

    public boolean isAllLockedNearOnly​(Iterable<? extends K> keys)

Parameters:
  `keys` - Keys.
Returns:
  If near entries for given keys are locked.

#### loadAsync

    public IgniteInternalFuture<Map<K,​V>> loadAsync​(@Nullable
                                                          @Nullable IgniteInternalTx tx,
                                                          @Nullable
                                                          @Nullable Collection<KeyCacheObject> keys,
                                                          boolean forcePrimary,
                                                          String taskName,
                                                          boolean deserializeBinary,
                                                          boolean recovery,
                                                          @Nullable
                                                          @Nullable javax.cache.expiry.ExpiryPolicy expiryPlc,
                                                          boolean skipVal,
                                                          boolean skipStore,
                                                          boolean needVer)

Parameters:
  `tx` - Transaction.
  `keys` - Keys to load.
  `forcePrimary` - Force primary flag.
  `taskName` - Task name.
  `deserializeBinary` - Deserialize binary flag.
  `expiryPlc` - Expiry policy.
  `skipVal` - Skip value flag.
  `skipStore` - Skip store flag.
  `needVer` - Need version.
Returns:
  Loaded values.

#### localLoadCache

    public void localLoadCache​(IgniteBiPredicate<K,​V> p,
                               Object[] args)
                        throws IgniteCheckedException

Delegates to `CacheStore.loadCache(IgniteBiInClosure, Object...)` method to load state from the underlying persistent storage. The loaded values will then be given to the optionally passed in predicate, and, if the predicate returns `true`, will be stored in cache. If predicate is `null`, then all loaded values will be stored in cache.

Note that this method does not receive keys as a parameter, so it is up to `CacheStore` implementation to provide all the data to be loaded.

This method is not transactional and may end up loading a stale value into cache if another thread has updated the value immediately after it has been loaded. It is mostly useful when pre-loading the cache from underlying data store before start, or for read-only caches.

Specified by:
  `localLoadCache` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `localLoadCache` in class `GridCacheAdapter<K,​V>`
Parameters:
  `p` - Optional predicate (may be `null`). If provided, will be used to filter values to be put into cache.
  `args` - Optional user arguments to be passed into `CacheStore.loadCache(IgniteBiInClosure, Object...)` method.
Throws:
  `IgniteCheckedException` - If loading failed.

#### localLoad

    public void localLoad​(Collection<? extends K> keys,
                          javax.cache.expiry.ExpiryPolicy plc,
                          boolean keepBinary)
                   throws IgniteCheckedException

Overrides:
  `localLoad` in class `GridCacheAdapter<K,​V>`
Parameters:
  `keys` - Keys to load.
  `plc` - Optional expiry policy.
Throws:
  `IgniteCheckedException` - If failed.

#### localLoadCacheAsync

    public IgniteInternalFuture<?> localLoadCacheAsync​(IgniteBiPredicate<K,​V> p,
                                                       Object[] args)

Asynchronously delegates to `CacheStore.loadCache(IgniteBiInClosure, Object...)` method to reload state from the underlying persistent storage. The reloaded values will then be given to the optionally passed in predicate, and if the predicate returns `true`, will be stored in cache. If predicate is `null`, then all reloaded values will be stored in cache.

Note that this method does not receive keys as a parameter, so it is up to `CacheStore` implementation to provide all the data to be loaded.

This method is not transactional and may end up loading a stale value into cache if another thread has updated the value immediately after it has been loaded. It is mostly useful when pre-loading the cache from underlying data store before start, or for read-only caches.

Specified by:
  `localLoadCacheAsync` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `localLoadCacheAsync` in class `GridCacheAdapter<K,​V>`
Parameters:
  `p` - Optional predicate (may be `null`). If provided, will be used to filter values to be put into cache.
  `args` - Optional user arguments to be passed into `CacheStore.loadCache(IgniteBiInClosure, Object...)` method.
Returns:
  Future to be completed whenever loading completes.

#### processGetResponse

    protected void processGetResponse​(UUID nodeId,
                                      GridNearGetResponse res)

Parameters:
  `nodeId` - Sender ID.
  `res` - Response.

#### size

    public int size()

Gets the number of all entries cached on this node. This method will return the count of all cache entries and has O(1) complexity on base `IgniteInternalCache`. It is essentially the size of cache key set and is semantically identical to {`Cache.keySet().size()`.

NOTE: this operation is not distributed and returns only the number of entries cached on this node.

Specified by:
  `size` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `size` in class `GridCacheAdapter<K,​V>`
Returns:
  Size of cache on this node.

#### sizeLong

    public long sizeLong()

Gets the number of all entries cached on this node as a long value. This method will return the count of all cache entries and has O(1) complexity on base `IgniteInternalCache`. It is essentially the size of cache key set and is semantically identical to {`Cache.keySet().size()`.

NOTE: this operation is not distributed and returns only the number of entries cached on this node.

Specified by:
  `sizeLong` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `sizeLong` in class `GridCacheAdapter<K,​V>`
Returns:
  Size of cache on this node.

#### primarySize

    public int primarySize()

Gets the number of all primary entries cached on this node.

For `CacheMode.PARTITIONED` and `CacheMode.REPLICATED` modes, this method will return number of primary entries cached on this node (excluding any backups). The complexity of this method is O(P), where P is the total number of partitions.

NOTE: this operation is not distributed and returns only the number of primary entries cached on this node.

Specified by:
  `primarySize` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `primarySize` in class `GridCacheAdapter<K,​V>`
Returns:
  Number of primary entries in cache.

#### primarySizeLong

    public long primarySizeLong()

Gets the number of all primary entries cached on this node as a long value.

For `CacheMode.PARTITIONED` and `CacheMode.REPLICATED` modes, this method will return number of primary entries cached on this node (excluding any backups). The complexity of this method is O(P), where P is the total number of partitions.

NOTE: this operation is not distributed and returns only the number of primary entries cached on this node.

Specified by:
  `primarySizeLong` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `primarySizeLong` in class `GridCacheAdapter<K,​V>`
Returns:
  Number of primary entries in cache.

#### nearSize

    public int nearSize()

Gets size of near cache key set. This method will return count of all entries in near cache and has O(1) complexity on base cache projection.

Note that for `LOCAL` non-distributed caches this method will always return `0`

Specified by:
  `nearSize` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `nearSize` in class `GridCacheAdapter<K,​V>`
Returns:
  Size of near cache key set or `0` if cache is not `CacheMode.PARTITIONED`.

#### nearEntries

    public Set<javax.cache.Cache.Entry<K,​V>> nearEntries()

Returns:
  Near entries.

#### entrySet

    public Set<javax.cache.Cache.Entry<K,​V>> entrySet​(@Nullable
                                                            @Nullable CacheEntryPredicate... filter)

Overrides:
  `entrySet` in class `GridCacheAdapter<K,​V>`
Parameters:
  `filter` - Filters to evaluate.
Returns:
  Entry set.

#### evict

    public boolean evict​(K key)

Evicts entry associated with given key from cache. Note, that entry will be evicted only if it's not used (not participating in any locks or transactions).

Specified by:
  `evict` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `evict` in class `GridCacheAdapter<K,​V>`
Parameters:
  `key` - Key to evict from cache.
Returns:
  `True` if entry could be evicted, `false` otherwise.

#### evictAll

    public void evictAll​(Collection<? extends K> keys)

Attempts to evict all entries associated with keys. Note, that entry will be evicted only if it's not used (not participating in any locks or transactions).

Specified by:
  `evictAll` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `evictAll` in class `GridCacheAdapter<K,​V>`
Parameters:
  `keys` - Keys to evict.

#### clearLocally

    public boolean clearLocally​(K key)

Clears an entry from this cache and swap storage only if the entry is not currently locked, and is not participating in a transaction.

Note that this operation is local as it merely clears an entry from local cache. It does not remove entries from remote caches or from underlying persistent storage.

Specified by:
  `clearLocally` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `clearLocally` in class `GridCacheAdapter<K,​V>`
Parameters:
  `key` - Key to clearLocally.
Returns:
  `True` if entry was successfully cleared from cache, `false` if entry was in use at the time of this method invocation and could not be cleared.

#### clearLocallyAll

    public void clearLocallyAll​(Set<? extends K> keys,
                                boolean srv,
                                boolean near,
                                boolean readers)

Clears entries from this cache and swap storage only if the entry is not currently locked, and is not participating in a transaction.

Note that this operation is local as it merely clears an entry from local cache. It does not remove entries from remote caches or from underlying persistent storage.

Specified by:
  `clearLocallyAll` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `clearLocallyAll` in class `GridCacheAdapter<K,​V>`
Parameters:
  `keys` - Keys to clearLocally.
  `srv` - Whether to clear server cache.
  `near` - Whether to clear near cache.
  `readers` - Whether to clear readers.

#### offHeapEntriesCount

    public long offHeapEntriesCount()

Gets number of cache entries stored in off-heap memory.

Specified by:
  `offHeapEntriesCount` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `offHeapEntriesCount` in class `GridCacheAdapter<K,​V>`
Returns:
  Number of cache entries stored in off-heap memory.

#### offHeapAllocatedSize

    public long offHeapAllocatedSize()

Gets memory size allocated in off-heap.

Specified by:
  `offHeapAllocatedSize` in interface `IgniteInternalCache<K,​V>`
Overrides:
  `offHeapAllocatedSize` in class `GridCacheAdapter<K,​V>`
Returns:
  Allocated memory size.

#### splitClearLocally

    public List<GridCacheClearAllRunnable<K,​V>> splitClearLocally​(boolean srv,
                                                                        boolean near,
                                                                        boolean readers)

Split clearLocally all task into multiple runnables.

Overrides:
  `splitClearLocally` in class `GridCacheAdapter<K,​V>`
Parameters:
  `srv` - Whether to clear server cache.
  `near` - Whether to clear near cache.
  `readers` - Whether to clear readers.
Returns:
  Split runnables.

#### toString

    public String toString()

Overrides:
  `toString` in class `GridDistributedCacheAdapter<K,​V>`