From b94148545072ae9b683cbb1dd7ac13c66e78c06b Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 29 May 2013 19:19:31 +0200 Subject: [PATCH] ensureIndex init works right after a database load --- benchmarks/commonUtilities.js | 2 +- lib/datastore.js | 3 ++- test/db.test.js | 40 +++++++++++++++++++++++++++++++++++ test/indexes.test.js | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/benchmarks/commonUtilities.js b/benchmarks/commonUtilities.js index d4461f4..5ba0c62 100644 --- a/benchmarks/commonUtilities.js +++ b/benchmarks/commonUtilities.js @@ -179,7 +179,7 @@ module.exports.removeDocs = function (options, d, n, profiler, cb) { function runFrom(i) { if (i === n) { // Finished - console.log("Average time for one remove in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms"); + console.log("Average time for one remove and one insert in a collection of " + n + " docs: " + (profiler.elapsedSinceLastStep() / n) + "ms"); profiler.step('Finished removing ' + n + ' docs'); return cb(); } diff --git a/lib/datastore.js b/lib/datastore.js index c7369bc..cc0499b 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -407,7 +407,7 @@ Datastore.prototype.update = function () { Datastore.prototype._remove = function (query, options, cb) { var callback , self = this - , candidates = this.getCandidates(query) + //, candidates = this.getCandidates(query) , numRemoved = 0 , multi , newData = [] @@ -423,6 +423,7 @@ Datastore.prototype._remove = function (query, options, cb) { if (model.match(d, query) && (multi || numRemoved === 0)) { numRemoved += 1; removedDocs.push({ $$deleted: true, _id: d._id }); + self.removeFromIndexes(d); } else { newData.push(d); } diff --git a/test/db.test.js b/test/db.test.js index 95bff89..6cf4956 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1107,4 +1107,44 @@ describe('Database', function () { }); // ==== End of 'Remove' ==== // + describe.only('Using indexes', function () { + + describe('ensureIndex', function () { + + it('ensureIndex can be called right after a loadDatabase and be initialized and filled correctly', 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); + + assert.deepEqual(d.indexes, {}); + + d.ensureIndex({ fieldName: '_id' }); + d.indexes._id.fieldName.should.equal('_id'); + d.indexes._id.unique.should.equal(false); + d.indexes._id.sparse.should.equal(false); + d.indexes._id.tree.getNumberOfKeys().should.equal(3); + d.indexes._id.tree.search('1')[0].should.equal(d.data[0]); + d.indexes._id.tree.search('2')[0].should.equal(d.data[1]); + d.indexes._id.tree.search('3')[0].should.equal(d.data[2]); + + done(); + }); + }); + }); + + }); + + }); // ==== End of 'Using indexes' ==== // + + }); diff --git a/test/indexes.test.js b/test/indexes.test.js index e59d739..01b100f 100644 --- a/test/indexes.test.js +++ b/test/indexes.test.js @@ -7,7 +7,7 @@ var Index = require('../lib/indexes') , model = require('../lib/model') ; -describe('Indexing', function () { +describe('Indexes', function () { describe('Insertion', function () {