From 9b8a2c3030fa0354d1a56fc7ccb6e9e3f9fd8462 Mon Sep 17 00:00:00 2001 From: Paulo Reis Date: Fri, 5 May 2017 15:12:06 +0100 Subject: [PATCH] Consolidate methods to accept changes. --- services/document-updater/app.coffee | 4 +-- .../app/coffee/DocumentManager.coffee | 32 ++++--------------- .../app/coffee/HttpController.coffee | 11 ++++--- .../app/coffee/RangesManager.coffee | 10 +----- 4 files changed, 15 insertions(+), 42 deletions(-) diff --git a/services/document-updater/app.coffee b/services/document-updater/app.coffee index 1672d1b3b5..9e96d2b859 100644 --- a/services/document-updater/app.coffee +++ b/services/document-updater/app.coffee @@ -48,8 +48,8 @@ app.post '/project/:project_id/doc/:doc_id/flush', HttpCont app.delete '/project/:project_id/doc/:doc_id', HttpController.flushAndDeleteDoc app.delete '/project/:project_id', HttpController.deleteProject app.post '/project/:project_id/flush', HttpController.flushProject -app.post '/project/:project_id/doc/:doc_id/change/:change_id/accept', HttpController.acceptChange -app.post '/project/:project_id/doc/:doc_id/change/accept', HttpController.bulkAcceptChanges +app.post '/project/:project_id/doc/:doc_id/change/:change_id/accept', HttpController.acceptChanges +app.post '/project/:project_id/doc/:doc_id/change/accept', HttpController.acceptChanges app.del '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment app.get '/total', (req, res)-> diff --git a/services/document-updater/app/coffee/DocumentManager.coffee b/services/document-updater/app/coffee/DocumentManager.coffee index a327f04af4..a608331003 100644 --- a/services/document-updater/app/coffee/DocumentManager.coffee +++ b/services/document-updater/app/coffee/DocumentManager.coffee @@ -123,9 +123,9 @@ module.exports = DocumentManager = RedisManager.removeDocFromMemory project_id, doc_id, (error) -> return callback(error) if error? callback null - - acceptChange: (project_id, doc_id, change_id, _callback = (error) ->) -> - timer = new Metrics.Timer("docManager.acceptChange") + + acceptChanges: (project_id, doc_id, change_ids = [], _callback = (error) ->) -> + timer = new Metrics.Timer("docManager.acceptChanges") callback = (args...) -> timer.done() _callback(args...) @@ -134,23 +134,7 @@ module.exports = DocumentManager = return callback(error) if error? if !lines? or !version? return callback(new Errors.NotFoundError("document not found: #{doc_id}")) - RangesManager.acceptChange change_id, ranges, (error, new_ranges) -> - return callback(error) if error? - RedisManager.updateDocument doc_id, lines, version, [], new_ranges, (error) -> - return callback(error) if error? - callback() - - bulkAcceptChanges: (project_id, doc_id, change_ids, _callback = (error) ->) -> - timer = new Metrics.Timer("docManager.bulkAcceptChanges") - callback = (args...) -> - timer.done() - _callback(args...) - - DocumentManager.getDoc project_id, doc_id, (error, lines, version, ranges) -> - return callback(error) if error? - if !lines? or !version? - return callback(new Errors.NotFoundError("document not found: #{doc_id}")) - RangesManager.bulkAcceptChanges change_ids, ranges, (error, new_ranges) -> + RangesManager.acceptChanges change_ids, ranges, (error, new_ranges) -> return callback(error) if error? RedisManager.updateDocument doc_id, lines, version, [], new_ranges, (error) -> return callback(error) if error? @@ -192,13 +176,9 @@ module.exports = DocumentManager = UpdateManager = require "./UpdateManager" UpdateManager.lockUpdatesAndDo DocumentManager.flushAndDeleteDoc, project_id, doc_id, callback - acceptChangeWithLock: (project_id, doc_id, change_id, callback = (error) ->) -> + acceptChangesWithLock: (project_id, doc_id, change_ids, callback = (error) ->) -> UpdateManager = require "./UpdateManager" - UpdateManager.lockUpdatesAndDo DocumentManager.acceptChange, project_id, doc_id, change_id, callback - - bulkAcceptChangesWithLock: (project_id, doc_id, change_ids, callback = (error) ->) -> - UpdateManager = require "./UpdateManager" - UpdateManager.lockUpdatesAndDo DocumentManager.bulkAcceptChanges, project_id, doc_id, change_ids, callback + UpdateManager.lockUpdatesAndDo DocumentManager.acceptChanges, project_id, doc_id, change_ids, callback deleteCommentWithLock: (project_id, doc_id, thread_id, callback = (error) ->) -> UpdateManager = require "./UpdateManager" diff --git a/services/document-updater/app/coffee/HttpController.coffee b/services/document-updater/app/coffee/HttpController.coffee index 075d1f8e40..8267df38f5 100644 --- a/services/document-updater/app/coffee/HttpController.coffee +++ b/services/document-updater/app/coffee/HttpController.coffee @@ -100,19 +100,20 @@ module.exports = HttpController = {project_id, doc_id, change_id} = req.params logger.log {project_id, doc_id, change_id}, "accepting change via http" timer = new Metrics.Timer("http.acceptChange") - DocumentManager.acceptChangeWithLock project_id, doc_id, change_id, (error) -> + DocumentManager.acceptChangesWithLock project_id, doc_id, [ change_id ], (error) -> timer.done() return next(error) if error? logger.log {project_id, doc_id, change_id}, "accepted change via http" res.send 204 # No Content - bulkAcceptChanges: (req, res, next = (error) ->) -> + acceptChanges: (req, res, next = (error) ->) -> {project_id, doc_id} = req.params {change_ids} = req.body - change_ids ?= [] + if !change_ids? + change_ids = [ req.params.change_id ] logger.log {project_id, doc_id}, "accepting #{ change_ids.length } changes via http" - timer = new Metrics.Timer("http.bulkAcceptChanges") - DocumentManager.bulkAcceptChangesWithLock project_id, doc_id, change_ids, (error) -> + timer = new Metrics.Timer("http.acceptChanges") + DocumentManager.acceptChangesWithLock project_id, doc_id, change_ids, (error) -> timer.done() return next(error) if error? logger.log {project_id, doc_id}, "accepted #{ change_ids.length } changes via http" diff --git a/services/document-updater/app/coffee/RangesManager.coffee b/services/document-updater/app/coffee/RangesManager.coffee index 559cd18da4..977790f825 100644 --- a/services/document-updater/app/coffee/RangesManager.coffee +++ b/services/document-updater/app/coffee/RangesManager.coffee @@ -33,15 +33,7 @@ module.exports = RangesManager = logger.log {project_id, doc_id, changesCount: response.changes?.length, commentsCount: response.comments?.length}, "applied updates to ranges" callback null, response - acceptChange: (change_id, ranges, callback = (error, ranges) ->) -> - {changes, comments} = ranges - logger.log {change_id}, "accepting change in ranges" - rangesTracker = new RangesTracker(changes, comments) - rangesTracker.removeChangeId(change_id) - response = RangesManager._getRanges(rangesTracker) - callback null, response - - bulkAcceptChanges: (change_ids, ranges, callback = (error, ranges) ->) -> + acceptChanges: (change_ids, ranges, callback = (error, ranges) ->) -> {changes, comments} = ranges logger.log {change_id}, "accepting #{ change_ids.length } changes in ranges" rangesTracker = new RangesTracker(changes, comments)