Skip to content

Commit

Permalink
Benchmark for findOne usable with indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed May 29, 2013
1 parent 62cbbe2 commit bae0de0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
21 changes: 18 additions & 3 deletions benchmarks/findOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 28 additions & 3 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bae0de0

Please sign in to comment.