Comparing version 0.9.2 to 0.9.4
@@ -33,3 +33,3 @@ if (global.GENTLY) require = GENTLY.hijack(require); | ||
} else { | ||
if (!this._keys[key]) { | ||
if (this._keys.indexOf(key) === -1) { | ||
this._keys.push(key); | ||
@@ -69,7 +69,11 @@ } | ||
Dirty.prototype._load = function() { | ||
var self = this, buffer = '', length = 0; | ||
if (!this.path) { | ||
process.nextTick(function() { | ||
self.emit('load', 0); | ||
}); | ||
return; | ||
} | ||
var self = this, buffer = '', length = 0; | ||
this._readStream = fs.createReadStream(this.path, { | ||
@@ -91,3 +95,10 @@ encoding: 'utf-8', | ||
buffer += chunk; | ||
buffer = buffer.replace(/([^\n]+)\n/g, function(m, rowStr) { | ||
if (chunk.lastIndexOf('\n') == -1) return; | ||
var arr = buffer.split('\n'); | ||
buffer = arr.pop(); | ||
arr.forEach(function(rowStr) { | ||
if (!rowStr) { | ||
self.emit('error', new Error('Empty lines never appear in a healthy database')); | ||
return | ||
} | ||
try { | ||
@@ -110,5 +121,5 @@ var row = JSON.parse(rowStr); | ||
if (!(row.key in self._docs)) { | ||
self._keys.push(row.key); | ||
length++; | ||
} | ||
self._keys.push(row.key); | ||
self._docs[row.key] = row.val; | ||
@@ -132,14 +143,18 @@ } | ||
this._writeStream.on('drain', function() { | ||
self.flushing = false; | ||
if (!self._queue.length) { | ||
self.emit('drain'); | ||
} else { | ||
self._maybeFlush(); | ||
} | ||
self._writeDrain(); | ||
}); | ||
}; | ||
Dirty.prototype._writeDrain = function() { | ||
this.flushing = false; | ||
if (!this._queue.length) { | ||
this.emit('drain'); | ||
} else { | ||
this._maybeFlush(); | ||
} | ||
} | ||
Dirty.prototype._maybeFlush = function() { | ||
if (this.flushing || !this.path || !this._queue.length) { | ||
if (this.flushing || !this._queue.length) { | ||
return; | ||
@@ -161,2 +176,8 @@ } | ||
function callbacks(err, cbs) { | ||
while (cbs.length) { | ||
cbs.shift()(err); | ||
} | ||
} | ||
for (var i = 0; i < length; i++) { | ||
@@ -177,2 +198,10 @@ key = this._queue[i]; | ||
(function(cbs) { | ||
if (!self.path) { | ||
process.nextTick(function() { | ||
callbacks(null, cbs); | ||
self._writeDrain(); | ||
}); | ||
return; | ||
} | ||
self._writeStream.write(bundleStr, function(err) { | ||
@@ -184,5 +213,3 @@ if (!cbs.length && err) { | ||
while (cbs.length) { | ||
cbs.shift()(err); | ||
} | ||
callbacks(err, cbs); | ||
}); | ||
@@ -189,0 +216,0 @@ })(cbs); |
{ | ||
"name": "dirty", | ||
"description": "A tiny & fast key value store with append-only disk log. Ideal for apps with < 1 million records.", | ||
"version": "0.9.2", | ||
"dependencies": { | ||
"version": "0.9.4", | ||
"dependencies": {}, | ||
"main": "./lib/dirty", | ||
"devDependencies": { | ||
"gently": ">=0.8.0" | ||
}, | ||
"main": "./lib/dirty", | ||
"devDependencies": {}, | ||
"engines": { | ||
@@ -11,0 +11,0 @@ "node": "*" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32382
0
37
787
1
- Removedgently@>=0.8.0
- Removedgently@1.0.0(transitive)