1.0.1 • Published 8 years ago

hash-ring v1.0.1

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

hash-ring

Build Status

Hash ring implemented in JavaScript. Inspired by http://www.tom-e-white.com//2007/11/consistent-hashing.html


Install

npm install hash-ring

APIs

HashRing

Initialize hash ring instance.

  • nodes {Array}: Nodes
  • options {Object}: Config + hashMethod {String}: Specific hash method, crc32 or md5, default is crc32 + replicas {Int}: Virtual nodes number,. default is 128

Usage

const HashRing = require('HashRing');
let ring = new HashRing([
	'1',
	'2'
], {
	hashMethod: 'crc32',
	replicas: 1
});

addNode

Add a node to hash ring, node should a string.

  • node {String}: Node

Usage

const node = '3';
ring.addNode(node);

removeNode

Remove a node from hash ring, node should a string

  • node {String}: Node

Usage

const node = '1';
ring.removeNode(node);

getNode

Get a node by specific key.

  • key {String}: Query key

Usage

const key = '5';
console.log(ring.getNode(key));

Test

make test