org.apache.ignite.internal.client.impl

## Class GridClientDataImpl

- java.lang.Object
- - org.apache.ignite.internal.client.impl.GridClientDataImpl

All Implemented Interfaces:
  GridClientData

    public class GridClientDataImpl
    extends Object
    implements GridClientData

Data projection that serves one cache instance and handles communication errors.

### Nested Class Summary

Nested Classes
`protected static interface` `GridClientAbstractProjection.ClientProjectionClosure<R>` Closure to execute reconnect-handling code.
`protected static interface` `GridClientAbstractProjection.ProjectionFactory<X extends org.apache.ignite.internal.client.impl.GridClientAbstractProjection>` Factory for new projection creation.

### Field Summary

Fields
`protected GridClientLoadBalancer` `balancer` Balancer to be used in this projection.
`protected GridClientImpl` `client` Client instance.
`protected GridClientPredicate<? super GridClientNode>` `filter` Node filter to be applied for this projection.
`protected Collection<GridClientNode>` `nodes` List of nodes included in this projection.

### Method Summary

All Methods Instance Methods Concrete Methods
`<K> UUID` `affinity(K key)` Gets affinity node ID for provided key.
`<K,V> boolean` `append(K key, V val)` Append requested value to already cached one.
`<K,V> GridClientFuture<Boolean>` `appendAsync(K key, V val)` Append requested value to already cached one.
`GridClientDataMetrics` `cachedMetrics()` Tries to get metrics from local cache.
`String` `cacheName()` Gets name of the remote cache.
`<K,V> boolean` `cas(K key, V val1, V val2)` Sets entry value to `val1` if current value is `val2` with following conditions: If `val1` is `null` and `val2` is equal to current value, entry is removed from cache.
`<K,V> GridClientFuture<Boolean>` `casAsync(K key, V val1, V val2)` Asynchronously sets entry value to `val1` if current value is `val2` with following conditions: If `val1` is `null` and `val2` is equal to current value, entry is removed from cache.
`protected boolean` `checkNodeAlive(UUID nodeId)` Tries to refresh node on every possible connection in topology.
`protected T` `createProjection(@Nullable Collection<GridClientNode> nodes, @Nullable GridClientPredicate<? super GridClientNode> filter, @Nullable GridClientLoadBalancer balancer, GridClientAbstractProjection.ProjectionFactory<T> factory)` Creates a sub-projection for current projection.
`Set<GridClientCacheFlag>` `flags()` Gets cache flags enabled on this data projection.
`GridClientData` `flagsOff(GridClientCacheFlag... flags)` Creates new client data object with disabled cache flags.
`GridClientData` `flagsOn(GridClientCacheFlag... flags)` Creates new client data object with enabled cache flags.
`<K,V> V` `get(K key)` Gets value from cache on remote node.
`<K,V> Map<K,V>` `getAll(Collection<K> keys)` Gets entries from cache on remote grid.
`<K,V> GridClientFuture<Map<K,V>>` `getAllAsync(Collection<K> keys)` Asynchronously gets entries from cache on remote grid.
`<K,V> GridClientFuture<V>` `getAsync(K key)` Asynchronously gets value from cache on remote grid.
`GridClientDataMetrics` `metrics()` Fetches metrics for cache from remote grid.
`GridClientFuture<GridClientDataMetrics>` `metricsAsync()` Asynchronously fetches metrics for cache from remote grid.
`Collection<GridClientNode>` `pinnedNodes()` Gets pinned node or `null` if no nodes were pinned.
`GridClientData` `pinNodes(GridClientNode node, GridClientNode... nodes)` Gets client data projection which will only contact specified remote grid node.
`<K,V> boolean` `prepend(K key, V val)` Prepend requested value to already cached one.
`<K,V> GridClientFuture<Boolean>` `prependAsync(K key, V val)` Prepend requested value to already cached one.
`Collection<? extends GridClientNode>` `projectionNodes()` Gets most recently refreshed topology.
`protected Collection<? extends GridClientNode>` `projectionNodes(@Nullable GridClientPredicate<GridClientNode> pred)` Gets most recently refreshed topology.
`<K,V> boolean` `put(K key, V val)` Puts value to cache on remote grid.
`<K,V> void` `putAll(Map<K,V> entries)` Puts entries to cache on remote grid.
`<K,V> GridClientFuture<?>` `putAllAsync(Map<K,V> entries)` Asynchronously puts entries to cache on remote grid.
`<K,V> GridClientFuture<Boolean>` `putAsync(K key, V val)` Asynchronously puts value to cache on remote node.
`<K> boolean` `remove(K key)` Removes value from cache on remote node.
`<K> void` `removeAll(Collection<K> keys)` Removes entries from cache on remote node.
`<K> GridClientFuture<?>` `removeAllAsync(Collection<K> keys)` Asynchronously removes entries from cache on remote grid.
`<K> GridClientFuture<Boolean>` `removeAsync(K key)` Asynchronously removes value from cache on remote grid.
`<K,V> boolean` `replace(K key, V val)` Replaces value in cache on remote grid only if there was a `non-null` value associated with this key.
`<K,V> GridClientFuture<Boolean>` `replaceAsync(K key, V val)` Asynchronously replaces value in cache on remote grid only if there was a `non-null` value associated with this key.
`protected <R> GridClientFuture<R>` `withReconnectHandling(GridClientAbstractProjection.ClientProjectionClosure<R> c)` This method executes request to a communication layer and handles connection error, if it occurs.
`protected <R> GridClientFuture<R>` `withReconnectHandling(GridClientAbstractProjection.ClientProjectionClosure<R> c, String cacheName, @Nullable Object affKey)` This method executes request to a communication layer and handles connection error, if it occurs.

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

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

### Field Detail

#### nodes

    protected Collection<GridClientNode> nodes

List of nodes included in this projection. If null, all nodes in topology are included.

#### filter

    protected GridClientPredicate<? super GridClientNode> filter

Node filter to be applied for this projection.

#### balancer

    protected GridClientLoadBalancer balancer

Balancer to be used in this projection.

#### client

    protected GridClientImpl client

Client instance.

### Method Detail

#### cacheName

    public String cacheName()

Gets name of the remote cache. The cache name for this projection was specified via `GridClient.data(String)` method at the time of creation.

Specified by:
  `cacheName` in interface `GridClientData`
Returns:
  Name of the remote cache.

#### pinNodes

    public GridClientData pinNodes(GridClientNode node,
                                   GridClientNode... nodes)
                            throws GridClientException

Gets client data projection which will only contact specified remote grid node. By default, remote node is determined based on `GridClientDataAffinity` provided - this method allows to override default behavior and use only specified server for all cache operations.

Use this method when there are other than `key-affinity` reasons why a certain node should be contacted.

Specified by:
  `pinNodes` in interface `GridClientData`
Parameters:
  `node` - Node to be contacted (optional).
  `nodes` - Additional nodes (optional).
Returns:
  Client data which will only contact server with given node ID.
Throws:
  `GridClientException` - If resulting projection is empty.

#### pinnedNodes

    public Collection<GridClientNode> pinnedNodes()

Gets pinned node or `null` if no nodes were pinned.

Specified by:
  `pinnedNodes` in interface `GridClientData`
Returns:
  Pinned node.

#### put

    public <K,V> boolean put(K key,
                             V val)
                      throws GridClientException

Puts value to cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `put` in interface `GridClientData`
Parameters:
  `key` - Key to put in cache.
  `val` - Value to put in cache.
Returns:
  Whether value was actually put to cache.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### putAsync

    public <K,V> GridClientFuture<Boolean> putAsync(K key,
                                                    V val)

Asynchronously puts value to cache on remote node.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `putAsync` in interface `GridClientData`
Parameters:
  `key` - Key to put in cache.
  `val` - Value to put in cache.
Returns:
  Future whether value was actually put to cache.

#### putAll

    public <K,V> void putAll(Map<K,V> entries)
                      throws GridClientException

Puts entries to cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote nodes on which these keys are supposed to be cached (unless some nodes were `pinned`). If entries do not map to one node, then the node which has most mapped entries will be contacted.

Specified by:
  `putAll` in interface `GridClientData`
Parameters:
  `entries` - Entries to put in cache.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### putAllAsync

    public <K,V> GridClientFuture<?> putAllAsync(Map<K,V> entries)

Asynchronously puts entries to cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote nodes on which these keys are supposed to be cached (unless some nodes were `pinned`). If entries do not map to one node, then the node which has most mapped entries will be contacted.

Specified by:
  `putAllAsync` in interface `GridClientData`
Parameters:
  `entries` - Entries to put in cache.
Returns:
  Future whether this operation completes.

#### get

    public <K,V> V get(K key)
                throws GridClientException

Gets value from cache on remote node.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `get` in interface `GridClientData`
Parameters:
  `key` - Key to get from cache.
Returns:
  Value for given key or `null` if no value was cached.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### getAsync

    public <K,V> GridClientFuture<V> getAsync(K key)

Asynchronously gets value from cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `getAsync` in interface `GridClientData`
Parameters:
  `key` - Key to get from cache.
Returns:
  Future with value for given key or with `null` if no value was cached.

#### getAll

    public <K,V> Map<K,V> getAll(Collection<K> keys)
                          throws GridClientException

Gets entries from cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote nodes on which these keys are supposed to be cached (unless some nodes were `pinned`). If entries do not map to one node, then the node which has most mapped entries will be contacted.

Specified by:
  `getAll` in interface `GridClientData`
Parameters:
  `keys` - Keys to get.
Returns:
  Entries retrieved from remote cache nodes.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### getAllAsync

    public <K,V> GridClientFuture<Map<K,V>> getAllAsync(Collection<K> keys)

Asynchronously gets entries from cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote nodes on which these keys are supposed to be cached (unless some nodes were `pinned`). If entries do not map to one node, then the node which has most mapped entries will be contacted.

Specified by:
  `getAllAsync` in interface `GridClientData`
Parameters:
  `keys` - Keys to get.
Returns:
  Future with entries retrieved from remote cache nodes.

#### remove

    public <K> boolean remove(K key)
                       throws GridClientException

Removes value from cache on remote node.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `remove` in interface `GridClientData`
Parameters:
  `key` - Key to remove.
Returns:
  Whether value was actually removed.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### removeAsync

    public <K> GridClientFuture<Boolean> removeAsync(K key)

Asynchronously removes value from cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `removeAsync` in interface `GridClientData`
Parameters:
  `key` - Key to remove.
Returns:
  Future whether value was actually removed.

#### removeAll

    public <K> void removeAll(Collection<K> keys)
                       throws GridClientException

Removes entries from cache on remote node.

Note that this operation is affinity-aware and will immediately contact exactly the remote nodes on which these keys are supposed to be cached (unless some nodes were `pinned`). If entries do not map to one node, then the node which has most mapped entries will be contacted.

Specified by:
  `removeAll` in interface `GridClientData`
Parameters:
  `keys` - Keys to remove.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### removeAllAsync

    public <K> GridClientFuture<?> removeAllAsync(Collection<K> keys)

Asynchronously removes entries from cache on remote grid.

Note that this operation is affinity-aware and will immediately contact exactly the remote nodes on which these keys are supposed to be cached (unless some nodes were `pinned`). If entries do not map to one node, then the node which has most mapped entries will be contacted.

Specified by:
  `removeAllAsync` in interface `GridClientData`
Parameters:
  `keys` - Keys to remove.
Returns:
  Future whether operation finishes.

#### replace

    public <K,V> boolean replace(K key,
                                 V val)
                          throws GridClientException

Replaces value in cache on remote grid only if there was a `non-null` value associated with this key.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `replace` in interface `GridClientData`
Parameters:
  `key` - Key to replace.
  `val` - Value to replace.
Returns:
  Whether value was actually replaced.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### replaceAsync

    public <K,V> GridClientFuture<Boolean> replaceAsync(K key,
                                                        V val)

Asynchronously replaces value in cache on remote grid only if there was a `non-null` value associated with this key.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `replaceAsync` in interface `GridClientData`
Parameters:
  `key` - Key to replace.
  `val` - Value to replace.
Returns:
  Future whether value was actually replaced.

#### cas

    public <K,V> boolean cas(K key,
                             V val1,
                             V val2)
                      throws GridClientException

Sets entry value to `val1` if current value is `val2` with following conditions:

- If `val1` is `null` and `val2` is equal to current value, entry is removed from cache.
- If `val2` is `null`, entry is created if it doesn't exist.
- If both `val1` and `val2` are `null`, entry is removed.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `cas` in interface `GridClientData`
Parameters:
  `key` - Key to set.
  `val1` - Value to set.
  `val2` - Check value.
Returns:
  Whether value of entry was changed.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### casAsync

    public <K,V> GridClientFuture<Boolean> casAsync(K key,
                                                    V val1,
                                                    V val2)

Asynchronously sets entry value to `val1` if current value is `val2` with following conditions:

- If `val1` is `null` and `val2` is equal to current value, entry is removed from cache.
- If `val2` is `null`, entry is created if it doesn't exist.
- If both `val1` and `val2` are `null`, entry is removed.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `casAsync` in interface `GridClientData`
Parameters:
  `key` - Key to set.
  `val1` - Value to set.
  `val2` - Check value.
Returns:
  Future whether value of entry was changed.

#### affinity

    public <K> UUID affinity(K key)
                      throws GridClientException

Gets affinity node ID for provided key. This method will return `null` if no affinity was configured for the given cache for this client or there are no nodes in topology with cache enabled.

Specified by:
  `affinity` in interface `GridClientData`
Parameters:
  `key` - Key.
Returns:
  Node ID.
Throws:
  `GridClientException` - In case of error.

#### metrics

    public GridClientDataMetrics metrics()
                                  throws GridClientException

Fetches metrics for cache from remote grid.

Specified by:
  `metrics` in interface `GridClientData`
Returns:
  Cache metrics.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### metricsAsync

    public GridClientFuture<GridClientDataMetrics> metricsAsync()

Asynchronously fetches metrics for cache from remote grid.

Specified by:
  `metricsAsync` in interface `GridClientData`
Returns:
  Future with cache metrics.

#### cachedMetrics

    public GridClientDataMetrics cachedMetrics()
                                        throws GridClientException

Tries to get metrics from local cache.

Local cache is updated on every `GridClientData.metrics()` or `GridClientData.metricsAsync()` call if `GridClientConfiguration.isEnableMetricsCache()` is enabled. If it is disabled then this method will always return `null`.

Specified by:
  `cachedMetrics` in interface `GridClientData`
Returns:
  Cached metrics or `null` if no cached metrics available.
Throws:
  `GridClientException` - In case of error.
  `GridServerUnreachableException` - If none of the servers can be reached.
  `GridClientClosedException` - If client was closed manually.

#### append

    public <K,V> boolean append(K key,
                                V val)
                         throws GridClientException

Append requested value to already cached one. This method supports work with strings, lists and maps.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `append` in interface `GridClientData`
Parameters:
  `key` - Key to manipulate cache value for.
  `val` - Value to append to the cached one.
Returns:
  Whether value of entry was changed.
Throws:
  `GridClientException` - In case of error.

#### appendAsync

    public <K,V> GridClientFuture<Boolean> appendAsync(K key,
                                                       V val)
                                                throws GridClientException

Append requested value to already cached one. This method supports work with strings, lists and maps.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `appendAsync` in interface `GridClientData`
Parameters:
  `key` - Key to manipulate cache value for.
  `val` - Value to append to the cached one.
Returns:
  Future whether value of entry was changed.
Throws:
  `GridClientException` - In case of error.

#### prepend

    public <K,V> boolean prepend(K key,
                                 V val)
                          throws GridClientException

Prepend requested value to already cached one. This method supports work with strings, lists and maps.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `prepend` in interface `GridClientData`
Parameters:
  `key` - Key to manipulate cache value for.
  `val` - Value to prepend to the cached one.
Returns:
  Whether value of entry was changed.
Throws:
  `GridClientException` - In case of error.

#### prependAsync

    public <K,V> GridClientFuture<Boolean> prependAsync(K key,
                                                        V val)
                                                 throws GridClientException

Prepend requested value to already cached one. This method supports work with strings, lists and maps.

Note that this operation is affinity-aware and will immediately contact exactly the remote node on which this key is supposed to be cached (unless some nodes were `pinned`).

Specified by:
  `prependAsync` in interface `GridClientData`
Parameters:
  `key` - Key to manipulate cache value for.
  `val` - Value to prepend to the cached one.
Returns:
  Future whether value of entry was changed.
Throws:
  `GridClientException` - In case of error.

#### flags

    public Set<GridClientCacheFlag> flags()

Gets cache flags enabled on this data projection.

Specified by:
  `flags` in interface `GridClientData`
Returns:
  Flags for this data projection (empty set if no flags have been set).

#### flagsOn

    public GridClientData flagsOn(GridClientCacheFlag... flags)
                           throws GridClientException

Creates new client data object with enabled cache flags.

Specified by:
  `flagsOn` in interface `GridClientData`
Parameters:
  `flags` - Optional cache flags to be enabled.
Returns:
  New client data object.
Throws:
  `GridClientException` - In case of error.

#### flagsOff

    public GridClientData flagsOff(GridClientCacheFlag... flags)
                            throws GridClientException

Creates new client data object with disabled cache flags.

Specified by:
  `flagsOff` in interface `GridClientData`
Parameters:
  `flags` - Cache flags to be disabled.
Returns:
  New client data object.
Throws:
  `GridClientException` - In case of error.

#### withReconnectHandling

    protected <R> GridClientFuture<R> withReconnectHandling(GridClientAbstractProjection.ClientProjectionClosure<R> c)

This method executes request to a communication layer and handles connection error, if it occurs. In case of communication exception client instance is notified and new instance of client is created. If none of the grid servers can be reached, an exception is thrown.

Type Parameters:
  `R` - Result future type.
Parameters:
  `c` - Closure to be executed.
Returns:
  Future returned by closure.

#### withReconnectHandling

    protected <R> GridClientFuture<R> withReconnectHandling(GridClientAbstractProjection.ClientProjectionClosure<R> c,
                                                            String cacheName,
                                                            @Nullable
                                                            @Nullable Object affKey)

This method executes request to a communication layer and handles connection error, if it occurs. Server is picked up according to the projection affinity and key given. Connection will be made with the node on which key is cached. In case of communication exception client instance is notified and new instance of client is created. If none of servers can be reached, an exception is thrown.

Type Parameters:
  `R` - Type of result in future.
Parameters:
  `c` - Closure to be executed.
  `cacheName` - Cache name for which mapped node will be calculated.
  `affKey` - Affinity key.
Returns:
  Operation future.

#### checkNodeAlive

    protected boolean checkNodeAlive(UUID nodeId)
                              throws GridClientException,
                                     InterruptedException

Tries to refresh node on every possible connection in topology.

Parameters:
  `nodeId` - Node id to check.
Returns:
  `True` if response was received, `false` if either `null` response received or no nodes can be contacted at all.
Throws:
  `GridClientException` - If failed to refresh topology or if client was closed manually.
  `InterruptedException` - If interrupted.

#### projectionNodes

    public Collection<? extends GridClientNode> projectionNodes()
                                                         throws GridClientException

Gets most recently refreshed topology. If this compute instance is a projection, then only nodes that satisfy projection criteria will be returned.

Returns:
  Most recently refreshed topology.
Throws:
  `GridClientException` - If failed to refresh topology.

#### projectionNodes

    protected Collection<? extends GridClientNode> projectionNodes(@Nullable
                                                                   @Nullable GridClientPredicate<GridClientNode> pred)
                                                            throws GridClientException

Gets most recently refreshed topology. If this compute instance is a projection, then only nodes that satisfy projection criteria will be returned.

Parameters:
  `pred` - Predicate to additionally filter projection nodes, if `null` just return projection.
Returns:
  Most recently refreshed topology.
Throws:
  `GridClientException` - If failed to refresh topology.

#### createProjection

    protected T createProjection(@Nullable
                                 @Nullable Collection<GridClientNode> nodes,
                                 @Nullable
                                 @Nullable GridClientPredicate<? super GridClientNode> filter,
                                 @Nullable
                                 @Nullable GridClientLoadBalancer balancer,
                                 GridClientAbstractProjection.ProjectionFactory<T> factory)
                          throws GridClientException

Creates a sub-projection for current projection.

Parameters:
  `nodes` - Collection of nodes that sub-projection will be restricted to. If `null`, created projection is dynamic and will take nodes from topology.
  `filter` - Filter to be applied to nodes in projection. If `null` - no filter applied.
  `balancer` - Balancer to use in projection. If `null` - inherit balancer from the current projection.
  `factory` - Factory to create new projection.
Returns:
  Created projection.
Throws:
  `GridClientException` - If resulting projection is empty. Note that this exception may only be thrown on case of static projections, i.e. where collection of nodes is not null.

${current.year} Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.9.1
Release Date | : December 9 2020