Ai
1 Star 0 Fork 0

雨碎江南/dsa4j

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Mod.js 1.00 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Created by yiihua-013 on 16/10/26.
*
* 分布式取余算法
*/
'use strict';
const util = require('util')
var Hash = require('./hash')
function Mod() {
this.nodes = [];
}
util.inherits(Mod, Hash)
// var ret = new Mod().hash('1223')
// console.log(ret)
var fn = Mod.prototype;
fn.lookup = function (key) {
var pos = this.hash(key) % this.nodes.length;
return this.nodes[pos];
};
fn.addNode = function (node) {
if (!~this.nodes.indexOf(node)) this.nodes.push(node);
};
fn.deleteNode = function (node) {
var index = this.nodes.indexOf(node);
if (~index) this.nodes.splice(index, 1);
};
// var mod = new Mod();
// mod.addNode('192.168.1.1')
// mod.addNode('192.168.1.1')
// mod.addNode('192.168.1.2')
// mod.addNode('192.168.1.3')
//
// var key = 'name';
// console.log(`key ${key}的落点为${mod.hash(key)},所在服务器为:${mod.lookup(key)}`)
// key = "sex";
// console.log(`key ${key}的落点为${mod.hash(key)},所在服务器为:${mod.lookup(key)}`)
module.exports = Mod
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/gaopengfei/dsa4j.git
git@gitee.com:gaopengfei/dsa4j.git
gaopengfei
dsa4j
dsa4j
master

搜索帮助