forked from OneOfOne/go-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a JumpConsistentHash implementation
- Loading branch information
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package hash | ||
|
||
const prime = 2862933555777941757 | ||
|
||
// JumpConsistentHash a fast, minimal memory, consistent hash algorithm based on the | ||
// [paper](http://arxiv.org/pdf/1406.2294v1.pdf) by John Lamping and Eric Veach. | ||
func JumpConsistentHash(key uint64, buckets int) (b int) { | ||
j := 0 | ||
for j < buckets { | ||
b = j | ||
key = key*prime + 1 | ||
j = int(float64(b+1) * (float64(1<<31) / float64(key>>33+1))) | ||
} | ||
return b | ||
} |