Permalink
Browse files

initial files

  • Loading branch information...
pconstr committed Dec 11, 2011
0 parents commit e46035862a706a86ebea77d63cd09b6113d52897
Showing with 1,022 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 .npmignore
  3. +20 −0 MIT-LICENSE.txt
  4. +89 −0 README.md
  5. +25 −0 package.json
  6. +63 −0 perf/native.js
  7. +66 −0 perf/rawhash.js
  8. +335 −0 src/MurmurHash3.cpp
  9. +37 −0 src/MurmurHash3.h
  10. +281 −0 src/rawhash.cpp
  11. +60 −0 test.js
  12. +37 −0 wscript
@@ -0,0 +1,5 @@
+.lock-wscript
+build
+rawhash.node
+*~
+\#*\#
@@ -0,0 +1,4 @@
+.gitignore
+*~
+\#*\#
+rawhash.node
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Carlos Guerreiro, http://perceptiveconstructs.com
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,89 @@
+rawhash
+-------
+
+An experimental binary friendly alternative to using a hash as a key:value cache, for [node.js](http://www.nodejs.org).
+
+Keys are binary [Buffer](http://nodejs.org/docs/v0.6.5/api/buffers.html) objects rather than strings. Values are arbitrary objects.
+
+`rawhash` is built on [google-sparsehash](http://code.google.com/p/google-sparsehash) (not included) and [murmurhash3](http://code.google.com/p/smhasher/) (included).
+
+Install
+-------
+
+get google-sparsehash:
+
+* on Debian/Ubuntu: `apt-get install libsparsehash-dev`
+* on OS X: `brew install google-sparsehash` or `port install google-sparsehash`
+
+or get the latest version from the [google\-sparsehash project](http://code.google.com/p/google-sparsehash/downloads/list)
+
+then install rawhash itself:
+
+`npm install rawhash`
+
+Usage
+-----
+
+```javascript
+var rh = require('rawhash');
+var k = new Buffer(6);
+var h = new rh.Sparse();
+h.set(k, {a:1, b:2});
+console.log(h.get(k));
+h.each(function(k, v) {
+ console.log(k, v);
+});
+h.del(k);
+```
+
+There are 3 kinds of hashes:
+
+* `Sparse` is slower, but uses less memory, uses [sparse\_hash\_map<>](http://google-sparsehash.googlecode.com/svn/trunk/doc/sparse_hash_map.html)
+* `Dense` is faster, but uses more memory, uses [dense\_hash\_map<>](http://google-sparsehash.googlecode.com/svn/trunk/doc/dense_hash_map.html)
+* `Map` is usually somewhere between `Sparse` and `Dense`, uses the STL's [map<>](http://www.sgi.com/tech/stl/Map.html) which is actually ordered and supports range queries - not exposed in `Map`
+
+`Sparse` and `Dense` take an optional argument to seed `murmurhash3`.
+
+```javascript
+var h = new rh.Dense(42);
+```
+
+Performance
+-----------
+
+This is largely TBD
+
+On a synthetic test (see `./perf/`) with 150K sets, gets and deletes (very similar to `./test.js`) on a low-end MBP, this is how rawhash compares with using a Javascript hash:
+
+<pre>
+Sparse 509 ms
+Dense 330 ms
+Map 463 ms
+{} 754 ms
+</pre>
+
+License
+-------
+
+(The MIT License)
+
+Copyright (c) 2011 Carlos Guerreiro, [perceptiveconstructs.com](http://perceptiveconstructs.com)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
+{
+ "name": "rawhash",
+ "version": "0.1.3",
+ "keywords": ["key:value", "buffer", "store", "in-memory", "cache"],
+ "homepage": "http://perceptiveconstructs.com",
+ "description": "experimental in-memory key:value cache where keys are binary Buffers",
+ "main": "rawhash.node",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/pconstr/rawhash"
+ },
+ "engines": {
+ "node": ">=0.4.0 <0.7.0"
+ },
+ "author": "Carlos Guerreiro <carlos@perceptiveconstructs.com (http://perceptiveconstructs.com)",
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/MIT"
+ }
+ ],
+ "scripts": {
+ "test": "./test.js"
+ }
+}
@@ -0,0 +1,63 @@
+#!/usr/bin/env node
+
+/* Copyright 2011, Carlos Guerreiro
+ * Licensed under the MIT license */
+
+var assert = require('assert');
+
+function doTests(h) {
+ var timeStart = Date.now();
+
+ var numItems = 150000;
+ var i;
+ var b = new Buffer(18);
+ for(i = 0; i < 18; ++i)
+ b[i] = i;
+
+ var i0, i1, i2, i3;
+ for(i = 0; i < numItems; ++i) {
+ i0 = i & 127;
+ i1 = (i >> 7) & 127;
+ i2 = (i >> 14) & 127;
+ i3 = (i >> 21) & 127;
+ b[0] = i0;
+ b[1] = i1;
+ b[2] = i2;
+ b[3] = i3;
+ h[b] = {a: i0, b: i1, c: i2};
+ }
+ h[b] = {a: i0, b: i1, c: i2}; // reset value
+
+ var v;
+ for(i = 0; i < numItems; ++i) {
+ i0 = i & 127;
+ i1 = (i >> 7) & 127;
+ i2 = (i >> 14) & 127;
+ i3 = (i >> 21) & 127;
+ b[0] = i0;
+ b[1] = i1;
+ b[2] = i2;
+ b[3] = i3;
+ v = h[b];
+ assert.equal(v.a, i0);
+ assert.equal(v.b, i1);
+ assert.equal(v.c, i2);
+ }
+
+ for(i = 0; i < numItems; ++i) {
+ i0 = i & 127;
+ i1 = (i >> 7) & 127;
+ i2 = (i >> 14) & 127;
+ i3 = (i >> 21) & 127;
+ b[0] = i0;
+ b[1] = i1;
+ b[2] = i2;
+ b[3] = i3;
+ assert(delete h[b]);
+ }
+
+ var timeEnd = Date.now();
+ console.log(h.constructor.name+ ' '+ (timeEnd - timeStart)+ ' ms');
+}
+
+doTests({});
@@ -0,0 +1,66 @@
+#!/usr/bin/env node
+
+/* Copyright 2011, Carlos Guerreiro
+ * Licensed under the MIT license */
+
+var assert = require('assert');
+var rh = require("../rawhash");
+
+function doTests(h) {
+ var timeStart = Date.now();
+
+ var numItems = 150000;
+ var i;
+ var b = new Buffer(18);
+ for(i = 0; i < 18; ++i)
+ b[i] = i;
+
+ var i0, i1, i2, i3;
+ for(i = 0; i < numItems; ++i) {
+ i0 = i & 127;
+ i1 = (i >> 7) & 127;
+ i2 = (i >> 14) & 127;
+ i3 = (i >> 21) & 127;
+ b[0] = i0;
+ b[1] = i1;
+ b[2] = i2;
+ b[3] = i3;
+ h.set(b, {a: i0, b: i1, c: i2});
+ }
+ h.set(b, {a: i0, b: i1, c: i2}); // reset value
+
+ var v;
+ for(i = 0; i < numItems; ++i) {
+ i0 = i & 127;
+ i1 = (i >> 7) & 127;
+ i2 = (i >> 14) & 127;
+ i3 = (i >> 21) & 127;
+ b[0] = i0;
+ b[1] = i1;
+ b[2] = i2;
+ b[3] = i3;
+ v = h.get(b);;
+ assert.equal(v.a, i0);
+ assert.equal(v.b, i1);
+ assert.equal(v.c, i2);
+ }
+
+ for(i = 0; i < numItems; ++i) {
+ i0 = i & 127;
+ i1 = (i >> 7) & 127;
+ i2 = (i >> 14) & 127;
+ i3 = (i >> 21) & 127;
+ b[0] = i0;
+ b[1] = i1;
+ b[2] = i2;
+ b[3] = i3;
+ assert(h.del(b));
+ }
+
+ var timeEnd = Date.now();
+ console.log(h.constructor.name+ ' '+ (timeEnd - timeStart)+ ' ms');
+}
+
+doTests(new rh.Sparse(42));
+doTests(new rh.Dense(42));
+doTests(new rh.Map());
Oops, something went wrong.

0 comments on commit e460358

Please sign in to comment.