import * as murmurhash from 'murmurhash' ; import { Allocation } from './Config' ; class Bucketer { static HASH_SEED = 1 ; static MAX_HASH_VALUE = Math . pow ( 2 , 32 ) - 1 ; private maxBuckets : number ; constructor ( maxBuckets ) { this . maxBuckets = maxBuckets ; } private computeBucketId ( id ) { const hashValue = murmurhash . v3 ( id , Bucketer . HASH_SEED ) ; const ratio = hashValue / Bucketer . MAX_HASH_VALUE ; return Math . floor ( ratio * this . maxBuckets ) ; } bucket ( key , allocations ) { const bucketId = this . computeBucketId ( key ) ; const allocation = allocations . find ( allocation => bucketId < allocation . rangeEnd ) ; if ( allocation ) { return allocation . id ; } return null ; } } export default Bucketer ;