Permalink
Browse files

Updated to use the external bisection module.

Added test cases, updated documentation.
  • Loading branch information...
1 parent 3923a4d commit 0eed6f3527c1445625c644e93b4eaca8856d2e1f @3rd-Eden committed Apr 22, 2011
Showing with 51 additions and 67 deletions.
  1. +4 −1 Makefile
  2. +2 −34 doc/index.html
  3. +0 −31 lib/bisection.js
  4. +1 −1 lib/hashring.js
  5. +3 −0 package.json
  6. +41 −0 tests/hashring.test.js
View
@@ -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
View
@@ -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">
View
@@ -1,31 +0,0 @@
-/**
- * Calculates the index of the Array where item X should be placed, assuming the Array is sorted.
- *
- * @param {Array} array The array containing the items.
- * @param {Number} x The item that needs to be added to the array.
- * @param {Number} low Inital Index that is used to start searching, optional.
- * @param {Number} high The maximum Index that is used to stop searching, optional.
- * @returns {Number} the index where item X should be placed
- */
-exports.Bisection = function bisection(array, x, low, high){
- // The low and high bounds the inital slice of the array that needs to be searched
- // this is optional
- low || (low = 0);
- high || (high = array.length);
-
- var mid;
-
- if (low < 0) throw new Error('Low must be a non-negative integer');
-
- while(low < high){
- mid = (low + high) >> 1;
-
- if (x < array[mid]){
- high = mid;
- } else {
- low = mid + 1;
- }
- }
-
- return low;
-};
View
@@ -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
View
@@ -28,4 +28,7 @@
"type": "git"
, "url" : "http://github.com/3rd-Eden/node-hashring.git"
}]
+, "dependencies": {
+ "bisection": ""
+ }
}
View
@@ -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.