From d530ee9501eba8e51cf6a30cefa2f3b5811f73c7 Mon Sep 17 00:00:00 2001 From: Paulo Reis Date: Thu, 4 May 2017 15:32:54 +0100 Subject: [PATCH] Add methods to bulk accept changes. --- services/document-updater/app.coffee | 15 +++++++------- .../app/coffee/DocumentManager.coffee | 20 +++++++++++++++++++ .../app/coffee/HttpController.coffee | 12 +++++++++++ .../app/coffee/RangesManager.coffee | 9 +++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/services/document-updater/app.coffee b/services/document-updater/app.coffee index 36c0cb3a72..1672d1b3b5 100644 --- a/services/document-updater/app.coffee +++ b/services/document-updater/app.coffee @@ -42,14 +42,15 @@ app.param 'doc_id', (req, res, next, doc_id) -> else next new Error("invalid doc id") -app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc -app.post '/project/:project_id/doc/:doc_id', HttpController.setDoc -app.post '/project/:project_id/doc/:doc_id/flush', HttpController.flushDocIfLoaded -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.get '/project/:project_id/doc/:doc_id', HttpController.getDoc +app.post '/project/:project_id/doc/:doc_id', HttpController.setDoc +app.post '/project/:project_id/doc/:doc_id/flush', HttpController.flushDocIfLoaded +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.del '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment +app.post '/project/:project_id/doc/:doc_id/change/accept', HttpController.bulkAcceptChanges +app.del '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment app.get '/total', (req, res)-> timer = new Metrics.Timer("http.allDocList") diff --git a/services/document-updater/app/coffee/DocumentManager.coffee b/services/document-updater/app/coffee/DocumentManager.coffee index be47ec4c8c..a327f04af4 100644 --- a/services/document-updater/app/coffee/DocumentManager.coffee +++ b/services/document-updater/app/coffee/DocumentManager.coffee @@ -139,6 +139,22 @@ module.exports = DocumentManager = 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) -> + return callback(error) if error? + RedisManager.updateDocument doc_id, lines, version, [], new_ranges, (error) -> + return callback(error) if error? + callback() deleteComment: (project_id, doc_id, comment_id, _callback = (error) ->) -> timer = new Metrics.Timer("docManager.deleteComment") @@ -180,6 +196,10 @@ module.exports = DocumentManager = 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 + deleteCommentWithLock: (project_id, doc_id, thread_id, callback = (error) ->) -> UpdateManager = require "./UpdateManager" UpdateManager.lockUpdatesAndDo DocumentManager.deleteComment, project_id, doc_id, thread_id, callback diff --git a/services/document-updater/app/coffee/HttpController.coffee b/services/document-updater/app/coffee/HttpController.coffee index aae2b51f8e..075d1f8e40 100644 --- a/services/document-updater/app/coffee/HttpController.coffee +++ b/services/document-updater/app/coffee/HttpController.coffee @@ -105,6 +105,18 @@ module.exports = HttpController = 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) ->) -> + {project_id, doc_id} = req.params + {change_ids} = req.body + change_ids ?= [] + 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.done() + return next(error) if error? + logger.log {project_id, doc_id}, "accepted #{ change_ids.length } changes via http" + res.send 204 # No Content deleteComment: (req, res, next = (error) ->) -> {project_id, doc_id, comment_id} = req.params diff --git a/services/document-updater/app/coffee/RangesManager.coffee b/services/document-updater/app/coffee/RangesManager.coffee index ebef566424..559cd18da4 100644 --- a/services/document-updater/app/coffee/RangesManager.coffee +++ b/services/document-updater/app/coffee/RangesManager.coffee @@ -41,6 +41,15 @@ module.exports = RangesManager = response = RangesManager._getRanges(rangesTracker) callback null, response + bulkAcceptChanges: (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) + for change_id in change_ids + rangesTracker.removeChangeId(change_id) + response = RangesManager._getRanges(rangesTracker) + callback null, response + deleteComment: (comment_id, ranges, callback = (error, ranges) ->) -> {changes, comments} = ranges logger.log {comment_id}, "deleting comment in ranges"