Comparing version 0.9.6 to 0.9.7
@@ -1,4 +0,4 @@ | ||
require('../../test/common'); | ||
var config = require('../../test/config'); | ||
var COUNT = 1e6, | ||
dirty = require('dirty')(), | ||
dirty = require(config.LIB_DIRTY)(), | ||
util = require('util'); | ||
@@ -10,3 +10,3 @@ | ||
var start = +new Date, i = 0; | ||
var start = Date.now(), i = 0; | ||
dirty.forEach(function(key, doc) { | ||
@@ -18,3 +18,3 @@ if (!key && key !== 0) { | ||
var ms = +new Date - start, | ||
var ms = Date.now() - start, | ||
mhz = ((COUNT / (ms / 1000)) / 1e6).toFixed(2), | ||
@@ -25,2 +25,1 @@ million = COUNT / 1e6; | ||
util.log(mhz+' Mhz ('+million+' million in '+ms+' ms)'); | ||
@@ -1,4 +0,4 @@ | ||
require('../../test/common'); | ||
var config = require('../../test/config'); | ||
var COUNT = 1e6, | ||
dirty = require('dirty')(), | ||
dirty = require(config.LIB_DIRTY)(), | ||
util = require('util'); | ||
@@ -10,3 +10,3 @@ | ||
var start = +new Date; | ||
var start = Date.now(); | ||
for (var i = 0; i < COUNT; i++) { | ||
@@ -18,3 +18,3 @@ if (dirty.get(i) !== i) { | ||
var ms = +new Date - start, | ||
var ms = Date.now() - start, | ||
mhz = ((COUNT / (ms / 1000)) / 1e6).toFixed(2), | ||
@@ -21,0 +21,0 @@ million = COUNT / 1e6; |
@@ -1,5 +0,5 @@ | ||
require('../../test/common'); | ||
var config = require('../../test/config'); | ||
var COUNT = 1e4, | ||
DB_FILE = __dirname+'/../../test/tmp/benchmark-set-drain.dirty', | ||
dirty = require('dirty')(DB_FILE), | ||
DB_FILE = config.TMP_PATH + '/benchmark-set-drain.dirty', | ||
dirty = require(config.LIB_DIRTY)(DB_FILE), | ||
util = require('util'), | ||
@@ -13,5 +13,5 @@ loaded = false; | ||
dirty.on('drain', function() { | ||
var start = +new Date; | ||
var start = Date.now(); | ||
require('dirty')(DB_FILE).on('load', function(length) { | ||
var ms = +new Date - start, | ||
var ms = Date.now() - start, | ||
mhz = ((COUNT / (ms / 1000)) / 1e3).toFixed(2), | ||
@@ -18,0 +18,0 @@ million = COUNT / 1e6; |
@@ -1,8 +0,8 @@ | ||
require('../../test/common'); | ||
var config = require('../../test/config'); | ||
var COUNT = 1e5, | ||
dirty = require('dirty')(__dirname+'/../../test/tmp/benchmark-set-drain.dirty'), | ||
dirty = require(config.LIB_DIRTY)(config.TMP_PATH + '/benchmark-set-drain.dirty'), | ||
util = require('util'), | ||
drained = false; | ||
var start = +new Date; | ||
var start = Date.now(); | ||
for (var i = 0; i < COUNT; i++) { | ||
@@ -13,3 +13,3 @@ dirty.set(i, 'This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string has 256 bytes. This string'); | ||
dirty.on('drain', function() { | ||
var ms = +new Date - start, | ||
var ms = Date.now() - start, | ||
mhz = ((COUNT / (ms / 1000)) / 1e3).toFixed(2), | ||
@@ -16,0 +16,0 @@ million = COUNT / 1e6; |
@@ -1,8 +0,8 @@ | ||
require('../../test/common'); | ||
var config = require('../../test/config'); | ||
var COUNT = 1e4, | ||
dirty = require('dirty')(__dirname+'/../../test/tmp/benchmark-set-drain.dirty'), | ||
dirty = require(config.LIB_DIRTY)(config.TMP_PATH + '/benchmark-set-drain.dirty'), | ||
util = require('util'), | ||
drained = false; | ||
var start = +new Date; | ||
var start = Date.now(); | ||
for (var i = 0; i < COUNT; i++) { | ||
@@ -13,3 +13,3 @@ dirty.set(i, i); | ||
dirty.on('drain', function() { | ||
var ms = +new Date - start, | ||
var ms = Date.now() - start, | ||
mhz = ((COUNT / (ms / 1000)) / 1e3).toFixed(2), | ||
@@ -16,0 +16,0 @@ million = COUNT / 1e6; |
@@ -1,7 +0,7 @@ | ||
require('../../test/common'); | ||
var config = require('../../test/config'); | ||
var COUNT = 1e6, | ||
dirty = require('dirty')(__dirname+'/../../test/tmp/benchmark-set.dirty'), | ||
dirty = require(config.LIB_DIRTY)(config.TMP_PATH + '/benchmark-set.dirty'), | ||
util = require('util'); | ||
var start = +new Date; | ||
var start = Date.now(); | ||
for (var i = 0; i < COUNT; i++) { | ||
@@ -11,3 +11,3 @@ dirty.set(i, i); | ||
var ms = +new Date - start, | ||
var ms = Date.now() - start, | ||
mhz = ((COUNT / (ms / 1000)) / 1e6).toFixed(2), | ||
@@ -18,3 +18,1 @@ million = COUNT / 1e6; | ||
util.log(mhz+' Mhz ('+million+' million in '+ms+' ms)'); | ||
process.exit(0); | ||
@@ -7,2 +7,6 @@ if (global.GENTLY) require = GENTLY.hijack(require); | ||
/** | ||
* Constructor function | ||
*/ | ||
var Dirty = exports.Dirty = function(path) { | ||
@@ -29,8 +33,14 @@ if (!(this instanceof Dirty)) return new Dirty(path); | ||
/** | ||
* set() stores a JSON object in the database at key | ||
* cb is fired when the data is persisted. | ||
* In memory, this is immediate- on disk, it will take some time. | ||
*/ | ||
Dirty.prototype.set = function(key, val, cb) { | ||
if (val === undefined) { | ||
this._keys.splice(this._keys.indexOf(key), 1) | ||
this._keys.splice(this._keys.indexOf(key), 1); | ||
delete this._docs[key]; | ||
} else { | ||
if (this._keys.indexOf(key) === -1) { | ||
if (this._keys.indexOf(key) === -1) { | ||
this._keys.push(key); | ||
@@ -50,2 +60,6 @@ } | ||
/** | ||
* Get the value stored at a key in the database | ||
* This is synchronous since a cache is maintained in-memory | ||
*/ | ||
Dirty.prototype.get = function(key) { | ||
@@ -55,2 +69,5 @@ return this._docs[key]; | ||
/** | ||
* Get total number of stored keys | ||
*/ | ||
Dirty.prototype.size = function() { | ||
@@ -60,2 +77,5 @@ return this._keys.length; | ||
/** | ||
* Remove a key and the value stored there | ||
*/ | ||
Dirty.prototype.rm = function(key, cb) { | ||
@@ -65,2 +85,6 @@ this.set(key, undefined, cb); | ||
/** | ||
* Iterate over keys, applying match function | ||
*/ | ||
Dirty.prototype.forEach = function(fn) { | ||
@@ -77,2 +101,7 @@ | ||
// Called when a dirty connection is instantiated | ||
Dirty.prototype._load = function() { | ||
@@ -110,6 +139,8 @@ var self = this, buffer = '', length = 0; | ||
self.emit('error', new Error('Empty lines never appear in a healthy database')); | ||
return | ||
return; | ||
} | ||
var row; | ||
try { | ||
var row = JSON.parse(rowStr); | ||
row = JSON.parse(rowStr); | ||
if (!('key' in row)) { | ||
@@ -157,3 +188,3 @@ throw new Error(); | ||
this.flushing = false; | ||
if (!this._queue.length) { | ||
@@ -164,3 +195,3 @@ this.emit('drain'); | ||
} | ||
} | ||
}; | ||
@@ -206,2 +237,4 @@ Dirty.prototype._maybeFlush = function() { | ||
(function(cbs) { | ||
var isDrained; | ||
if (!self.path) { | ||
@@ -215,3 +248,7 @@ process.nextTick(function() { | ||
self._writeStream.write(bundleStr, function(err) { | ||
isDrained = self._writeStream.write(bundleStr, function(err) { | ||
if (isDrained) { | ||
self._writeDrain(); | ||
} | ||
if (!cbs.length && err) { | ||
@@ -224,2 +261,3 @@ self.emit('error', err); | ||
}); | ||
})(cbs); | ||
@@ -226,0 +264,0 @@ |
{ | ||
"name": "dirty", | ||
"description": "A tiny & fast key value store with append-only disk log. Ideal for apps with < 1 million records.", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"dependencies": {}, | ||
"main": "./lib/dirty", | ||
"devDependencies": { | ||
"gently": ">=0.8.0" | ||
"mocha": "~1.8.2", | ||
"rimraf": "~2.1.4" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha test/test-*.js -R list" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
} | ||
} |
@@ -9,3 +9,5 @@ # node-dirty | ||
npm install dirty | ||
```bash | ||
npm install dirty | ||
``` | ||
@@ -25,22 +27,23 @@ ## Why dirty? | ||
require('../test/common'); | ||
var db = require('dirty')('user.db'); | ||
```javascript | ||
var dirty = require('dirty'); | ||
var db = dirty('user.db'); | ||
db.on('load', function() { | ||
db.set('john', {eyes: 'blue'}); | ||
console.log('Added john, he has %s eyes.', db.get('john').eyes); | ||
db.on('load', function() { | ||
db.set('john', {eyes: 'blue'}); | ||
console.log('Added john, he has %s eyes.', db.get('john').eyes); | ||
db.set('bob', {eyes: 'brown'}, function() { | ||
console.log('User bob is now saved on disk.') | ||
}); | ||
db.forEach(function(key, val) { | ||
console.log('Found key: %s, val: %j', key, val); | ||
}); | ||
db.set('bob', {eyes: 'brown'}, function() { | ||
console.log('User bob is now saved on disk.') | ||
}); | ||
db.on('drain', function() { | ||
console.log('All records are saved on disk now.'); | ||
db.forEach(function(key, val) { | ||
console.log('Found key: %s, val: %j', key, val); | ||
}); | ||
}); | ||
db.on('drain', function() { | ||
console.log('All records are saved on disk now.'); | ||
}); | ||
``` | ||
Output: | ||
@@ -63,7 +66,8 @@ | ||
require('dirty')('my.db'); | ||
require('dirty').Dirty('my.db'); | ||
new (require('dirty'))('my.db'); | ||
new (require('dirty').Dirty)('my.db'); | ||
```javascript | ||
require('dirty')('my.db'); | ||
require('dirty').Dirty('my.db'); | ||
new (require('dirty'))('my.db'); | ||
new (require('dirty').Dirty)('my.db'); | ||
``` | ||
### dirty.path | ||
@@ -109,4 +113,15 @@ | ||
## Tests | ||
Dirty utilizes the [Mocha](http://visionmedia.github.com/mocha/) test framework. | ||
```bash | ||
git clone https://github.com/felixge/node-dirty | ||
cd node-dirty | ||
npm install | ||
npm test | ||
``` | ||
## License | ||
node-dirty is licensed under the MIT license. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
124
0
26628
2
33
711
17