Skip to content

Commit

Permalink
Merge pull request #22 from tobek/master
Browse files Browse the repository at this point in the history
Adding 'has' function to check for existence of a server in the ring
  • Loading branch information
3rd-Eden committed Aug 6, 2014
2 parents 95d41bb + a3f8512 commit 852d35c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ Remove a server from the hash ring.

---

#### HashRing.has(**server**)

Checks if a given server exists in the hash ring.

- **server** Server for whose existence we're checking.

---

#### HashRing.reset()

Reset the HashRing and clean up it's references.
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ HashRing.prototype.remove = function remove(server) {
return this.continuum();
};

/**
* Checks if a given server exists in the hash ring.
*
* @param {String} server Server for whose existence we're checking
* @api public
*/
HashRing.prototype.has = function add(server) {
for (var i = 0; i < this.ring.length; i++) {
if (this.ring[i].server === server) return true;
}
return false;
};

/**
* Reset the HashRing to clean up all references
*
Expand Down
22 changes: 22 additions & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ describe('HashRing', function () {
});
});

describe('#has', function () {
it('has a server', function () {
var ring = new Hashring([
'192.168.0.102:11212'
, '192.168.0.103:11212'
, '192.168.0.104:11212'
]);

ring.has('192.168.0.102:11212').should.equal(true);
});

it('does not have a server', function () {
var ring = new Hashring([
'192.168.0.102:11212'
, '192.168.0.103:11212'
, '192.168.0.104:11212'
]);

ring.has('192.168.0.105:11212').should.equal(false);
});
});

describe('#range', function () {
it('returns 20 servers', function () {
var ring = new Hashring([
Expand Down

0 comments on commit 852d35c

Please sign in to comment.