nedb
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -6,3 +6,3 @@ var fs = require('fs') | ||
, async = require('async') | ||
, executor = require('./executor') | ||
, Executor = require('./executor') | ||
; | ||
@@ -17,2 +17,7 @@ | ||
this.data = []; | ||
this.executor = new Executor(); | ||
// We keep internally the number of lines in the datafile | ||
// This will be used when/if I implement autocompacting when the datafile grows too big | ||
this.datafileSize = 0; | ||
} | ||
@@ -39,2 +44,3 @@ | ||
self.data = []; | ||
self.datafileSize = 0; | ||
fs.writeFile(self.filename, '', 'utf8', function (err) { return callback(err); }); | ||
@@ -45,2 +51,3 @@ } else { | ||
self.data = Datastore.treatRawData(rawData); | ||
self.datafileSize = self.data.length; | ||
self.persistCachedDatabase(callback); | ||
@@ -54,3 +61,3 @@ }); | ||
Datastore.prototype.loadDatabase = function () { | ||
executor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); | ||
this.executor.push({ this: this, fn: this._loadDatabase, arguments: arguments }); | ||
}; | ||
@@ -138,4 +145,4 @@ | ||
var insertedDoc = model.deserialize(persistableNewDoc); | ||
self.data.push(insertedDoc); // Make sure the doc is the same on the disk and in memory | ||
// Some docs can't be stringified correctly | ||
self.data.push(insertedDoc); | ||
self.datafileSize += 1; | ||
return callback(null, insertedDoc); | ||
@@ -146,3 +153,3 @@ }); | ||
Datastore.prototype.insert = function () { | ||
executor.push({ this: this, fn: this._insert, arguments: arguments }); | ||
this.executor.push({ this: this, fn: this._insert, arguments: arguments }); | ||
}; | ||
@@ -210,2 +217,4 @@ | ||
self.datafileSize += newDocs.length; | ||
newDocs.forEach(function (doc) { | ||
@@ -288,3 +297,3 @@ toPersist += model.serialize(doc) + '\n'; | ||
Datastore.prototype.update = function () { | ||
executor.push({ this: this, fn: this._update, arguments: arguments }); | ||
this.executor.push({ this: this, fn: this._update, arguments: arguments }); | ||
}; | ||
@@ -336,3 +345,3 @@ | ||
Datastore.prototype.remove = function () { | ||
executor.push({ this: this, fn: this._remove, arguments: arguments }); | ||
this.executor.push({ this: this, fn: this._remove, arguments: arguments }); | ||
}; | ||
@@ -339,0 +348,0 @@ |
@@ -8,32 +8,39 @@ /** | ||
var async = require('async') | ||
, queue | ||
; | ||
executor = async.queue(function (task, cb) { | ||
var callback | ||
, lastArg = task.arguments[task.arguments.length - 1] | ||
; | ||
function Executor () { | ||
this.queue = async.queue(function (task, cb) { | ||
var callback | ||
, lastArg = task.arguments[task.arguments.length - 1] | ||
; | ||
// Always tell the queue task is complete. Execute callback if any was given. | ||
if (typeof lastArg === 'function') { | ||
callback = function () { | ||
lastArg.apply(null, arguments); | ||
cb(); | ||
}; | ||
// Always tell the queue task is complete. Execute callback if any was given. | ||
if (typeof lastArg === 'function') { | ||
callback = function () { | ||
lastArg.apply(null, arguments); | ||
cb(); | ||
}; | ||
task.arguments[task.arguments.length - 1] = callback; | ||
} else { | ||
callback = function () { | ||
cb(); | ||
}; | ||
task.arguments[task.arguments.length - 1] = callback; | ||
} else { | ||
callback = function () { | ||
cb(); | ||
}; | ||
task.arguments.push(callback); | ||
} | ||
task.arguments.push(callback); | ||
} | ||
task.fn.apply(task.this, task.arguments); | ||
}, 1); | ||
task.fn.apply(task.this, task.arguments); | ||
}, 1); | ||
} | ||
Executor.prototype.push = function () { | ||
this.queue.push.apply(this, arguments); | ||
}; | ||
// Interface | ||
module.exports = executor; | ||
module.exports = Executor; |
{ | ||
"name": "nedb", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "tldr.io", |
@@ -31,2 +31,4 @@ var Datastore = require('../lib/datastore') | ||
assert.isNull(err); | ||
d.datafileSize.should.equal(0); | ||
d.data.length.should.equal(0); | ||
return cb(); | ||
@@ -156,2 +158,30 @@ }); | ||
it('datafileSize is the size of the dataset upon a databaseLoad', function (done) { | ||
var now = new Date() | ||
, rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + | ||
model.serialize({ _id: "2", hello: 'world' }) + '\n' + | ||
model.serialize({ _id: "3", nested: { today: now } }) | ||
; | ||
d.data.length.should.equal(0); | ||
d.datafileSize.should.equal(0); | ||
fs.writeFile(testDb, rawData, 'utf8', function () { | ||
d.loadDatabase(function () { | ||
d.data.length.should.equal(3); | ||
d.datafileSize.should.equal(3); | ||
d.find({}, function (err, docs) { | ||
docs.sort(function (a, b) { return a._id - b._id; }); | ||
docs.length.should.equal(3); | ||
_.isEqual(docs[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); | ||
_.isEqual(docs[1], { _id: "2", hello: 'world' }).should.equal(true); | ||
_.isEqual(docs[2], { _id: "3", nested: { today: now } }).should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); // ==== End of 'Loading the database data from file and persistence' ==== // | ||
@@ -275,2 +305,20 @@ | ||
it('datafileSize is incremented by 1 upon every insert', function (done) { | ||
d.datafileSize.should.equal(0); | ||
d.data.length.should.equal(0); | ||
d.insert({ a: 3 }, function () { | ||
d.datafileSize.should.equal(1); | ||
d.data.length.should.equal(1); | ||
d.insert({ a: 3 }, function () { | ||
d.datafileSize.should.equal(2); | ||
d.data.length.should.equal(2); | ||
d.insert({ a: 3 }, function () { | ||
d.datafileSize.should.equal(3); | ||
d.data.length.should.equal(3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); // ==== End of 'Insert' ==== // | ||
@@ -823,2 +871,25 @@ | ||
it('datafileSize stays correct upon updates', function (done) { | ||
d.insert({ a: 2 }, function () { | ||
d.insert({ a: 3 }, function () { | ||
d.insert({ a: 5 }, function () { | ||
d.datafileSize.should.equal(3); | ||
d.data.length.should.equal(3); | ||
d.update({ a: 3 }, { $set: { a: 4 } }, {}, function () { | ||
d.datafileSize.should.equal(4); | ||
d.data.length.should.equal(3); | ||
d.update({ a: { $in: [2, 4] } }, { $set: { a: 5 } }, { multi: true }, function () { | ||
d.datafileSize.should.equal(6); | ||
d.data.length.should.equal(3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); // ==== End of 'Update' ==== // | ||
@@ -973,2 +1044,25 @@ | ||
it('datafileSize stays correct upon removes', function (done) { | ||
d.insert({ a: 2 }, function () { | ||
d.insert({ a: 3 }, function () { | ||
d.insert({ a: 5 }, function () { | ||
d.datafileSize.should.equal(3); | ||
d.data.length.should.equal(3); | ||
d.remove({ a: 3 }, {}, function () { | ||
d.datafileSize.should.equal(4); | ||
d.data.length.should.equal(2); | ||
d.remove({ a: { $in: [2, 5] } }, { multi: true }, function () { | ||
d.datafileSize.should.equal(6); | ||
d.data.length.should.equal(0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); // ==== End of 'Remove' ==== // | ||
@@ -975,0 +1069,0 @@ |
113140
2426