Comparing version 0.3.4 to 0.3.5
@@ -187,4 +187,30 @@ /** | ||
/** | ||
* Load database | ||
*/ | ||
module.exports.loadDatabase = function (d, n, profiler, cb) { | ||
var beg = new Date() | ||
, order = getRandomArray(n) | ||
; | ||
profiler.step("Loading the database " + n + " times"); | ||
function runFrom(i) { | ||
if (i === n) { // Finished | ||
console.log("Average time for one loadDatabse for a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms"); | ||
profiler.step('Finished loading a database' + n + ' times'); | ||
return cb(); | ||
} | ||
d.loadDatabase(function (err) { | ||
process.nextTick(function () { | ||
runFrom(i + 1); | ||
}); | ||
}); | ||
} | ||
runFrom(0); | ||
}; | ||
/** | ||
* The datastore itself | ||
* TODO | ||
* Queue operations | ||
* Update and removes should only modify the corresponding part of the database | ||
* Update and removes should only modify the corresponding part of the database (or use much faster append-only format) | ||
*/ | ||
@@ -13,2 +12,3 @@ | ||
, async = require('async') | ||
, executor = require('./executor') | ||
; | ||
@@ -80,3 +80,3 @@ | ||
*/ | ||
Datastore.prototype.insert = function (newDoc, cb) { | ||
Datastore.prototype._insert = function (newDoc, cb) { | ||
var callback = cb || function () {} | ||
@@ -104,3 +104,7 @@ , self = this | ||
Datastore.prototype.insert = function () { | ||
executor.push({ this: this, fn: this._insert, arguments: arguments }); | ||
}; | ||
/** | ||
@@ -184,3 +188,3 @@ * Find all documents matching the query | ||
*/ | ||
Datastore.prototype.update = function (query, updateQuery, options, cb) { | ||
Datastore.prototype._update = function (query, updateQuery, options, cb) { | ||
var callback | ||
@@ -208,3 +212,3 @@ , self = this | ||
// documents), modified by the updateQuery | ||
return self.insert(model.modify(query, updateQuery), function (err) { | ||
return self._insert(model.modify(query, updateQuery), function (err) { | ||
if (err) { return callback(err); } | ||
@@ -238,2 +242,5 @@ return callback(null, 1, true); | ||
}; | ||
Datastore.prototype.update = function () { | ||
executor.push({ this: this, fn: this._update, arguments: arguments }); | ||
}; | ||
@@ -249,3 +256,3 @@ | ||
*/ | ||
Datastore.prototype.remove = function (query, options, cb) { | ||
Datastore.prototype._remove = function (query, options, cb) { | ||
var callback | ||
@@ -275,4 +282,7 @@ , self = this | ||
}; | ||
Datastore.prototype.remove = function () { | ||
executor.push({ this: this, fn: this._remove, arguments: arguments }); | ||
}; | ||
module.exports = Datastore; |
{ | ||
"name": "nedb", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "tldr.io", |
@@ -67,3 +67,3 @@ # NeDB (Node embedded database) | ||
// { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false } | ||
// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true } | ||
// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true, humans: { genders: 2 } } | ||
// { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false } | ||
@@ -83,2 +83,7 @@ // { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true } | ||
// Use the dot-notation to match fields in subdocuments | ||
db.find({ "humans.genders": 2 }, function (err, docs) { | ||
// docs contains Earth | ||
}); | ||
db.find({}, function (err, docs) { | ||
@@ -88,2 +93,3 @@ // docs contains all documents in the collection | ||
// The same rules apply when you want to only find one document | ||
db.findOne({ _id: 'id1' }, function (err, doc) { | ||
@@ -196,3 +202,3 @@ // doc is the document _id1 | ||
You can run the simple benchmarks I use by executing the scripts in the `benchmarks` folder. They all take an optional parameter which is the size of the dataset to use (default is 10,000). Most of the time spent during update and remove operations is IO, and I will work on optimizing this in the future. | ||
You can run the simple benchmarks I use by executing the scripts in the `benchmarks` folder. They all take an optional parameter which is the size of the dataset to use (default is 10,000). Most of the time spent during update and remove operations is IO, and I will work on optimizing this in the future (probably by switching to an append-only format). | ||
@@ -199,0 +205,0 @@ ### Memory footprint |
@@ -570,26 +570,25 @@ var Datastore = require('../lib/datastore') | ||
// This tests concurrency issues | ||
// Right now, it doesn't pass, because I need to | ||
//it.only('Remove can be called multiple times in parallel and everything that needs to be removed will be', function (done) { | ||
//d.insert({ planet: 'Earth' }, function () { | ||
//d.insert({ planet: 'Mars' }, function () { | ||
//d.insert({ planet: 'Saturn' }, function () { | ||
//d.find({}, function (err, docs) { | ||
//docs.length.should.equal(3); | ||
it('Remove can be called multiple times in parallel and everything that needs to be removed will be', function (done) { | ||
d.insert({ planet: 'Earth' }, function () { | ||
d.insert({ planet: 'Mars' }, function () { | ||
d.insert({ planet: 'Saturn' }, function () { | ||
d.find({}, function (err, docs) { | ||
docs.length.should.equal(3); | ||
//// Remove two docs simultaneously | ||
//var toRemove = ['Mars', 'Saturn']; | ||
//async.each(toRemove, function(planet, cb) { | ||
//d.remove({ planet: planet }, function (err) { return cb(err); }); | ||
//}, function (err) { | ||
//d.find({}, function (err, docs) { | ||
//docs.length.should.equal(1); | ||
// Remove two docs simultaneously | ||
var toRemove = ['Mars', 'Saturn']; | ||
async.each(toRemove, function(planet, cb) { | ||
d.remove({ planet: planet }, function (err) { return cb(err); }); | ||
}, function (err) { | ||
d.find({}, function (err, docs) { | ||
docs.length.should.equal(1); | ||
//done(); | ||
//}); | ||
//}); | ||
//}); | ||
//}); | ||
//}); | ||
//}); | ||
//}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -596,0 +595,0 @@ }); // ==== End of 'Remove' ==== // |
71481
18
1620
240
9