diff --git a/benchmarks/findOne.js b/benchmarks/findOne.js index d5248e1..64ec3d9 100644 --- a/benchmarks/findOne.js +++ b/benchmarks/findOne.js @@ -7,15 +7,30 @@ var Datastore = require('../lib/datastore') , execTime = require('exec-time') , profiler = new execTime('FINDONE 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); + 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 0814f6e..c2188b0 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -65,6 +65,30 @@ Datastore.prototype.addToIndexes = function (doc) { }; +/** + * Remove one or several document(s) from all indexes + */ +Datastore.prototype.removeFromIndexes = function (doc) { + var self = this; + + Object.keys(this.indexes).forEach(function (i) { + self.indexes[i].remove(doc); + }); +}; + + +/** + * Update a document in all indexes + */ +Datastore.prototype.removeFromIndexes = function (doc, newDoc) { + var self = this; + + Object.keys(this.indexes).forEach(function (i) { + self.indexes[i].update(doc, newDoc); + }); +}; + + /** * Return the list of candidates for a given query * Very crude implementation for now, we return the candidates given by the first usable index if any @@ -254,13 +278,14 @@ Datastore.prototype.find = function (query, callback) { */ Datastore.prototype.findOne = function (query, callback) { var self = this + , candidates = this.getCandidates(query) , i ; try { - for (i = 0; i < self.data.length; i += 1) { - if (model.match(self.data[i], query)) { - return callback(null, model.deepCopy(self.data[i])); + for (i = 0; i < candidates.length; i += 1) { + if (model.match(candidates[i], query)) { + return callback(null, model.deepCopy(candidates[i])); } } } catch (err) {