org.infinispan.distribution.ch
Class DefaultConsistentHash

java.lang.Object
  extended by org.infinispan.distribution.ch.DefaultConsistentHash
All Implemented Interfaces:
ConsistentHash

@Immutable
public class DefaultConsistentHash
extends Object
implements ConsistentHash

Default ConsistentHash implementation. This object is immutable.

Since:
5.2
Author:
Dan Berindei, anistor@redhat.com

Nested Class Summary
static class DefaultConsistentHash.Externalizer
           
 
Constructor Summary
DefaultConsistentHash(Hash hashFunction, int numOwners, int numSegments, List<Address> members, List<Address>[] segmentOwners)
           
 
Method Summary
 boolean equals(Object o)
           
 Hash getHashFunction()
           
 List<Address> getMembers()
          Should return the addresses of the nodes used to create this consistent hash.
 int getNormalizedHash(Object key)
           
 int getNumOwners()
           
 int getNumSegments()
           
 String getRoutingTableAsString()
          Returns a string containing all the segments and their associated addresses.
 int getSegment(Object key)
           
 List<Integer> getSegmentEndHashes()
           
 Set<Integer> getSegmentsForOwner(Address owner)
          Returns the segments owned by a cache member.
 int hashCode()
           
 boolean isKeyLocalToNode(Address nodeAddress, Object key)
          Test to see whether a key is owned by a given node.
 Set<Address> locateAllOwners(Collection<Object> keys)
          The logical equivalent of calling ConsistentHash.locateOwners(java.lang.Object) multiple times for each key in the collection of keys and merging the results.
 List<Address> locateOwners(Object key)
          Finds all the owners of a key.
 List<Address> locateOwnersForSegment(int segmentId)
           
 Address locatePrimaryOwner(Object key)
          Should be equivalent to return the first element of ConsistentHash.locateOwners(java.lang.Object).
 Address locatePrimaryOwnerForSegment(int segmentId)
           
 String toString()
           
 DefaultConsistentHash union(DefaultConsistentHash dch2)
          Merges two consistent hash objects that have the same number of segments, numOwners and hash function.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultConsistentHash

public DefaultConsistentHash(Hash hashFunction,
                             int numOwners,
                             int numSegments,
                             List<Address> members,
                             List<Address>[] segmentOwners)
Method Detail

getHashFunction

public Hash getHashFunction()
Specified by:
getHashFunction in interface ConsistentHash

getNumSegments

public int getNumSegments()
Specified by:
getNumSegments in interface ConsistentHash
Returns:
The actual number of hash space segments. Note that it may not be the same as the number of segments passed in at creation time.

getSegmentsForOwner

public Set<Integer> getSegmentsForOwner(Address owner)
Description copied from interface: ConsistentHash
Returns the segments owned by a cache member.

Specified by:
getSegmentsForOwner in interface ConsistentHash
Parameters:
owner - the address of the member
Returns:
a non-nul set of segment IDs

getSegment

public int getSegment(Object key)
Specified by:
getSegment in interface ConsistentHash
Returns:
The hash space segment that a key maps to.

getNormalizedHash

public int getNormalizedHash(Object key)

getSegmentEndHashes

public List<Integer> getSegmentEndHashes()

locateOwnersForSegment

public List<Address> locateOwnersForSegment(int segmentId)
Specified by:
locateOwnersForSegment in interface ConsistentHash
Returns:
All the nodes that own a given hash space segment, first address is the primary owner. The returned list is unmodifiable.

locatePrimaryOwnerForSegment

public Address locatePrimaryOwnerForSegment(int segmentId)
Specified by:
locatePrimaryOwnerForSegment in interface ConsistentHash
Returns:
The primary owner of a given hash space segment. This is equivalent to locateOwnersForSegment(segmentId).get(0) but is more efficient

getMembers

public List<Address> getMembers()
Description copied from interface: ConsistentHash
Should return the addresses of the nodes used to create this consistent hash.

Specified by:
getMembers in interface ConsistentHash
Returns:
set of node addresses.

getNumOwners

public int getNumOwners()
Specified by:
getNumOwners in interface ConsistentHash
Returns:
The configured number of owners for each key. Note that {code @getOwners(key)} may return a different number of owners.

locatePrimaryOwner

public Address locatePrimaryOwner(Object key)
Description copied from interface: ConsistentHash
Should be equivalent to return the first element of ConsistentHash.locateOwners(java.lang.Object). Useful as a performance optimization, as this is a frequently needed information.

Specified by:
locatePrimaryOwner in interface ConsistentHash
Parameters:
key - key to locate
Returns:
the address of the owner

locateOwners

public List<Address> locateOwners(Object key)
Description copied from interface: ConsistentHash
Finds all the owners of a key. The first element in the returned list is the primary owner.

Specified by:
locateOwners in interface ConsistentHash
Parameters:
key - key to locate
Returns:
An unmodifiable list of addresses where the key resides. Will never be null, and it will always have at least 1 element.

locateAllOwners

public Set<Address> locateAllOwners(Collection<Object> keys)
Description copied from interface: ConsistentHash
The logical equivalent of calling ConsistentHash.locateOwners(java.lang.Object) multiple times for each key in the collection of keys and merging the results. Implementations may be optimised for such a bulk lookup.

Specified by:
locateAllOwners in interface ConsistentHash
Parameters:
keys - keys to locate.
Returns:
set of nodes that own at least one of the keys.

isKeyLocalToNode

public boolean isKeyLocalToNode(Address nodeAddress,
                                Object key)
Description copied from interface: ConsistentHash
Test to see whether a key is owned by a given node.

Specified by:
isKeyLocalToNode in interface ConsistentHash
Parameters:
nodeAddress - address of the node to test
key - key to test
Returns:
true if the key is mapped to the address; false otherwise

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object o)
Overrides:
equals in class Object

toString

public String toString()
Overrides:
toString in class Object

getRoutingTableAsString

public String getRoutingTableAsString()
Description copied from interface: ConsistentHash
Returns a string containing all the segments and their associated addresses.

Specified by:
getRoutingTableAsString in interface ConsistentHash

union

public DefaultConsistentHash union(DefaultConsistentHash dch2)
Merges two consistent hash objects that have the same number of segments, numOwners and hash function. For each segment, the primary owner of the first CH has priority, the other primary owners become backups.


-->

Copyright © 2013 JBoss, a division of Red Hat. All Rights Reserved.