Merge branch '3.0' of https://github.com/calzoneman/sync into 3.0
[KisSync.git] / src / partition / partitiondecider.js
bloba93668f32a9ff7896941958ec593e99bb0d7fadb
1 import { murmurHash1 } from '../util/murmur';
3 class PartitionDecider {
4 constructor(config, partitionMap) {
5 this.config = config;
6 this.partitionMap = partitionMap;
9 getPartitionForChannel(channel) {
10 return this.partitionMap.getPartitions()[this.getPartitionIdentityForChannel(channel)];
13 getPartitionIdentityForChannel(channel) {
14 channel = channel.toLowerCase();
15 const overrideMap = this.partitionMap.getOverrides();
16 if (overrideMap.hasOwnProperty(channel)) {
17 return overrideMap[channel];
18 } else if (this.partitionMap.getPool().length > 0) {
19 const pool = this.partitionMap.getPool();
20 const i = murmurHash1(channel) % pool.length;
21 return pool[i];
22 } else {
23 return { servers: [] };
27 isChannelOnThisPartition(channel) {
28 return this.getPartitionIdentityForChannel(channel) ===
29 this.config.getIdentity();
32 setPartitionMap(newMap) {
33 this.partitionMap = newMap;
37 export { PartitionDecider };