Skip to content
Browse files

Updated the testcase, removed some object.keys lookups

  • Loading branch information...
1 parent 0eed6f3 commit b6c9a4569c58bc909bcaa12441a8f4dfca09dcb2 @3rd-Eden 3rd-Eden committed Apr 22, 2011
Showing with 35 additions and 419 deletions.
  1. +0 −417 doc/index.html
  2. +2 −2 lib/hashring.js
  3. +33 −0 tests/hashring.test.js
View
417 doc/index.html
@@ -1,417 +0,0 @@
-<html>
- <head>
- <title>node-hashring</title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
- <style>body {
- margin: 0;
- padding: 0;
- font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
- color: #252519;
-}
-a {
- color: #252519;
-}
-a:hover {
- text-decoration: underline;
- color: #19469D;
-}
-p {
- margin: 12px 0;
-}
-h1, h2, h3 {
- margin: 0;
- padding: 0;
-}
-table#source {
- width: 100%;
- border-collapse: collapse;
-}
-table#source td:first-child {
- padding: 30px 40px 30px 40px;
- vertical-align: top;
-}
-table#source td:first-child,
-table#source td:first-child pre {
- width: 450px;
-}
-table#source td:last-child {
- padding: 30px 0 30px 40px;
- border-left: 1px solid #E5E5EE;
- background: #F5F5FF;
-}
-table#source tr {
- border-bottom: 1px solid #E5E5EE;
-}
-table#source tr.filename {
- padding-top: 40px;
- border-top: 1px solid #E5E5EE;
-}
-table#source tr.filename td:first-child {
- text-transform: capitalize;
-}
-table#source tr.filename td:last-child {
- font-size: 12px;
-}
-table#source tr.filename h2 {
- margin: 0;
- padding: 0;
- cursor: pointer;
-}
-table#source tr.code h1,
-table#source tr.code h2,
-table#source tr.code h3 {
- margin-top: 30px;
- font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
- font-size: 18px;
-}
-table#source tr.code h2 {
- font-size: 16px;
-}
-table#source tr.code h3 {
- font-size: 14px;
-}
-table#source tr.code ul {
- margin: 15px 0 15px 35px;
- padding: 0;
-}
-table#source tr.code ul li {
- margin: 0;
- padding: 1px 0;
-}
-table#source tr.code ul li p {
- margin: 0;
- padding: 0;
-}
-table#source tr.code td:first-child pre {
- padding: 20px;
-}
-#ribbon {
- position: fixed;
- top: 0;
- right: 0;
-}
-code .string { color: #219161; }
-code .regexp { color: #219161; }
-code .keyword { color: #954121; }
-code .number { color: #19469D; }
-code .comment { color: #bbb; }
-code .this { color: #19469D; }</style>
- <script>
- $(function(){
- $('tr.code').hide();
- $('tr.filename').toggle(function(){
- $(this).nextUntil('.filename').fadeIn();
- }, function(){
- $(this).nextUntil('.filename').fadeOut();
- });
- });
- </script>
- </head>
- <body>
-<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>);</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Creates a hashring for key =&gt; server lookups. It uses <code>crc32</code> as default algorithm
-as it creates nice dense key / server distribution. A other algorithm that could be used
-is <code>MD5</code> or <code>SHA1</code>. But take in account that the hashing can take allot time when choosing
-the algorithm. The JavaScript based <code>crc32</code> algorithm beats <code>MD5</code> in performance.</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>mixed</em> arg The server / weights / servers for the hashring</p></li><li><p><strong>param</strong>: <em>String</em> [algorithm] The algorithm that should be used to hash the keys.</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="keyword">function</span> <span class="variable">hashRing</span>(<span class="variable">args</span>, <span class="variable">algorithm</span>){
-
- <span class="keyword">var</span> <span class="variable">nodes</span> = []
- , <span class="variable">weights</span> = {};
-
- <span class="keyword">switch</span> (<span class="class">Object</span>.<span class="variable">prototype</span>.<span class="variable">toString</span>.<span class="variable">call</span>(<span class="variable">args</span>)){
- <span class="keyword">case</span> <span class="string">'[object String]'</span>:
- <span class="variable">nodes</span>.<span class="variable">push</span>(<span class="variable">args</span>);
- <span class="keyword">break</span>;
- <span class="keyword">case</span> <span class="string">'[object Object]'</span>:
- <span class="variable">weights</span> = <span class="variable">args</span>;
- <span class="variable">nodes</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">args</span>)
- <span class="keyword">break</span>;
- <span class="keyword">case</span> <span class="string">'[object Array]'</span>:
- <span class="keyword">default</span>:
- <span class="variable">nodes</span> = <span class="variable">args</span>;
- <span class="keyword">break</span>;
- }
-
- <span class="this">this</span>.<span class="variable">ring</span> = {};
- <span class="this">this</span>.<span class="variable">cache</span> = {};
- <span class="this">this</span>.<span class="variable">sortedKeys</span> = [];
-
- <span class="this">this</span>.<span class="variable">nodes</span> = <span class="variable">nodes</span>;
- <span class="this">this</span>.<span class="variable">weights</span> = <span class="variable">weights</span>;
- <span class="this">this</span>.<span class="variable">algorithm</span> = <span class="variable">algorithm</span> || <span class="string">'crc32'</span>;
-
- <span class="comment">// overwrite the hashKey method if crc32 is chosen</span>
- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">algorithm</span> === <span class="string">'crc32'</span>){
- <span class="this">this</span>.<span class="variable">hashKey</span> = <span class="this">this</span>.<span class="variable">crc32HashKey</span>;
- }
-
- <span class="this">this</span>.<span class="variable">generateRing</span>();
-};
-
-<span class="keyword">var</span> <span class="class">HashRing</span> = <span class="variable">hashRing</span>.<span class="variable">prototype</span>;</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>returns the correct node for the key based on the hashing, or false if it fails to get
-the node</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> key return</p></li><li><p><strong>returns</strong>: <em>String | Boolean</em> False if there isn't a match, string of the server ip when we have a match</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">getNode</span> = <span class="keyword">function</span>(<span class="variable">key</span>){
- <span class="comment">// faster loookups</span>
- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">cache</span>[<span class="variable">key</span>]) <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">cache</span>[<span class="variable">key</span>];
-
- <span class="keyword">var</span> <span class="variable">position</span> = <span class="this">this</span>.<span class="variable">getNodePosition</span>(<span class="variable">key</span>)
-
- <span class="comment">// needs explicit checking for `false` as we can have position of 0</span>
- , <span class="variable">node</span> = <span class="variable">position</span> === <span class="variable">false</span> ? <span class="variable">false</span> : <span class="this">this</span>.<span class="variable">ring</span>[<span class="this">this</span>.<span class="variable">sortedKeys</span>[<span class="variable">position</span>]];
-
- <span class="keyword">if</span> (!<span class="variable">node</span>) <span class="keyword">return</span> <span class="variable">false</span>;
- <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">cache</span>[<span class="variable">key</span>] = <span class="variable">node</span>;
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Returns the position of the key inside the keyring</p>
-
-<ul><li><p><strong>todo</strong>: <em>Remove</em>
-the Object.keys lookup, or just cache the <code>keys</code> on regeneration.
-## </p></li><li><p><strong>param</strong>: <em>String</em> key The key that needs his it's position calculated.</p></li><li><p><strong>returns</strong>: <em>Number</em> the index of sortedKeys</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">getNodePosition</span> = <span class="keyword">function</span>(<span class="variable">key</span>){
- <span class="keyword">if</span> (!<span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">ring</span>).<span class="variable">length</span>)
- <span class="keyword">return</span> <span class="variable">false</span>;
-
- <span class="keyword">var</span> <span class="variable">keys</span> = <span class="this">this</span>.<span class="variable">generateKey</span>(<span class="variable">key</span>)
- , <span class="variable">nodes</span> = <span class="this">this</span>.<span class="variable">sortedKeys</span>
- , <span class="variable">position</span> = <span class="class">Bisection</span>(<span class="variable">nodes</span>, <span class="variable">keys</span>);
-
- <span class="keyword">return</span> <span class="variable">position</span> === <span class="variable">nodes</span>.<span class="variable">length</span> ? <span class="number integer">0</span> : <span class="variable">position</span>;
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Replaces a assigned server of the ring with a new server
-hot swapping servers</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> oldServer The server that needs to be replaced</p></li><li><p><strong>param</strong>: <em>String</em> newServer The new server</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">replaceServer</span> = <span class="keyword">function</span>(<span class="variable">oldServer</span>, <span class="variable">newServer</span>){
- <span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
-
- <span class="comment">// replace the servers</span>
- <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">ring</span>).<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">key</span>){
- <span class="keyword">if</span> (<span class="variable">self</span>.<span class="variable">ring</span>[<span class="variable">key</span>] === <span class="variable">oldServer</span>){
- <span class="variable">self</span>.<span class="variable">ring</span>[<span class="variable">key</span>] = <span class="variable">newServer</span>;
- }
- });
-
- <span class="comment">// update the cache, because we don't want to completly kill our cache</span>
- <span class="comment">// as other key -&gt; server lookups might still be valid</span>
- <span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">cache</span>).<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">key</span>){
- <span class="keyword">if</span> (<span class="variable">self</span>.<span class="variable">cache</span>[<span class="variable">key</span>] === <span class="variable">oldServer</span>){
- <span class="variable">self</span>.<span class="variable">cache</span>[<span class="variable">key</span>] = <span class="variable">newServer</span>;
- }
- });
-
- <span class="comment">// remove the server from this.nodes and replace it with new server as well</span>
- <span class="this">this</span>.<span class="variable">nodes</span>.<span class="variable">splice</span>(<span class="this">this</span>.<span class="variable">nodes</span>.<span class="variable">indexOf</span>(<span class="variable">oldServer</span>), <span class="number integer">1</span>, <span class="variable">newServer</span>);
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Adds a server and regenerates the ring</p>
-
-<ul><li><p><strong>todo</strong>: <em>create</em>
-the same arguments interface as the constructor.
-## </p></li><li><p><strong>param</strong>: <em>Object</em> weights server=&gt;weights</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">addServer</span> = <span class="keyword">function</span>(<span class="variable">server</span>, <span class="variable">weights</span>){
- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">nodes</span>.<span class="variable">indexOf</span>(<span class="variable">server</span>) !== -<span class="number integer">1</span>) <span class="keyword">return</span>; <span class="comment">// prevents duplicates</span>
-
- <span class="comment">// add weights </span>
- <span class="keyword">if</span> (<span class="variable">weights</span>){
- <span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">key</span> <span class="keyword">in</span> <span class="variable">weights</span>){
- <span class="this">this</span>.<span class="variable">weights</span>[<span class="variable">key</span>] = <span class="variable">weights</span>[<span class="variable">key</span>];
- }
- }
- <span class="comment">// pushes one, or more servers to</span>
- <span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">push</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">nodes</span>,<span class="variable">server</span>);
- <span class="this">this</span>.<span class="variable">sortedKeys</span>.<span class="variable">length</span> = <span class="number integer">0</span>;
-
- <span class="comment">// clear all old caches and regenerate</span>
- <span class="this">this</span>.<span class="variable">ring</span> = {};
- <span class="this">this</span>.<span class="variable">cache</span> = {};
- <span class="this">this</span>.<span class="variable">generateRing</span>();
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Iterates over the nodes for a give key, can be used to create redundancy support.</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> key The key that should get a range</p></li><li><p><strong>param</strong>: <em>Number</em> size The maxium amount of nodes to return</p></li><li><p><strong>param</strong>: <em>Boolean</em> distinct Get duplicate nodes</p></li><li><p><strong>returns</strong>: <em>Array</em> The nodes</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">createRange</span> = <span class="keyword">function</span>(<span class="variable">key</span>, <span class="variable">size</span>, <span class="variable">distinct</span>){
- <span class="keyword">if</span> (!<span class="class">Object</span>.<span class="variable">keys</span>(<span class="this">this</span>.<span class="variable">ring</span>).<span class="variable">length</span>) <span class="keyword">return</span> <span class="variable">false</span>;
-
- <span class="variable">distinct</span> = <span class="variable">distinct</span> === <span class="string">'undefined'</span> ? <span class="variable">true</span> : <span class="variable">distinct</span>;
-
- <span class="keyword">var</span> <span class="variable">returnvalues</span> = {}
- , <span class="variable">returnnodes</span> = []
- , <span class="variable">position</span> = <span class="this">this</span>.<span class="variable">getNodePosition</span>(<span class="variable">key</span>)
- , <span class="variable">slices</span> = <span class="this">this</span>.<span class="variable">sortedKeys</span>.<span class="variable">slice</span>(<span class="variable">position</span>)
- <span class="comment">// a small filter function that checks for duplicates</span>
- , <span class="variable">distinctFilter</span> = <span class="keyword">function</span>(<span class="variable">value</span>){
- <span class="keyword">if</span> (!<span class="variable">returnvalues</span>[<span class="variable">value</span>]){
- <span class="variable">returnvalues</span>[<span class="variable">value</span>] = <span class="variable">true</span>;
- <span class="keyword">return</span> <span class="variable">value</span>;
- }
- }
- , <span class="variable">value</span>
- , <span class="variable">i</span> = <span class="number integer">0</span>
- , <span class="variable">length</span> = <span class="variable">slices</span>.<span class="variable">length</span>;
-
- <span class="keyword">for</span>(; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">length</span>; <span class="variable">i</span>++){
- <span class="variable">value</span> = <span class="variable">distinct</span> ? <span class="variable">distinctFilter</span>(<span class="this">this</span>.<span class="variable">ring</span>[<span class="variable">slices</span>[<span class="variable">i</span>]]) : <span class="this">this</span>.<span class="variable">ring</span>[<span class="variable">slices</span>[<span class="variable">i</span>]];
- <span class="keyword">if</span> (<span class="variable">value</span>) <span class="variable">returnnodes</span>.<span class="variable">push</span>(<span class="variable">value</span>);
- <span class="keyword">if</span> (<span class="variable">size</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">returnnodes</span>.<span class="variable">length</span> &<span class="variable">gt</span>;= <span class="variable">size</span>) <span class="keyword">break</span>;
- };
-
- <span class="comment">// as we might have reached the end of our sortedKeys array, and didn't fill our returnnodes completely:</span>
- <span class="keyword">if</span>(!<span class="variable">size</span> || <span class="variable">returnnodes</span>.<span class="variable">length</span> &<span class="variable">lt</span>; <span class="variable">size</span>){
- <span class="keyword">for</span>(<span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">length</span> = <span class="this">this</span>.<span class="variable">sortedKeys</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">length</span>; <span class="variable">i</span>++){
- <span class="keyword">if</span> (<span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">position</span>){
- <span class="variable">value</span> = <span class="variable">distinct</span> ? <span class="variable">distinctFilter</span>(<span class="this">this</span>.<span class="variable">ring</span>[<span class="this">this</span>.<span class="variable">sortedKeys</span>[<span class="variable">i</span>]]) : <span class="this">this</span>.<span class="variable">ring</span>[<span class="this">this</span>.<span class="variable">sortedKeys</span>[<span class="variable">i</span>]];
- <span class="keyword">if</span> (<span class="variable">value</span>) <span class="variable">returnnodes</span>.<span class="variable">push</span>(<span class="variable">value</span>);
- <span class="keyword">if</span>(<span class="variable">size</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">returnnodes</span>.<span class="variable">length</span> &<span class="variable">gt</span>;= <span class="variable">size</span>) <span class="keyword">break</span>;
- } <span class="keyword">else</span> {
- <span class="keyword">break</span>;
- }
- }
- }
-
- <span class="comment">// now that we have collect all the nodes, we can return the range</span>
- <span class="keyword">return</span> <span class="variable">returnnodes</span>;
-
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Generates a long value of the key that represents a place on the hash ring.</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> hash The hash that needs to transformed to a long value</p></li><li><p><strong>returns</strong>: <em>String</em> The hash value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">generateKey</span> = <span class="keyword">function</span>(<span class="variable">hash</span>){
- <span class="keyword">return</span> <span class="this">this</span>.<span class="variable">hashValue</span>(<span class="this">this</span>.<span class="variable">hashKey</span>(<span class="variable">hash</span>), <span class="keyword">function</span>(<span class="variable">x</span>){<span class="keyword">return</span> <span class="variable">x</span>});
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Changes the returned key to a <code>long value</code> by using the compare function</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> key The key that needs to be calculated</p></li><li><p><strong>param</strong>: <em>Function</em> compare The calculation function</p></li><li><p><strong>returns</strong>: <em>String</em> The hash value</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">hashValue</span> = <span class="keyword">function</span>(<span class="variable">key</span>, <span class="variable">compare</span>){
- <span class="keyword">return</span> (
- (<span class="variable">key</span>[<span class="variable">compare</span>(<span class="number integer">3</span>)] &<span class="variable">lt</span>;&<span class="variable">lt</span>; <span class="number integer">24</span>) |
- (<span class="variable">key</span>[<span class="variable">compare</span>(<span class="number integer">2</span>)] &<span class="variable">lt</span>;&<span class="variable">lt</span>; <span class="number integer">16</span>) |
- (<span class="variable">key</span>[<span class="variable">compare</span>(<span class="number integer">1</span>)] &<span class="variable">lt</span>;&<span class="variable">lt</span>; <span class="number integer">8</span>) |
- <span class="variable">key</span>[<span class="variable">compare</span>(<span class="number integer">0</span>)]
- )
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Generates a hash from a key</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> The key that needs to be transformed in to a hash.</p></li><li><p><strong>returns</strong>: <em>String</em> The hashed key</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">hashKey</span> = <span class="keyword">function</span>(<span class="variable">key</span>){
- <span class="keyword">return</span> <span class="class">CreateHash</span>(<span class="this">this</span>.<span class="variable">algorithm</span>).<span class="variable">update</span>(<span class="variable">key</span>).<span class="variable">digest</span>(<span class="string">'hex'</span>).<span class="variable">split</span>(<span class="string">''</span>).<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">v</span>){ <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">charCodeAt</span>(<span class="number integer">0</span>) })
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Generates a crc32 value of a string.</p>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>String</em> str The string that needs to converted in to a hash.</p></li><li><p><strong>returns</strong>: <em>String</em> The hash</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">HashRing</span>.<span class="variable">crc32HashKey</span> = <span class="keyword">function</span>(<span class="variable">str</span>){
- <span class="variable">str</span> = <span class="keyword">new</span> <span class="class">StringDecoder</span>(<span class="string">'utf8'</span>).<span class="variable">write</span>(<span class="variable">str</span>);
-
- <span class="keyword">var</span> <span class="variable">crc</span> = <span class="number integer">0</span> ^ (-<span class="number integer">1</span>)
- , <span class="variable">i</span> = <span class="number integer">0</span>
- , <span class="variable">length</span> = <span class="variable">str</span>.<span class="variable">length</span>
- , <span class="variable">map</span> = <span class="string">'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'</span>;
-
- <span class="keyword">for</span>(; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">length</span>; <span class="variable">i</span>++){
- <span class="variable">crc</span> = (<span class="variable">crc</span> &<span class="variable">gt</span>;&<span class="variable">gt</span>;&<span class="variable">gt</span>; <span class="number integer">8</span>) ^ (<span class="string">'0x'</span> + <span class="variable">map</span>.<span class="variable">substr</span>(((<span class="variable">crc</span> ^ <span class="variable">str</span>.<span class="variable">charCodeAt</span>(<span class="variable">i</span>)) &<span class="variable">amp</span>; <span class="variable">xFF</span>) * <span class="number integer">9</span>, <span class="number integer">8</span>));
- }
-
- <span class="variable">crc</span> = <span class="variable">crc</span> ^ (-<span class="number integer">1</span>);
- <span class="keyword">return</span> (<span class="variable">crc</span> &<span class="variable">lt</span>; <span class="number integer">0</span> ? <span class="variable">crc</span> += <span class="number integer">4294967296</span> : <span class="variable">crc</span>).<span class="variable">toString</span>().<span class="variable">split</span>(<span class="string">''</span>).<span class="variable">map</span>(<span class="keyword">function</span>(<span class="variable">v</span>){ <span class="keyword">return</span> <span class="variable">v</span>.<span class="variable">charCodeAt</span>(<span class="number integer">0</span>) })
-};</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Library version
- </p>
-</td>
-<td class="code">
-<pre><code><span class="variable">hashRing</span>.<span class="variable">version</span> = <span class="string">'0.0.2'</span>;
-
-<span class="variable">module</span>.<span class="variable">exports</span> = <span class="variable">hashRing</span>;</code></pre>
-</td>
-</tr> </body>
-</html></tbody></table>
View
4 lib/hashring.js
@@ -129,14 +129,14 @@ HashRing.getNode = function(key){
/**
* Returns the position of the key inside the keyring
*
- * @todo Remove the Object.keys lookup, or just cache the `keys` on regeneration.
* @param {String} key The key that needs his it's position calculated.
* @returns {Number} the index of sortedKeys
* @api public
*/
HashRing.getNodePosition = function(key){
- if (!Object.keys(this.ring).length)
+ if (!this.sortedKeys.length){
return false;
+ }
var keys = this.generateKey(key)
, nodes = this.sortedKeys
View
33 tests/hashring.test.js
@@ -38,4 +38,37 @@ module.exports = {
ring.sortedKeys.length.should.be.above(1);
Object.keys(ring.weights).should.have.length(0);
}
+
+, 'Looking up keys': function(){
+ var ring = new hashring(['192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212']);
+ ring.nodes.indexOf(ring.getNode('foo')).should.be.above(-1);
+
+ // NOTE we are going to do some flaky testing ;P
+ ring.getNode('foo').should.equal('192.168.0.104:11212');
+ ring.getNode('pewpew').should.equal('192.168.0.103:11212');
+
+ // we are not gonna verify the results
+ // we are just gonna test if we don't fuck something up in the code, so it throws errors or whatever
+
+ // unicode keys, just because people roll like that
+ ring.nodes.indexOf(ring.getNode('привет мир, Memcached и nodejs для победы')).should.be.above(-1);
+
+ // other odd keys
+ ring.nodes.indexOf(ring.getNode(1)).should.be.above(-1);
+ ring.nodes.indexOf(ring.getNode(0)).should.be.above(-1);
+ ring.nodes.indexOf(ring.getNode([])).should.be.above(-1);
+ ring.nodes.indexOf(ring.getNode({wtf:'lol'})).should.be.above(-1);
+ ring.getNode({wtf:'lol'}).should.equal(ring.getNode({wtf:'amazing .toStringing'}));
+ }
+
+, 'Replacing servers': function(){
+ var ring = new hashring(['192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212'])
+ , amazon = ring.getNode('justdied')
+ , skynet = '192.168.0.128:11212'
+
+ ring.replaceServer(amazon, skynet);
+ ring.cache.justdied.should.equal(skynet);
+ ring.cache = {}; // clear cache
+ ring.getNode('justdied').should.equal(skynet);
+ }
};

0 comments on commit b6c9a45

Please sign in to comment.
Something went wrong with that request. Please try again.