memchched 분포 식 일치 성 알고리즘 구현

1426 단어 자바알고리즘
더 읽 기


import java.util.Collection;
import java.util.SortedMap;
import java.util.TreeMap;
public class ConsistentHash {
	private final HashFunction hashFunction;
	private final int numberOfReplicas; 
	private final SortedMap circle = new TreeMap();
	public ConsistentHash(HashFunction hashFunction, int numberOfReplicas,     Collection nodes) {
		this.hashFunction = hashFunction;  
		this.numberOfReplicas = numberOfReplicas; 
		for (T node : nodes) {  
			add(node);  
			} 
			} 
	public void add(T node) { 
		for (int i = 0; i < numberOfReplicas; i++) { 
			circle.put(hashFunction.hash(node.toString() + i), node); 
			} 
			}
	public void remove(T node) {   
		for (int i = 0; i < numberOfReplicas; i++) { 
			circle.remove(hashFunction.hash(node.toString() + i));  
			}
			}
	public T get(Object key) { 
		if (circle.isEmpty()) { 
			return null;  
			}  
			int hash = hashFunction.hash(key);  
			if (!circle.containsKey(hash)) {   
				SortedMap tailMap = circle.tailMap(hash);  
				hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();  
				} 
				return circle.get(hash);
				}
}

좋은 웹페이지 즐겨찾기