Created
December 17, 2020 14:55
-
-
Save peter/4f54a161d2614fc99c1274318c06fe75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// See: https://medium.com/@promentol/cryptography-for-javascript-node-js-developers-part-1-hash-function-86d119c7304 | |
function hash(data, digest = 'hex') { | |
return require('crypto').createHash('sha256').update(data).digest(digest) | |
} | |
// ('foo', 10) => 6 | |
// ('bar', 10) => 6 | |
// ('zz', 10) => 2 | |
// ('foo', 6) => 2 | |
// ('bar', 6) => 4 | |
// ('foo', 20) => 16 | |
// NOTE: probably better to use https://en.wikipedia.org/wiki/Consistent_hashing | |
function selectPartition(data, nPartitions) { | |
return parseInt(hash(data), 16) % nPartitions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment