Skip to content

Instantly share code, notes, and snippets.

@freshtonic
Created August 2, 2010 13:14
Show Gist options
  • Save freshtonic/504629 to your computer and use it in GitHub Desktop.
Save freshtonic/504629 to your computer and use it in GitHub Desktop.
# The Ruby version
VBUCKET_COUNT = 4096
def keyToVBucket(key)
b = 27183
a = 31415
vbucket = 0
(0...key.length).each do |i|
vbucket = (a * vbucket + key[i]) % VBUCKET_COUNT
a = ((a % VBUCKET_COUNT) * (b % VBUCKET_COUNT)) % VBUCKET_COUNT
end
vbucket
end
# The CoffeeScript version
VBUCKET_COUNT: 4096
keyToVBucket: (key) ->
b: 27183
a: 31415
vbucket: 0
for i in [0...key.length]
vbucket: (a * vbucket + key[i].charCodeAt()) % VBUCKET_COUNT
a: ((a % VBUCKET_COUNT) * (b % VBUCKET_COUNT)) % VBUCKET_COUNT
vbucket
# some sample data (each line is a key, and should hash to the same vbucket in both implementations)
hnda884rmgff8w8m4
62dldpvrp0lf7ox2b
y6fj1g2kusa1gwnqp
0s1787s216oqqc9oi
fuqv9nlays7a827tw
lu9j9b8t54rcif2lo
xsq22bcoxu89xvnss
3jxojo76vamt5g6ob
cklrclbwqwv6lpis2
qk6slltxbcrfw9efi
xushlx4sqkyt19hfo
6xgjqm56fi2q9y827
bxn5edn3gbdc5r5fx
i8w1vftjnk2sxis4j
brh2hxjm9q7hxydhs
al3h3f9ix05xgoqia
xdbfmg9fdeors6oej
2pgscyx349ei1pspv
ip38uef9x17wk51s5
2vnx6oxi1t09k4ffn
k3xa5yq7mw66qoj5o
h8pu5q6dsiq2xg7xa
3bajwfuvdw76klkaj
5dobp9mhd46bqmixt
b8c1eq42dealehwo5
7xqsretixsogdv6qy
bhc96bs72iqdr074i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment