From 878082f38d7b093cef467f02bd0b8eedcd397d17 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 7 Dec 2013 21:42:03 -0800 Subject: [PATCH] Remember an index upon any reload --- lib/persistence.js | 7 ++++++- test/db.test.js | 13 ++++++++++++- test/persistence.test.js | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/persistence.js b/lib/persistence.js index 5a6d823..147f65f 100644 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -104,7 +104,12 @@ Persistence.prototype.persistCachedDatabase = function (cb) { this.db.getAllData().forEach(function (doc) { toPersist += model.serialize(doc) + '\n'; }); - + Object.keys(this.db.indexes).forEach(function (fieldName) { + if (fieldName != "_id") { // The special _id index is managed by datastore.js, the others need to be persisted + toPersist += model.serialize({ $$indexCreated: { fieldName: fieldName, unique: self.db.indexes[fieldName].unique, sparse: self.db.indexes[fieldName].sparse }}) + '\n'; + } + }); + async.waterfall([ async.apply(customUtils.ensureFileDoesntExist, self.tempFilename) , async.apply(customUtils.ensureFileDoesntExist, self.oldFilename) diff --git a/test/db.test.js b/test/db.test.js index fe60d74..7cb2326 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -2009,7 +2009,7 @@ describe('Database', function () { }); // ==== End of 'Removing indexes upon document update' ==== // - describe.skip('Persisting indexes', function () { + describe.only('Persisting indexes', function () { it('If the persistIndexes options is used, indexes are persisted to a separate file and recreated upon reload', function (done) { var persDb = "workspace/persistIndexes.db" @@ -2043,8 +2043,19 @@ describe('Database', function () { Object.keys(db.indexes)[1].should.equal("planet"); db.indexes._id.getAll().length.should.equal(2); db.indexes.planet.getAll().length.should.equal(2); + + // After another reload the indexes are still there (i.e. they are preserved during autocompaction) + db = new Datastore({ filename: persDb }); + db.loadDatabase(function (err) { + assert.isNull(err); + Object.keys(db.indexes).length.should.equal(2); + Object.keys(db.indexes)[0].should.equal("_id"); + Object.keys(db.indexes)[1].should.equal("planet"); + db.indexes._id.getAll().length.should.equal(2); + db.indexes.planet.getAll().length.should.equal(2); done(); + }); }); }); }); diff --git a/test/persistence.test.js b/test/persistence.test.js index 402be2f..0f1b73e 100644 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -126,6 +126,24 @@ describe('Persistence', function () { _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); _.isEqual(treatedData[1], { _id: "3", today: now }).should.equal(true); }); + + it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () { + var now = new Date() + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + + model.serialize({ $$indexCreated: { fieldName: "test", unique: true } }) + '\n' + + model.serialize({ _id: "3", today: now }) + , treatedData = Persistence.treatRawData(rawData).data + , indexes = Persistence.treatRawData(rawData).indexes + ; + + Object.keys(indexes).length.should.equal(1); + assert.deepEqual(indexes.test, { fieldName: "test", unique: true }); + + treatedData.sort(function (a, b) { return a._id - b._id; }); + treatedData.length.should.equal(2); + _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); + _.isEqual(treatedData[1], { _id: "3", today: now }).should.equal(true); + }); it('Compact database on load', function (done) { d.insert({ a: 2 }, function () {