导航

一致性Hash算法介绍及简单实现

Posted on 2013-07-10 21:27  酷鱼影子  阅读(196)  评论(0编辑  收藏  举报
一致性 hash 算法( consistent hashing )介绍: 
http://blog.csdn.net/sparkliang/archive/2010/02/02/5279393.aspx 

一致性 hash 算法简单实现: 
hashcode产生接口 
Java代码  收藏代码
  1. package consistentHash;  
  2.   
  3. /** 
  4.  * @author zhengtian 
  5.  *  
  6.  * @date 2012-4-20 下午02:51:39 
  7.  */  
  8. @SuppressWarnings("all")  
  9. public interface HashFunction {  
  10.     public int hash(Object key);  
  11. }  

一致性 hash循环圈 
Java代码  收藏代码
  1. package consistentHash;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.SortedMap;  
  5. import java.util.TreeMap;  
  6.   
  7. /** 
  8.  * @author zhengtian 
  9.  *  
  10.  * @date 2012-4-20 下午02:50:26 
  11.  */  
  12. @SuppressWarnings("all")  
  13. public class ConsistentHash<T> {  
  14.     // hashcode生成接口  
  15.     private final HashFunction hashFunction;  
  16.     // 需要复制的虚拟节点个数  
  17.     private final int numberOfReplicas;  
  18.     // hashcode循环圈  
  19.     private final SortedMap<Integer, T> circle = new TreeMap<Integer, T>();  
  20.   
  21.     /** 
  22.      * 构造函数 
  23.      *  
  24.      * @param hashFunction 
  25.      * @param numberOfReplicas 
  26.      * @param nodes 
  27.      *            真实节点数 
  28.      */  
  29.     public ConsistentHash(HashFunction hashFunction, int numberOfReplicas, Collection<T> nodes) {  
  30.         this.hashFunction = hashFunction;  
  31.         this.numberOfReplicas = numberOfReplicas;  
  32.   
  33.         for (T node : nodes) {  
  34.             add(node);  
  35.         }  
  36.     }  
  37.   
  38.     /** 
  39.      * 增加节点 
  40.      *  
  41.      * @param node 
  42.      */  
  43.     public void add(T node) {  
  44.         for (int i = 0; i < numberOfReplicas; i++) {  
  45.             circle.put(hashFunction.hash(node.toString() + i), node);  
  46.         }  
  47.     }  
  48.   
  49.     /** 
  50.      * 移除节点 
  51.      *  
  52.      * @param node 
  53.      */  
  54.     public void remove(T node) {  
  55.         for (int i = 0; i < numberOfReplicas; i++) {  
  56.             circle.remove(hashFunction.hash(node.toString() + i));  
  57.         }  
  58.     }  
  59.   
  60.     /** 
  61.      * 根据对象的key得到顺时针方向的第一个node 
  62.      *  
  63.      * @param key 
  64.      * @return 
  65.      */  
  66.     public T get(Object key) {  
  67.         if (circle.isEmpty()) {  
  68.             return null;  
  69.         }  
  70.         int hash = hashFunction.hash(key);  
  71.         if (!circle.containsKey(hash)) {  
  72.             // 得到circle中hashcode值大于等于hash的部分映射  
  73.             SortedMap<Integer, T> tailMap = circle.tailMap(hash);  
  74.             hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();  
  75.         }  
  76.         return circle.get(hash);  
  77.     }  
  78.   
  79.     public static void main(String[] args) {  
  80.         SortedMap<Integer, String> tailMap = new TreeMap<Integer, String>();  
  81.         tailMap.put(1"111");  
  82.         tailMap.put(3"333");  
  83.         tailMap.put(4"444");  
  84.         tailMap.put(2"222");  
  85.         System.out.println(tailMap.firstKey());  
  86.         System.out.println(tailMap.get(tailMap.firstKey()));  
  87.     }  
  88. }