Show / Hide Table of Contents

# Interface ICacheAffinity

Provides affinity information to detect which node is primary and which nodes are backups for a partitioned cache. You can get an instance of this interface by calling GetAffinity(String) method.

Mapping of a key to a node is a three-step operation. First step will get an affinity key for given key using `CacheAffinityKeyMapper`. If mapper is not specified, the original key will be used. Second step will map affinity key to partition using `CacheAffinityFunction.partition(Object)` method. Third step will map obtained partition to nodes for current grid topology version.

Interface provides various `mapKeysToNodes(...)` methods which provide node affinity mapping for given keys. All `mapKeysToNodes(...)` methods are not transactional and will not enlist keys into ongoing transaction.

All members are thread-safe and may be used concurrently from multiple threads.

###### Namespace: Apache.Ignite.Core.Cache

###### Assembly: Apache.Ignite.Core.dll

##### Syntax

    public interface ICacheAffinity

### Properties

#### Partitions

Gets number of partitions in cache according to configured affinity function.

##### Declaration

    int Partitions { get; }

##### Property Value

Type | Description
--- | ---
System.Int32 | Number of cache partitions.

### Methods

#### GetAffinityKey<TK, TR>(TK)

Maps passed in key to a key which will be used for node affinity.

##### Declaration

    TR GetAffinityKey<TK, TR>(TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
TK | key | Key to map.

##### Returns

Type | Description
--- | ---
TR | Key to be used for node-to-affinity mapping (may be the same key as passed in).

##### Type Parameters

Name | Description
--- | ---
TK |
TR |

#### GetAllPartitions(IClusterNode)

Gets partition ids for which nodes of the given projection has ownership (either primary or backup).

##### Declaration

    int[] GetAllPartitions(IClusterNode n)

##### Parameters

Type | Name | Description
--- | --- | ---
IClusterNode | n | Node.

##### Returns

Type | Description
--- | ---
System.Int32[] | Partition ids for which given projection has ownership.

#### GetBackupPartitions(IClusterNode)

Gets partition ids for which nodes of the given projection has backup ownership.

##### Declaration

    int[] GetBackupPartitions(IClusterNode n)

##### Parameters

Type | Name | Description
--- | --- | ---
IClusterNode | n | Node.

##### Returns

Type | Description
--- | ---
System.Int32[] | Partition ids for which given projection has backup ownership.

#### GetPartition<TK>(TK)

Gets partition id for the given key.

##### Declaration

    int GetPartition<TK>(TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
TK | key | Key to get partition id for.

##### Returns

Type | Description
--- | ---
System.Int32 | Partition id.

##### Type Parameters

Name | Description
--- | ---
TK |

#### GetPrimaryPartitions(IClusterNode)

Gets partition ids for which nodes of the given projection has primary ownership.

##### Declaration

    int[] GetPrimaryPartitions(IClusterNode n)

##### Parameters

Type | Name | Description
--- | --- | ---
IClusterNode | n | Node.

##### Returns

Type | Description
--- | ---
System.Int32[] | Partition ids for which given projection has primary ownership.

#### IsBackup<TK>(IClusterNode, TK)

Returns 'true' if given node is the backup node for given key.

##### Declaration

    bool IsBackup<TK>(IClusterNode n, TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
IClusterNode | n | Node.
TK | key | Key.

##### Returns

Type | Description
--- | ---
System.Boolean | 'True' if given node is the backup node for given key.

##### Type Parameters

Name | Description
--- | ---
TK |

#### IsPrimary<TK>(IClusterNode, TK)

Returns 'true' if given node is the primary node for given key.

##### Declaration

    bool IsPrimary<TK>(IClusterNode n, TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
IClusterNode | n | Node.
TK | key | Key.

##### Returns

Type | Description
--- | ---
System.Boolean | 'True' if given node is the primary node for given key.

##### Type Parameters

Name | Description
--- | ---
TK |

#### IsPrimaryOrBackup<TK>(IClusterNode, TK)

Returns 'true' if given node is either primary or backup node for given key.

##### Declaration

    bool IsPrimaryOrBackup<TK>(IClusterNode n, TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
IClusterNode | n | Node.
TK | key | Key.

##### Returns

Type | Description
--- | ---
System.Boolean | 'True' if given node is either primary or backup node for given key.

##### Type Parameters

Name | Description
--- | ---
TK |

#### MapKeysToNodes<TK>(IEnumerable<TK>)

This method provides ability to detect which keys are mapped to which nodes. Use it to determine which nodes are storing which keys prior to sending jobs that access these keys.

##### Declaration

    IDictionary<IClusterNode, IList<TK>> MapKeysToNodes<TK>(IEnumerable<TK> keys)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Collections.Generic.IEnumerable<TK> | keys | Keys to map to nodes.

##### Returns

Type | Description
--- | ---
System.Collections.Generic.IDictionary<IClusterNode, System.Collections.Generic.IList<TK>> | Map of nodes to keys or empty map if there are no alive nodes for this cache.

##### Type Parameters

Name | Description
--- | ---
TK |

#### MapKeyToNode<TK>(TK)

This method provides ability to detect to which primary node the given key is mapped. Use it to determine which nodes are storing which keys prior to sending jobs that access these keys.

##### Declaration

    IClusterNode MapKeyToNode<TK>(TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
TK | key | Key to map to a node.

##### Returns

Type | Description
--- | ---
IClusterNode | Primary node for the key or null if there are no alive nodes for this cache.

##### Type Parameters

Name | Description
--- | ---
TK |

#### MapKeyToPrimaryAndBackups<TK>(TK)

Gets primary and backup nodes for the key. Note that primary node is always first in the returned collection.

##### Declaration

    IList<IClusterNode> MapKeyToPrimaryAndBackups<TK>(TK key)

##### Parameters

Type | Name | Description
--- | --- | ---
TK | key |

##### Returns

Type | Description
--- | ---
System.Collections.Generic.IList<IClusterNode> |

##### Type Parameters

Name | Description
--- | ---
TK |

#### MapPartitionsToNodes(IEnumerable<Int32>)

Gets primary nodes for the given partitions.

##### Declaration

    IDictionary<int, IClusterNode> MapPartitionsToNodes(IEnumerable<int> parts)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Collections.Generic.IEnumerable<System.Int32> | parts | Partition ids.

##### Returns

Type | Description
--- | ---
System.Collections.Generic.IDictionary<System.Int32, IClusterNode> | Mapping of given partitions to their primary nodes.

#### MapPartitionToNode(Int32)

Gets primary node for the given partition.

##### Declaration

    IClusterNode MapPartitionToNode(int part)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Int32 | part | Partition id.

##### Returns

Type | Description
--- | ---
IClusterNode | Primary node for the given partition.

#### MapPartitionToPrimaryAndBackups(Int32)

Gets primary and backup nodes for partition. Note that primary node is always first in the returned collection.

##### Declaration

    IList<IClusterNode> MapPartitionToPrimaryAndBackups(int part)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Int32 | part | Partition to get affinity nodes for.

##### Returns

Type | Description
--- | ---
System.Collections.Generic.IList<IClusterNode> | Collection of primary and backup nodes for partition with primary node always first