From 5153ed82171ce62f85502518418207ff10d321e5 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 15 Jan 2016 15:02:09 +0000 Subject: [PATCH] make peekLastUpdate alway return lastVersion when available --- .../app/coffee/MongoManager.coffee | 20 +++++++++++-------- .../app/coffee/UpdatesManager.coffee | 16 ++++++--------- .../MongoManager/MongoManagerTests.coffee | 8 ++++++-- .../UpdatesManager/UpdatesManagerTests.coffee | 2 +- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/services/track-changes/app/coffee/MongoManager.coffee b/services/track-changes/app/coffee/MongoManager.coffee index bee0a5ac67..83b89bce34 100644 --- a/services/track-changes/app/coffee/MongoManager.coffee +++ b/services/track-changes/app/coffee/MongoManager.coffee @@ -18,7 +18,7 @@ module.exports = MongoManager = peekLastCompressedUpdate: (doc_id, callback = (error, update, version) ->) -> # under normal use we pass back the last update as - # callback(null,update). + # callback(null,update,version). # # when we have an existing last update but want to force a new one # to start, we pass it back as callback(null,null,version), just @@ -26,17 +26,18 @@ module.exports = MongoManager = MongoManager.getLastCompressedUpdate doc_id, (error, update) -> return callback(error) if error? if update? - if update.inS3? - # we want to force a new update, but ensure that it is - # consistent with the version of the existing one in S3 - return callback null, null, update.v - else if update.broken + if update.broken # the update is marked as broken so we will force a new op return callback null, null + else if update.pack? + return callback null, update, update.pack[0]?.v else - return callback null, update + return callback null, update, update.v else - callback null, null + MongoManager.getArchivedDocStatus doc_id, (error, status) -> + return callback(error) if error? + return callback(null, null, status.lastVersion) if status?.inS3? and status?.lastVersion? + callback null, null insertCompressedUpdates: (project_id, doc_id, updates, temporary, callback = (error) ->) -> jobs = [] @@ -152,6 +153,9 @@ module.exports = MongoManager = db.docHistoryStats.ensureIndex { doc_id: 1 }, { background: true } db.docHistoryStats.ensureIndex { updates: -1, doc_id: 1 }, { background: true } + getArchivedDocStatus: (doc_id, callback)-> + db.docHistoryStats.findOne {doc_id: ObjectId(doc_id.toString()), inS3: {$exists:true}}, {inS3: true, lastVersion: true}, callback + getDocChangesCount: (doc_id, callback)-> db.docHistory.count { doc_id : ObjectId(doc_id.toString()), inS3 : { $exists : false }}, callback diff --git a/services/track-changes/app/coffee/UpdatesManager.coffee b/services/track-changes/app/coffee/UpdatesManager.coffee index c52d920cb7..f40a7c8503 100644 --- a/services/track-changes/app/coffee/UpdatesManager.coffee +++ b/services/track-changes/app/coffee/UpdatesManager.coffee @@ -17,17 +17,13 @@ module.exports = UpdatesManager = return callback() MongoManager.peekLastCompressedUpdate doc_id, (error, lastCompressedUpdate, lastVersion) -> - # lastCompressedUpdate is the most recent update in Mongo. + # lastCompressedUpdate is the most recent update in Mongo, and + # lastVersion is its sharejs version number. # - # The peekLastCompressedUpdate method may pass it back as 'null' - # to force the start of a new compressed update, even when there - # was a previous compressed update in Mongo. In this case it - # passes back the lastVersion from the update to check - # consistency. - - # when lastVersion is not provided, default to lastCompressedUpdate.v - lastVersion ?= lastCompressedUpdate?.v - + # The peekLastCompressedUpdate method may pass the update back + # as 'null' (for example if the previous compressed update has + # been archived). In this case it can still pass back the + # lastVersion from the update to allow us to check consistency. return callback(error) if error? # Ensure that raw updates start where lastVersion left off diff --git a/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee b/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee index 2cdc7c8680..300357e406 100644 --- a/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee @@ -57,6 +57,8 @@ describe "MongoManager", -> describe "peekLastCompressedUpdate", -> describe "when there is no last update", -> beforeEach -> + @db.docHistoryStats = {} + @db.docHistoryStats.findOne = sinon.stub().callsArgWith(2, null, null) @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null) @MongoManager.peekLastCompressedUpdate @doc_id, @callback @@ -84,8 +86,10 @@ describe "MongoManager", -> describe "when there is a last update in S3", -> beforeEach -> - @update = { _id: Object(), v: 12345, inS3: true } - @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update) + @update = { _id: Object(), v: 12345} + @db.docHistoryStats = {} + @db.docHistoryStats.findOne = sinon.stub().callsArgWith(2, null, {inS3:true, lastVersion: @update.v}) + @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null) @MongoManager.peekLastCompressedUpdate @doc_id, @callback it "should get the last update", -> diff --git a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee index 576fa76ef4..4fc85a5e19 100644 --- a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee @@ -69,7 +69,7 @@ describe "UpdatesManager", -> @lastCompressedUpdate = { v: 11, op: "compressed-op-11" } @compressedUpdates = [ { v: 12, op: "compressed-op-11+12" }, { v: 13, op: "compressed-op-12" } ] - @MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate) + @MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v) @MongoManager.modifyCompressedUpdate = sinon.stub().callsArg(2) @MongoManager.insertCompressedUpdates = sinon.stub().callsArg(4) @UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)