sync/src/partition/partitiondecider.js
2016-06-06 21:54:49 -07:00

29 lines
855 B
JavaScript

import { murmurHash1 } from '../util/murmur';
class PartitionDecider {
constructor(config) {
this.identity = config.getIdentity();
this.partitionMap = config.getPartitionMap();
this.pool = config.getPool();
this.overrideMap = config.getOverrideMap();
}
getPartitionForChannel(channel) {
return this.partitionMap[this.getPartitionIdentityForChannel(channel)];
}
getPartitionIdentityForChannel(channel) {
if (this.overrideMap.hasOwnProperty(channel)) {
return this.overrideMap[channel];
} else {
const i = murmurHash1(channel) % this.pool.length;
return this.pool[i];
}
}
isChannelOnThisPartition(channel) {
return this.getPartitionIdentityForChannel(channel) === this.identity;
}
}
export { PartitionDecider };