mirror of
https://github.com/Spengreb/sync.git
synced 2026-05-14 03:32:06 +00:00
29 lines
855 B
JavaScript
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 };
|