Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Release v0.0.2

  • Loading branch information...
commit d0057e160104a1b5b014e2e0e1527f5e61ca32cb 1 parent bc68b81
@3rd-Eden 3rd-Eden authored
Showing with 42 additions and 13 deletions.
  1. +3 −0  CHANGELOG
  2. +19 −6 doc/index.html
  3. +19 −6 lib/hashring.js
  4. +1 −1  package.json
View
3  CHANGELOG
@@ -0,0 +1,3 @@
+April 21, 2011
+
+- Fixed some issues related to the cache addition as the cache needs to be cleared when new servers are added and replaced.
View
25 doc/index.html
@@ -191,7 +191,7 @@
<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="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>;
}
@@ -242,7 +242,7 @@
, <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>;
+ <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>
@@ -259,9 +259,19 @@
<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="class">Object</span>.<span class="variable">key</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="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>
@@ -290,7 +300,10 @@
<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>
@@ -307,7 +320,7 @@
<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="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> = []
@@ -428,7 +441,7 @@
</p>
</td>
<td class="code">
-<pre><code><span class="variable">hashRing</span>.<span class="variable">version</span> = <span class="string">'0.0.1'</span>;
+<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>
View
25 lib/hashring.js
@@ -43,7 +43,7 @@ function hashRing(args, algorithm){
this.algorithm = algorithm || 'crc32';
// overwrite the hashKey method if crc32 is chosen
- if (this.algorithm == 'crc32'){
+ if (this.algorithm === 'crc32'){
this.hashKey = this.crc32HashKey;
}
@@ -142,7 +142,7 @@ HashRing.getNodePosition = function(key){
, nodes = this.sortedKeys
, position = Bisection(nodes, keys);
- return position == nodes.length ? 0 : position;
+ return position === nodes.length ? 0 : position;
};
/**
@@ -156,9 +156,19 @@ HashRing.getNodePosition = function(key){
HashRing.replaceServer = function(oldServer, newServer){
var self = this;
- Object.key(this.ring).forEach(function(key){
- if (self.ring[key] == oldServer)
+ // replace the servers
+ Object.keys(this.ring).forEach(function(key){
+ if (self.ring[key] === oldServer){
self.ring[key] = newServer;
+ }
+ });
+
+ // update the cache, because we don't want to completly kill our cache
+ // as other key -> server lookups might still be valid
+ Object.keys(this.cache).forEach(function(key){
+ if (self.cache[key] === oldServer){
+ self.cache[key] = newServer;
+ }
});
// remove the server from this.nodes and replace it with new server as well
@@ -184,7 +194,10 @@ HashRing.addServer = function(server, weights){
// pushes one, or more servers to
Array.prototype.push.apply(this.nodes,server);
this.sortedKeys.length = 0;
+
+ // clear all old caches and regenerate
this.ring = {};
+ this.cache = {};
this.generateRing();
};
@@ -200,7 +213,7 @@ HashRing.addServer = function(server, weights){
HashRing.createRange = function(key, size, distinct){
if (!Object.keys(this.ring).length) return false;
- distinct = distinct == 'undefined' ? true : distinct;
+ distinct = distinct === 'undefined' ? true : distinct;
var returnvalues = {}
, returnnodes = []
@@ -306,6 +319,6 @@ HashRing.crc32HashKey = function(str){
/**
* Library version
*/
-hashRing.version = '0.0.1';
+hashRing.version = '0.0.2';
module.exports = hashRing;
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "hashring"
-, "version": "0.0.1"
+, "version": "0.0.2"
, "author": "Arnout Kazemier"
, "description": "A pure JavaScript hash ring based on libketama"
, "main": "index"
Please sign in to comment.
Something went wrong with that request. Please try again.