DiffManager.coffee 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. UpdatesManager = require "./UpdatesManager"
  2. DocumentUpdaterManager = require "./DocumentUpdaterManager"
  3. DiffGenerator = require "./DiffGenerator"
  4. logger = require "logger-sharelatex"
  5. module.exports = DiffManager =
  6. getLatestDocAndUpdates: (project_id, doc_id, fromVersion, toVersion, callback = (error, content, version, updates) ->) ->
  7. # Get updates last, since then they must be ahead and it
  8. # might be possible to rewind to the same version as the doc.
  9. DocumentUpdaterManager.getDocument project_id, doc_id, (error, content, version) ->
  10. return callback(error) if error?
  11. UpdatesManager.getDocUpdatesWithUserInfo project_id, doc_id, from: fromVersion, to: toVersion, (error, updates) ->
  12. return callback(error) if error?
  13. callback(null, content, version, updates)
  14. getDiff: (project_id, doc_id, fromVersion, toVersion, callback = (error, diff) ->) ->
  15. DiffManager.getDocumentBeforeVersion project_id, doc_id, fromVersion, (error, startingContent, updates) ->
  16. if error?
  17. if error.message == "broken-history"
  18. return callback(null, "history unavailable")
  19. else
  20. return callback(error)
  21. updatesToApply = []
  22. for update in updates.slice().reverse()
  23. if update.v <= toVersion
  24. updatesToApply.push update
  25. try
  26. diff = DiffGenerator.buildDiff startingContent, updatesToApply
  27. catch e
  28. return callback(e)
  29. callback(null, diff)
  30. getDocumentBeforeVersion: (project_id, doc_id, version, _callback = (error, document, rewoundUpdates) ->) ->
  31. # Whichever order we get the latest document and the latest updates,
  32. # there is potential for updates to be applied between them so that
  33. # they do not return the same 'latest' versions.
  34. # If this happens, we just retry and hopefully get them at the compatible
  35. # versions.
  36. retries = 3
  37. callback = (error, args...) ->
  38. if error?
  39. if error.retry and retries > 0
  40. logger.warn {error, project_id, doc_id, version}, "retrying getDocumentBeforeVersion"
  41. retry()
  42. else
  43. _callback(error)
  44. else
  45. _callback(null, args...)
  46. do retry = () ->
  47. retries--
  48. DiffManager._tryGetDocumentBeforeVersion(project_id, doc_id, version, callback)
  49. _tryGetDocumentBeforeVersion: (project_id, doc_id, version, callback = (error, document, rewoundUpdates) ->) ->
  50. logger.log project_id: project_id, doc_id: doc_id, version: version, "getting document before version"
  51. DiffManager.getLatestDocAndUpdates project_id, doc_id, version, null, (error, content, version, updates) ->
  52. return callback(error) if error?
  53. # bail out if we hit a broken update
  54. for u in updates when u.broken
  55. return callback new Error "broken-history"
  56. # discard any updates which are ahead of this document version
  57. while updates[0]?.v >= version
  58. updates.shift()
  59. lastUpdate = updates[0]
  60. if lastUpdate? and lastUpdate.v != version - 1
  61. error = new Error("latest update version, #{lastUpdate.v}, does not match doc version, #{version}")
  62. error.retry = true
  63. return callback error
  64. logger.log {docVersion: version, lastUpdateVersion: lastUpdate?.v, updateCount: updates.length}, "rewinding updates"
  65. tryUpdates = updates.slice().reverse()
  66. try
  67. startingContent = DiffGenerator.rewindUpdates content, tryUpdates
  68. # tryUpdates is reversed, and any unapplied ops are marked as broken
  69. catch e
  70. return callback(e)
  71. callback(null, startingContent, tryUpdates)