Created event compaction.done fired by datastore after a compaction operation is complete

pull/2/head
Louis Chatriot 9 years ago
parent d92ccb96c9
commit 20eceff265
  1. 5
      lib/datastore.js
  2. 6
      lib/persistence.js
  3. 11
      test/persistence.test.js

@ -22,6 +22,9 @@ var customUtils = require('./customUtils')
* @param {Function} options.afterSerialization/options.beforeDeserialization Optional, serialization hooks
* @param {Number} options.corruptAlertThreshold Optional, threshold after which an alert is thrown if too much data is corrupt
* @param {Function} options.compareStrings Optional, string comparison function that overrides default for sorting
*
* Event Emitter - Events
* * compaction.done - Fired whenever a compaction operation was finished
*/
function Datastore (options) {
var filename;
@ -74,6 +77,8 @@ function Datastore (options) {
}); }
}
util.inherits(Datastore, require('events'));
/**
* Load the database from the datafile, and trigger the execution of buffered commands if any

@ -134,7 +134,11 @@ Persistence.prototype.persistCachedDatabase = function (cb) {
}
});
storage.crashSafeWriteFile(this.filename, toPersist, callback);
storage.crashSafeWriteFile(this.filename, toPersist, function (err) {
if (err) { return callback(err); }
self.db.emit('compaction.done');
return callback(null);
});
};

@ -144,7 +144,7 @@ describe('Persistence', function () {
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 () {
@ -299,6 +299,15 @@ describe('Persistence', function () {
});
});
it("Can listen to compaction events", function (done) {
d.on('compaction.done', function () {
d.removeAllListeners('compaction.done'); // Tidy up for next tests
done();
});
d.persistence.compactDatafile();
});
describe('Serialization hooks', function () {
var as = function (s) { return "before_" + s + "_after"; }

Loading…
Cancel
Save