diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index 7224ef5..d4461f4 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -154,7 +154,8 @@ module.exports.updateDocs = function (options, d, n, profiler, cb) { return cb(); } - d.update({ docNumber: order[i] }, { newDocNumber: i }, options, function (err, nr) { + // Will not actually modify the document but will take the same time + d.update({ docNumber: order[i] }, { docNumber: order[i] }, options, function (err, nr) { if (nr !== 1) { return cb('One update didnt work'); } executeAsap(function () { runFrom(i + 1); diff --git a/benchmarks/insert.js b/benchmarks/insert.js index e10797c..86ba8ce 100644 --- a/benchmarks/insert.js +++ b/benchmarks/insert.js @@ -21,7 +21,6 @@ console.log("Test with " + n + " documents"); console.log(program.withIndex ? "Use an index" : "Don't use an index"); console.log("----------------------------"); - async.waterfall([ async.apply(commonUtilities.prepareDb, benchDb) , function (cb) { diff --git a/benchmarks/remove.js b/benchmarks/remove.js index b07eda0..4c8f5d4 100644 --- a/benchmarks/remove.js +++ b/benchmarks/remove.js @@ -7,14 +7,33 @@ var Datastore = require('../lib/datastore') , execTime = require('exec-time') , profiler = new execTime('REMOVE BENCH') , d = new Datastore(benchDb) - , n = 10000 + , program = require('commander') + , n ; -if (process.argv[2]) { n = parseInt(process.argv[2], 10); } +program + .option('-n --number [number]', 'Size of the collection to test on', parseInt) + .option('-i --with-index', 'Test with an index') + .parse(process.argv); + +n = program.number || 10000; + +console.log("----------------------------"); +console.log("Test with " + n + " documents"); +console.log(program.withIndex ? "Use an index" : "Don't use an index"); +console.log("----------------------------"); async.waterfall([ async.apply(commonUtilities.prepareDb, benchDb) -, function (cb) { d.loadDatabase(cb); } +, function (cb) { + d.loadDatabase(function (err) { + if (err) { return cb(err); } + if (program.withIndex) { + d.ensureIndex({ fieldName: 'docNumber' }); + } + cb(); + }); + } , function (cb) { profiler.beginProfiling(); return cb(); } , async.apply(commonUtilities.insertDocs, d, n, profiler) diff --git a/benchmarks/update.js b/benchmarks/update.js index de71348..e27e009 100644 --- a/benchmarks/update.js +++ b/benchmarks/update.js @@ -7,14 +7,33 @@ var Datastore = require('../lib/datastore') , execTime = require('exec-time') , profiler = new execTime('UPDATE BENCH') , d = new Datastore(benchDb) - , n = 10000 + , program = require('commander') + , n ; -if (process.argv[2]) { n = parseInt(process.argv[2], 10); } +program + .option('-n --number [number]', 'Size of the collection to test on', parseInt) + .option('-i --with-index', 'Test with an index') + .parse(process.argv); + +n = program.number || 10000; + +console.log("----------------------------"); +console.log("Test with " + n + " documents"); +console.log(program.withIndex ? "Use an index" : "Don't use an index"); +console.log("----------------------------"); async.waterfall([ async.apply(commonUtilities.prepareDb, benchDb) -, function (cb) { d.loadDatabase(cb); } +, function (cb) { + d.loadDatabase(function (err) { + if (err) { return cb(err); } + if (program.withIndex) { + d.ensureIndex({ fieldName: 'docNumber' }); + } + cb(); + }); + } , function (cb) { profiler.beginProfiling(); return cb(); } , async.apply(commonUtilities.insertDocs, d, n, profiler) diff --git a/lib/datastore.js b/lib/datastore.js index c2188b0..c7369bc 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -340,6 +340,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { , numReplaced = 0 , multi, upsert , updatedDocs = [] + , candidates , i ; @@ -367,12 +368,14 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { }); } , function () { // Perform the update + candidates = self.getCandidates(query) + try { - for (i = 0; i < self.data.length; i += 1) { - if (model.match(self.data[i], query) && (multi || numReplaced === 0)) { + for (i = 0; i < candidates.length; i += 1) { + if (model.match(candidates[i], query) && (multi || numReplaced === 0)) { numReplaced += 1; - self.data[i] = model.modify(self.data[i], updateQuery); - updatedDocs.push(self.data[i]); + candidates[i] = model.modify(candidates[i], updateQuery); + updatedDocs.push(candidates[i]); } } } catch (err) { @@ -404,6 +407,7 @@ Datastore.prototype.update = function () { Datastore.prototype._remove = function (query, options, cb) { var callback , self = this + , candidates = this.getCandidates(query) , numRemoved = 0 , multi , newData = []