Get gets the closest item in the hash to the provided key.
(key string)
| 63 | |
| 64 | // Get gets the closest item in the hash to the provided key. |
| 65 | func (m *Map) Get(key string) string { |
| 66 | if m.IsEmpty() { |
| 67 | return "" |
| 68 | } |
| 69 | |
| 70 | hash := int(m.hash([]byte(key))) |
| 71 | |
| 72 | // Binary search for appropriate replica. |
| 73 | idx := sort.Search(len(m.keys), func(i int) bool { return m.keys[i] >= hash }) |
| 74 | |
| 75 | // Means we have cycled back to the first replica. |
| 76 | if idx == len(m.keys) { |
| 77 | idx = 0 |
| 78 | } |
| 79 | |
| 80 | return m.hashMap[m.keys[idx]] |
| 81 | } |