-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsistentHash2.java
More file actions
58 lines (55 loc) · 1.53 KB
/
Copy pathConsistentHash2.java
File metadata and controls
58 lines (55 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package leetcode;
import java.util.*;
public class ConsistentHash2 {
// @param n a positive integer
// @param k a positive integer
// @return a Solution object
private static ConsistentHash2 ch2;
private int k;
private int n;
private int[] machines;
public static ConsistentHash2 create(int n, int k) {
if(ch2 == null){
return new ConsistentHash2(n,k);
}
return ConsistentHash2.ch2;
}
public ConsistentHash2(int n, int k) {
this.k = k;
this.n = n;
this.machines = new int[n];
}
// @param machine_id an integer
// @return a list of shard ids
public List<Integer> addMachine(int machine_id) {
// Write your code here
ArrayList<Integer> ret = new ArrayList<Integer>();
int counter = 0;
while(counter < this.k){
int hashcode = (int)(Math.random() * this.n);
if(machines[hashcode] == 0){
ret.add(hashcode);
counter++;
machines[hashcode] = machine_id;
}
}
return ret;
}
// @param hashcode an integer
// @return a machine id
public int getMachineIdByHashCode(int hashcode) {
// Write your code here
for(int i=hashcode; i < n+hashcode; ++i){
if(this.machines[i%this.n] != 0){
return this.machines[i%this.n];
}
}
return -1;
}
public static void main(String[] args) {
ConsistentHash2 ch2 = ConsistentHash2.create(100, 3);
for(int hashcode:ch2.addMachine(1)){
System.out.println(ch2.getMachineIdByHashCode(hashcode));;
}
}
}