Skip to content

Commit

Permalink
Updated to use the external bisection module.
Browse files Browse the repository at this point in the history
Added test cases, updated documentation.
  • Loading branch information
3rd-Eden committed Apr 22, 2011
1 parent 3923a4d commit 0eed6f3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 67 deletions.
5 changes: 4 additions & 1 deletion Makefile
@@ -1,4 +1,7 @@
doc:
dox --title "node-hashring" lib/* > doc/index.html

.PHONY: doc
test:
expresso -I lib $(TESTFLAGS) tests/*.test.js

.PHONY: test doc
36 changes: 2 additions & 34 deletions doc/index.html
Expand Up @@ -108,47 +108,15 @@
</script>
</head>
<body>
<table id="source"><tbody><tr><td><h1>node-hashring</h1></td><td></td></tr><tr class="filename"><td><h2 id="lib/bisection.js"><a href="#">bisection</a></h2></td><td>lib/bisection.js</td></tr><tr class="code">
<td class="docs">
<p>Calculates the index of the Array where item X should be placed, assuming the Array is sorted.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>Array</em> array The array containing the items.</p></li><li><p><strong>param</strong>: <em>Number</em> x The item that needs to be added to the array.</p></li><li><p><strong>param</strong>: <em>Number</em> low Inital Index that is used to start searching, optional.</p></li><li><p><strong>param</strong>: <em>Number</em> high The maximum Index that is used to stop searching, optional.</p></li><li><p><strong>returns</strong>: <em>Number</em> the index where item X should be placed</p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Bisection</span> = <span class="keyword">function</span> <span class="variable">bisection</span>(<span class="variable">array</span>, <span class="variable">x</span>, <span class="variable">low</span>, <span class="variable">high</span>){
<span class="comment">// The low and high bounds the inital slice of the array that needs to be searched</span>
<span class="comment">// this is optional</span>
<span class="variable">low</span> = <span class="variable">low</span> || <span class="number integer">0</span>;
<span class="variable">high</span> = <span class="variable">high</span> || <span class="variable">array</span>.<span class="variable">length</span>;

<span class="keyword">var</span> <span class="variable">mid</span>;

<span class="keyword">if</span> (<span class="variable">low</span> &<span class="variable">lt</span>; <span class="number integer">0</span>) <span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'Low must be a non-negative integer'</span>);

<span class="keyword">while</span>(<span class="variable">low</span> &<span class="variable">lt</span>; <span class="variable">high</span>){
<span class="variable">mid</span> = <span class="class">Math</span>.<span class="variable">floor</span>((<span class="variable">low</span> + <span class="variable">high</span>) / <span class="number integer">2</span>);

<span class="keyword">if</span> (<span class="variable">x</span> &<span class="variable">lt</span>; <span class="variable">array</span>[<span class="variable">mid</span>]){
<span class="variable">high</span> = <span class="variable">mid</span>;
} <span class="keyword">else</span> {
<span class="variable">low</span> = <span class="variable">mid</span> + <span class="number integer">1</span>;
}
}

<span class="keyword">return</span> <span class="variable">low</span>;
};</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/hashring.js"><a href="#">hashring</a></h2></td><td>lib/hashring.js</td></tr><tr class="code">
<table id="source"><tbody><tr><td><h1>node-hashring</h1></td><td></td></tr><tr class="filename"><td><h2 id="lib/hashring.js"><a href="#">hashring</a></h2></td><td>lib/hashring.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">CreateHash</span> = <span class="variable">require</span>(<span class="string">'crypto'</span>).<span class="variable">createHash</span>
, <span class="class">StringDecoder</span> = <span class="variable">require</span>(<span class="string">'string_decoder'</span>).<span class="class">StringDecoder</span>
, <span class="class">Bisection</span> = <span class="variable">require</span>(<span class="string">'./bisection'</span>).<span class="class">Bisection</span>;</code></pre>
, <span class="class">Bisection</span> = <span class="variable">require</span>(<span class="string">'bisection'</span>);</code></pre>
</td>
</tr>
<tr class="code">
Expand Down
31 changes: 0 additions & 31 deletions lib/bisection.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/hashring.js
Expand Up @@ -3,7 +3,7 @@
*/
var CreateHash = require('crypto').createHash
, StringDecoder = require('string_decoder').StringDecoder
, Bisection = require('./bisection').Bisection;
, Bisection = require('bisection');

/**
* Creates a hashring for key => server lookups. It uses `crc32` as default algorithm
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -28,4 +28,7 @@
"type": "git"
, "url" : "http://github.com/3rd-Eden/node-hashring.git"
}]
, "dependencies": {
"bisection": ""
}
}
41 changes: 41 additions & 0 deletions tests/hashring.test.js
@@ -0,0 +1,41 @@
var should = require('should')
, hashring = require('../lib/hashring');

module.exports = {
'Library version': function(){
hashring.version.should.match(/^\d+\.\d+\.\d+$/);
}

, 'Constructing with a string': function(){
var ring = new hashring('192.168.0.102:11212');

ring.nodes.should.have.length(1);
ring.sortedKeys.length.should.be.above(1);
Object.keys(ring.weights).should.have.length(0);
}

, 'Constructing with a array': function(){
var ring = new hashring(['192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212']);

ring.nodes.should.have.length(3);
ring.sortedKeys.length.should.be.above(1);
Object.keys(ring.weights).should.have.length(0);
}

, 'Constructing with a object': function(){
var ring = new hashring({'192.168.0.102:11212': 1, '192.168.0.103:11212': 2, '192.168.0.104:11212': 1});

ring.nodes.should.have.length(3);
ring.sortedKeys.length.should.be.above(1);
Object.keys(ring.weights).should.have.length(3);
}

, 'Constructing with a different algorithm': function(){
var ring = new hashring('192.168.0.102:11212', 'md5');

ring.nodes.should.have.length(1);
ring.algorithm.should.equal('md5');
ring.sortedKeys.length.should.be.above(1);
Object.keys(ring.weights).should.have.length(0);
}
};

0 comments on commit 0eed6f3

Please sign in to comment.