Class GridClientDataImpl

  • All Implemented Interfaces:
    GridClientData

    public class GridClientDataImpl
    extends Object
    implements GridClientData
    Data projection that serves one cache instance and handles communication errors.
    • 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.
      • 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.
      • 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).
      • withReconnectHandling

        protected <R> GridClientFuture<R> withReconnectHandling​(org.apache.ignite.internal.client.impl.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​(org.apache.ignite.internal.client.impl.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,
                                     org.apache.ignite.internal.client.impl.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.