megahash
Advanced tools
Comparing version 1.0.2 to 1.0.3
40
main.js
@@ -7,13 +7,13 @@ // MegaHash v1.0 | ||
const BH_TYPE_BUFFER = 0; | ||
const BH_TYPE_STRING = 1; | ||
const BH_TYPE_NUMBER = 2; | ||
const BH_TYPE_BOOLEAN = 3; | ||
const BH_TYPE_OBJECT = 4; | ||
const BH_TYPE_BIGINT = 5; | ||
const BH_TYPE_NULL = 6; | ||
const MH_TYPE_BUFFER = 0; | ||
const MH_TYPE_STRING = 1; | ||
const MH_TYPE_NUMBER = 2; | ||
const MH_TYPE_BOOLEAN = 3; | ||
const MH_TYPE_OBJECT = 4; | ||
const MH_TYPE_BIGINT = 5; | ||
const MH_TYPE_NULL = 6; | ||
MegaHash.prototype.set = function(key, value) { | ||
// store key/value in hash, auto-convert format to buffer | ||
var flags = BH_TYPE_BUFFER; | ||
var flags = MH_TYPE_BUFFER; | ||
var keyBuf = Buffer.isBuffer(key) ? key : Buffer.from(''+key, 'utf8'); | ||
@@ -26,7 +26,7 @@ if (!keyBuf.length) throw new Error("Key must have length"); | ||
valueBuf = Buffer.alloc(0); | ||
flags = BH_TYPE_NULL; | ||
flags = MH_TYPE_NULL; | ||
} | ||
else if (typeof(valueBuf) == 'object') { | ||
valueBuf = Buffer.from( JSON.stringify(value) ); | ||
flags = BH_TYPE_OBJECT; | ||
flags = MH_TYPE_OBJECT; | ||
} | ||
@@ -36,3 +36,3 @@ else if (typeof(valueBuf) == 'number') { | ||
valueBuf.writeDoubleBE( value ); | ||
flags = BH_TYPE_NUMBER; | ||
flags = MH_TYPE_NUMBER; | ||
} | ||
@@ -42,3 +42,3 @@ else if (typeof(valueBuf) == 'bigint') { | ||
valueBuf.writeBigInt64BE( value ); | ||
flags = BH_TYPE_BIGINT; | ||
flags = MH_TYPE_BIGINT; | ||
} | ||
@@ -48,7 +48,7 @@ else if (typeof(valueBuf) == 'boolean') { | ||
valueBuf.writeUInt8( value ? 1 : 0 ); | ||
flags = BH_TYPE_BOOLEAN; | ||
flags = MH_TYPE_BOOLEAN; | ||
} | ||
else { | ||
valueBuf = Buffer.from(''+value, 'utf8'); | ||
flags = BH_TYPE_STRING; | ||
flags = MH_TYPE_STRING; | ||
} | ||
@@ -69,23 +69,23 @@ } | ||
switch (value.flags) { | ||
case BH_TYPE_NULL: | ||
case MH_TYPE_NULL: | ||
value = null; | ||
break; | ||
case BH_TYPE_OBJECT: | ||
case MH_TYPE_OBJECT: | ||
value = JSON.parse( value.toString() ); | ||
break; | ||
case BH_TYPE_NUMBER: | ||
case MH_TYPE_NUMBER: | ||
value = value.readDoubleBE(); break; | ||
break; | ||
case BH_TYPE_BIGINT: | ||
case MH_TYPE_BIGINT: | ||
value = value.readBigInt64BE(); break; | ||
break; | ||
case BH_TYPE_BOOLEAN: | ||
case MH_TYPE_BOOLEAN: | ||
value = (value.readUInt8() == 1) ? true : false; | ||
break; | ||
case BH_TYPE_STRING: | ||
case MH_TYPE_STRING: | ||
value = value.toString(); | ||
@@ -92,0 +92,0 @@ break; |
{ | ||
"name": "megahash", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A super-fast C++ hash table with Node.js wrapper.", | ||
@@ -5,0 +5,0 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", |
@@ -21,2 +21,3 @@ <details><summary>Table of Contents</summary> | ||
* [Iterating over Keys](#iterating-over-keys) | ||
* [Error Handling](#error-handling) | ||
* [Hash Stats](#hash-stats) | ||
@@ -252,2 +253,13 @@ - [API](#api) | ||
## Error Handling | ||
If a hash operation fails (i.e. out of memory), then [set()](#set) will return `0`. You can check for this and bubble up your own error. Example: | ||
```js | ||
var result = hash.set( "hello", "there" ); | ||
if (!result) { | ||
throw new Error("Failed to write to MegaHash: Out of memory"); | ||
} | ||
``` | ||
## Hash Stats | ||
@@ -291,3 +303,3 @@ | ||
``` | ||
VOID set( KEY, VALUE ) | ||
NUMBER set( KEY, VALUE ) | ||
``` | ||
@@ -301,2 +313,10 @@ | ||
The `set()` method actually returns a number, which will be `0`, `1` or `2`. They each have a different meaning: | ||
| Result | Description | | ||
|--------|-------------| | ||
| `0` | An error occurred (out of memory). | | ||
| `1` | A key was added to the hash (i.e. unique key). | | ||
| `2` | An existing key was replaced in the hash. | | ||
## get | ||
@@ -314,2 +334,4 @@ | ||
If the key is not found, `get()` will return `undefined`. | ||
## has | ||
@@ -316,0 +338,0 @@ |
@@ -213,2 +213,10 @@ // Unit tests for MegaHash | ||
function testSetReturnValue(test) { | ||
// make sure set() returns the expected return values | ||
var hash = new MegaHash(); | ||
test.ok( hash.set("hello", "there") == 1, "Unique key returns 1 on set" ); | ||
test.ok( hash.set("hello", "there") == 2, "Replaced key returns 2 on set" ); | ||
test.done(); | ||
}, | ||
function testRemove(test) { | ||
@@ -215,0 +223,0 @@ var hash = new MegaHash(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66538
586
469