Remember an index upon any reload

pull/2/head
Louis Chatriot 11 years ago
parent 34a3ba2046
commit 878082f38d
  1. 7
      lib/persistence.js
  2. 13
      test/db.test.js
  3. 18
      test/persistence.test.js

@ -104,7 +104,12 @@ Persistence.prototype.persistCachedDatabase = function (cb) {
this.db.getAllData().forEach(function (doc) { this.db.getAllData().forEach(function (doc) {
toPersist += model.serialize(doc) + '\n'; 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.waterfall([
async.apply(customUtils.ensureFileDoesntExist, self.tempFilename) async.apply(customUtils.ensureFileDoesntExist, self.tempFilename)
, async.apply(customUtils.ensureFileDoesntExist, self.oldFilename) , async.apply(customUtils.ensureFileDoesntExist, self.oldFilename)

@ -2009,7 +2009,7 @@ describe('Database', function () {
}); // ==== End of 'Removing indexes upon document update' ==== // }); // ==== 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) { 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" var persDb = "workspace/persistIndexes.db"
@ -2043,8 +2043,19 @@ describe('Database', function () {
Object.keys(db.indexes)[1].should.equal("planet"); Object.keys(db.indexes)[1].should.equal("planet");
db.indexes._id.getAll().length.should.equal(2); db.indexes._id.getAll().length.should.equal(2);
db.indexes.planet.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(); done();
});
}); });
}); });
}); });

@ -126,6 +126,24 @@ describe('Persistence', function () {
_.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true);
_.isEqual(treatedData[1], { _id: "3", today: now }).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) { it('Compact database on load', function (done) {
d.insert({ a: 2 }, function () { d.insert({ a: 2 }, function () {

Loading…
Cancel
Save