From 08804dde828b64d998e252bbd24d677b0f7336dc Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 30 May 2013 15:58:07 +0200 Subject: [PATCH] Still some problems to solve with indexing --- lib/datastore.js | 8 ++++-- test/db.test.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index fbda4fe..ee02bda 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -419,13 +419,17 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { }); } , function () { // Perform the update - candidates = self.getCandidates(query) + var modified; + + candidates = self.getCandidates(query); try { for (i = 0; i < candidates.length; i += 1) { if (model.match(candidates[i], query) && (multi || numReplaced === 0)) { numReplaced += 1; - candidates[i] = model.modify(candidates[i], updateQuery); + modified = model.modify(candidates[i], updateQuery); + self.updateIndexes(candidates[i], modified); + candidates[i] = modified; updatedDocs.push(candidates[i]); } } diff --git a/test/db.test.js b/test/db.test.js index 55fe14b..b57cf68 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1416,8 +1416,74 @@ describe('Database', function () { }); }); + it.skip('Insertion still works as before with indexing', function (done) { + d.ensureIndex({ fieldName: 'a' }); + d.ensureIndex({ fieldName: 'b' }); + + d.insert({ a: 1, b: 'hello' }, function (err, doc1) { + d.insert({ a: 2, b: 'si' }, function (err, doc2) { + d.find({}, function (err, docs) { + console.log(d.data); +// TODO + done(); + }); + }); + }); + }); + }); // ==== End of 'Indexing newly inserted documents' ==== // + describe('Updating indexes upon document update', function () { + + it.skip('Indexes get updated when a document (or multiple documents) is updated', function (done) { + d.ensureIndex({ fieldName: 'a' }); + d.ensureIndex({ fieldName: 'b' }); + + d.insert({ a: 1, b: 'hello' }, function (err, doc1) { + d.insert({ a: 2, b: 'si' }, function (err, doc2) { + d.update({ a: 1 }, { $set: { a: 456, b: 'no' } }, {}, function (err, nr) { + assert.isNull(err); + nr.should.equal(1); + console.log(d.data); + throw 'fds'; // TODO + + d.indexes.a.tree.getNumberOfKeys().should.equal(2); + d.indexes.a.getMatching(456)[0]._id.should.equal(doc1._id); + d.indexes.a.getMatching(2)[0]._id.should.equal(doc2._id); + + d.indexes.b.tree.getNumberOfKeys().should.equal(2); + d.indexes.b.getMatching('no')[0]._id.should.equal(doc1._id); + d.indexes.b.getMatching('si')[0]._id.should.equal(doc2._id); + + console.log("========================"); + console.log("========================"); + d.update({}, { $inc: { a: 10 }, $set: { b: 'same' } }, { multi: true }, function () { + assert.isNull(err); + nr.should.equal(2); + + d.indexes.a.tree.getNumberOfKeys().should.equal(2); + d.indexes.a.getMatching(466)[0]._id.should.equal(doc1._id); + d.indexes.a.getMatching(12)[0]._id.should.equal(doc2._id); + + d.indexes.b.tree.getNumberOfKeys().should.equal(1); + d.indexes.b.getMatching('same')[0]._id.should.equal(doc1._id); + d.indexes.b.getMatching('same')[1]._id.should.equal(doc2._id); + + done(); + }); + }); + }); + }); + }); + + it.skip('If an update violates a contraints, nothing is done', function (done) { + }); + + it.skip('Updates still work with indexing', function (done) { + }); + + }); // ==== End of 'Updating indexes upon document update' ==== // +