File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change 39
39
class ConsistentHashTable {
40
40
41
41
private $ nodes ;
42
+ private $ ringSize = PHP_INT_MAX ;
42
43
43
44
public function __construct () {
44
- $ this ->nodes = [];
45
+ $ this ->nodes = new HashTable ();
46
+ }
47
+
48
+ public function getNode ($ key ) {
49
+ $ angle = $ this ->getAngle ($ this ->getHash ($ key ));
50
+ $ i = $ angle ;
51
+
52
+ while ($ i >= 0 ) {
53
+
54
+ if ($ this ->nodes ->containsKey ($ i )) {
55
+ return $ this ->nodes ->get ($ i );
56
+ }
57
+ $ i --;
58
+ }
59
+
60
+ return null ;
61
+ }
62
+
63
+ public function addNode ($ key , $ node ) {
64
+ $ angle = $ this ->getAngle ($ this ->getHash ($ key ));
65
+ $ this ->nodes ->put ($ angle , $ node );
66
+ }
67
+
68
+ private function getAngle (int $ hash ): int {
69
+ // (1633428562 / 10^10) * 360
70
+ return ($ hash / $ this ->ringSize ) * 360 ;
45
71
}
46
72
47
73
/**
You can’t perform that action at this time.
0 commit comments