Skip to content

Commit

Permalink
Created event compaction.done fired by datastore after a compaction o…
Browse files Browse the repository at this point in the history
…peration is complete
  • Loading branch information
louischatriot committed Jan 17, 2016
1 parent d92ccb9 commit 20eceff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/datastore.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/persistence.js
Expand Up @@ -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);
});
};


Expand Down
11 changes: 10 additions & 1 deletion test/persistence.test.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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"; }
Expand Down

0 comments on commit 20eceff

Please sign in to comment.