From d2aaca74600ea6f8c748c033f57f7961ee63bb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 7 Dec 2015 13:55:14 +0100 Subject: [PATCH] Change: correctly rectify no-change Modify `Change.rectify()` to not make any changes to the Change instance (most notably to not modify the `checkpoint` field) when the tracked model instance was not changed. This should improve the performance of change replication as it reduces the number of unnecessary replications. For example, before this commit, every run of `rectifyAll` would trigger a full sync of all clients, because all change instances would be moved to the current checkpoint. --- common/models/change.js | 3 ++- test/change.test.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/common/models/change.js b/common/models/change.js index daac4054..a591d7d5 100644 --- a/common/models/change.js +++ b/common/models/change.js @@ -192,7 +192,8 @@ module.exports = function(Change) { if (rev) { // avoid setting rev and prev to the same value if (currentRev === rev) { - change.debug('rev and prev are equal (not updating rev)'); + change.debug('rev and prev are equal (not updating anything)'); + return cb(null, change); } else { change.rev = rev; change.debug('updated revision (was ' + currentRev + ')'); diff --git a/test/change.test.js b/test/change.test.js index 02a059df..55d551c7 100644 --- a/test/change.test.js +++ b/test/change.test.js @@ -200,6 +200,23 @@ describe('Change', function() { }); } }); + + it('should not change checkpoint when rev is the same', function(done) { + var test = this; + var originalCheckpoint = change.checkpoint; + var originalRev = change.rev; + + TestModel.checkpoint(function(err, inst) { + if (err) return done(err); + + change.rectify(function(err, c) { + if (err) return done(err); + expect(c.rev, 'rev').to.equal(originalRev); // sanity check + expect(c.checkpoint, 'checkpoint').to.equal(originalCheckpoint); + done(); + }); + }); + }); }); describe('change.currentRevision(callback)', function() {