[infinispan-commits] Infinispan SVN: r1420 - trunk/core/src/main/java/org/infinispan/distribution.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu Jan 28 11:29:48 EST 2010


Author: manik.surtani at jboss.com
Date: 2010-01-28 11:29:47 -0500 (Thu, 28 Jan 2010)
New Revision: 1420

Modified:
   trunk/core/src/main/java/org/infinispan/distribution/DefaultConsistentHash.java
Log:
use a bit spreader!

Modified: trunk/core/src/main/java/org/infinispan/distribution/DefaultConsistentHash.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/distribution/DefaultConsistentHash.java	2010-01-28 14:26:12 UTC (rev 1419)
+++ trunk/core/src/main/java/org/infinispan/distribution/DefaultConsistentHash.java	2010-01-28 16:29:47 UTC (rev 1420)
@@ -7,12 +7,13 @@
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import static java.lang.Math.min;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import static java.lang.Math.min;
+
 @Marshallable(externalizer = DefaultConsistentHash.Externalizer.class, id = Ids.DEFAULT_CONSISTENT_HASH)
 public class DefaultConsistentHash extends AbstractConsistentHash {
 
@@ -22,6 +23,17 @@
 
    final static int HASH_SPACE = 10240; // no more than 10k nodes?
 
+   private int hash(Object o) {
+      // Spread bits to regularize both segment and index locations,
+      // using variant of single-word Wang/Jenkins hash.
+      int h = o.hashCode();
+      h += (h << 15) ^ 0xffffcd7d;
+      h ^= (h >>> 10);
+      h += (h << 3);
+      h ^= (h >>> 6);
+      h += (h << 2) + (h << 14);
+      return h ^ (h >>> 16);
+   }
 
    public void setCaches(List<Address> caches) {
 
@@ -33,7 +45,7 @@
       positions = new TreeMap<Integer, Address>();
 
       for (Address a : addresses) {
-         int positionIndex = Math.abs(a.hashCode()) % HASH_SPACE;
+         int positionIndex = Math.abs(hash(a.hashCode())) % HASH_SPACE;
          // this is deterministic since the address list is ordered and the order is consistent across the grid
          while (positions.containsKey(positionIndex)) positionIndex = positionIndex + 1 % HASH_SPACE;
          positions.put(positionIndex, a);
@@ -49,7 +61,7 @@
    }
 
    public List<Address> locate(Object key, int replCount) {
-      int keyHashCode = key.hashCode();
+      int keyHashCode = hash(key.hashCode());
       if (keyHashCode == Integer.MIN_VALUE) keyHashCode += 1;
       int hash = Math.abs(keyHashCode);
       int numCopiesToFind = min(replCount, addresses.size());
@@ -86,7 +98,7 @@
    @Override
    public boolean isKeyLocalToAddress(Address target, Object key, int replCount) {
       // more efficient impl
-      int keyHashCode = key.hashCode();
+      int keyHashCode = hash(key.hashCode());
       if (keyHashCode == Integer.MIN_VALUE) keyHashCode += 1;
       int hash = Math.abs(keyHashCode);
       int numCopiesToFind = min(replCount, addresses.size());
@@ -113,7 +125,7 @@
             }
          }
       }
-      
+
       return false;
    }
 



More information about the infinispan-commits mailing list