DocumentManager.coffee 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. RedisManager = require "./RedisManager"
  2. PersistenceManager = require "./PersistenceManager"
  3. DiffCodec = require "./DiffCodec"
  4. logger = require "logger-sharelatex"
  5. Metrics = require "./Metrics"
  6. HistoryManager = require "./HistoryManager"
  7. RealTimeRedisManager = require "./RealTimeRedisManager"
  8. Errors = require "./Errors"
  9. RangesManager = require "./RangesManager"
  10. MAX_UNFLUSHED_AGE = 300 * 1000 # 5 mins, document should be flushed to mongo this time after a change
  11. module.exports = DocumentManager =
  12. getDoc: (project_id, doc_id, _callback = (error, lines, version, ranges, unflushedTime, alreadyLoaded) ->) ->
  13. timer = new Metrics.Timer("docManager.getDoc")
  14. callback = (args...) ->
  15. timer.done()
  16. _callback(args...)
  17. RedisManager.getDoc project_id, doc_id, (error, lines, version, ranges, unflushedTime) ->
  18. return callback(error) if error?
  19. if !lines? or !version?
  20. logger.log {project_id, doc_id}, "doc not in redis so getting from persistence API"
  21. PersistenceManager.getDoc project_id, doc_id, (error, lines, version, ranges) ->
  22. return callback(error) if error?
  23. logger.log {project_id, doc_id, lines, version}, "got doc from persistence API"
  24. RedisManager.putDocInMemory project_id, doc_id, lines, version, ranges, (error) ->
  25. return callback(error) if error?
  26. callback null, lines, version, ranges, null, false
  27. else
  28. callback null, lines, version, ranges, unflushedTime, true
  29. getDocAndRecentOps: (project_id, doc_id, fromVersion, _callback = (error, lines, version, recentOps, ranges) ->) ->
  30. timer = new Metrics.Timer("docManager.getDocAndRecentOps")
  31. callback = (args...) ->
  32. timer.done()
  33. _callback(args...)
  34. DocumentManager.getDoc project_id, doc_id, (error, lines, version, ranges) ->
  35. return callback(error) if error?
  36. if fromVersion == -1
  37. callback null, lines, version, [], ranges
  38. else
  39. RedisManager.getPreviousDocOps doc_id, fromVersion, version, (error, ops) ->
  40. return callback(error) if error?
  41. callback null, lines, version, ops, ranges
  42. setDoc: (project_id, doc_id, newLines, source, user_id, undoing, _callback = (error) ->) ->
  43. timer = new Metrics.Timer("docManager.setDoc")
  44. callback = (args...) ->
  45. timer.done()
  46. _callback(args...)
  47. if !newLines?
  48. return callback(new Error("No lines were provided to setDoc"))
  49. UpdateManager = require "./UpdateManager"
  50. DocumentManager.getDoc project_id, doc_id, (error, oldLines, version, ranges, unflushedTime, alreadyLoaded) ->
  51. return callback(error) if error?
  52. if oldLines? and oldLines.length > 0 and oldLines[0].text?
  53. logger.log doc_id: doc_id, project_id: project_id, oldLines: oldLines, newLines: newLines, "document is JSON so not updating"
  54. return callback(null)
  55. logger.log doc_id: doc_id, project_id: project_id, oldLines: oldLines, newLines: newLines, "setting a document via http"
  56. DiffCodec.diffAsShareJsOp oldLines, newLines, (error, op) ->
  57. return callback(error) if error?
  58. if undoing
  59. for o in op or []
  60. o.u = true # Turn on undo flag for each op for track changes
  61. update =
  62. doc: doc_id
  63. op: op
  64. v: version
  65. meta:
  66. type: "external"
  67. source: source
  68. user_id: user_id
  69. UpdateManager.applyUpdate project_id, doc_id, update, (error) ->
  70. return callback(error) if error?
  71. # If the document was loaded already, then someone has it open
  72. # in a project, and the usual flushing mechanism will happen.
  73. # Otherwise we should remove it immediately since nothing else
  74. # is using it.
  75. if alreadyLoaded
  76. DocumentManager.flushDocIfLoaded project_id, doc_id, (error) ->
  77. return callback(error) if error?
  78. callback null
  79. else
  80. DocumentManager.flushAndDeleteDoc project_id, doc_id, (error) ->
  81. return callback(error) if error?
  82. callback null
  83. flushDocIfLoaded: (project_id, doc_id, _callback = (error) ->) ->
  84. timer = new Metrics.Timer("docManager.flushDocIfLoaded")
  85. callback = (args...) ->
  86. timer.done()
  87. _callback(args...)
  88. RedisManager.getDoc project_id, doc_id, (error, lines, version, ranges) ->
  89. return callback(error) if error?
  90. if !lines? or !version?
  91. logger.log project_id: project_id, doc_id: doc_id, "doc is not loaded so not flushing"
  92. callback null # TODO: return a flag to bail out, as we go on to remove doc from memory?
  93. else
  94. logger.log project_id: project_id, doc_id: doc_id, version: version, "flushing doc"
  95. PersistenceManager.setDoc project_id, doc_id, lines, version, ranges, (error) ->
  96. return callback(error) if error?
  97. RedisManager.clearUnflushedTime doc_id, callback
  98. flushAndDeleteDoc: (project_id, doc_id, _callback = (error) ->) ->
  99. timer = new Metrics.Timer("docManager.flushAndDeleteDoc")
  100. callback = (args...) ->
  101. timer.done()
  102. _callback(args...)
  103. DocumentManager.flushDocIfLoaded project_id, doc_id, (error) ->
  104. return callback(error) if error?
  105. # Flush in the background since it requires and http request
  106. # to track changes
  107. HistoryManager.flushDocChanges project_id, doc_id, (err) ->
  108. if err?
  109. logger.err {err, project_id, doc_id}, "error flushing to track changes"
  110. RedisManager.removeDocFromMemory project_id, doc_id, (error) ->
  111. return callback(error) if error?
  112. callback null
  113. acceptChanges: (project_id, doc_id, change_ids = [], _callback = (error) ->) ->
  114. timer = new Metrics.Timer("docManager.acceptChanges")
  115. callback = (args...) ->
  116. timer.done()
  117. _callback(args...)
  118. DocumentManager.getDoc project_id, doc_id, (error, lines, version, ranges) ->
  119. return callback(error) if error?
  120. if !lines? or !version?
  121. return callback(new Errors.NotFoundError("document not found: #{doc_id}"))
  122. RangesManager.acceptChanges change_ids, ranges, (error, new_ranges) ->
  123. return callback(error) if error?
  124. RedisManager.updateDocument doc_id, lines, version, [], new_ranges, (error) ->
  125. return callback(error) if error?
  126. callback()
  127. deleteComment: (project_id, doc_id, comment_id, _callback = (error) ->) ->
  128. timer = new Metrics.Timer("docManager.deleteComment")
  129. callback = (args...) ->
  130. timer.done()
  131. _callback(args...)
  132. DocumentManager.getDoc project_id, doc_id, (error, lines, version, ranges) ->
  133. return callback(error) if error?
  134. if !lines? or !version?
  135. return callback(new Errors.NotFoundError("document not found: #{doc_id}"))
  136. RangesManager.deleteComment comment_id, ranges, (error, new_ranges) ->
  137. return callback(error) if error?
  138. RedisManager.updateDocument doc_id, lines, version, [], new_ranges, (error) ->
  139. return callback(error) if error?
  140. callback()
  141. getDocAndFlushIfOld: (project_id, doc_id, callback = (error, doc) ->) ->
  142. DocumentManager.getDoc project_id, doc_id, (error, lines, version, ranges, unflushedTime, alreadyLoaded) ->
  143. return callback(error) if error?
  144. # if doc was already loaded see if it needs to be flushed
  145. if alreadyLoaded and unflushedTime? and (Date.now() - unflushedTime) > MAX_UNFLUSHED_AGE
  146. DocumentManager.flushDocIfLoaded project_id, doc_id, (error) ->
  147. return callback(error) if error?
  148. callback(null, lines, version)
  149. else
  150. callback(null, lines, version)
  151. getDocWithLock: (project_id, doc_id, callback = (error, lines, version) ->) ->
  152. UpdateManager = require "./UpdateManager"
  153. UpdateManager.lockUpdatesAndDo DocumentManager.getDoc, project_id, doc_id, callback
  154. getDocAndRecentOpsWithLock: (project_id, doc_id, fromVersion, callback = (error, lines, version) ->) ->
  155. UpdateManager = require "./UpdateManager"
  156. UpdateManager.lockUpdatesAndDo DocumentManager.getDocAndRecentOps, project_id, doc_id, fromVersion, callback
  157. getDocAndFlushIfOldWithLock: (project_id, doc_id, callback = (error, doc) ->) ->
  158. UpdateManager = require "./UpdateManager"
  159. UpdateManager.lockUpdatesAndDo DocumentManager.getDocAndFlushIfOld, project_id, doc_id, callback
  160. setDocWithLock: (project_id, doc_id, lines, source, user_id, undoing, callback = (error) ->) ->
  161. UpdateManager = require "./UpdateManager"
  162. UpdateManager.lockUpdatesAndDo DocumentManager.setDoc, project_id, doc_id, lines, source, user_id, undoing, callback
  163. flushDocIfLoadedWithLock: (project_id, doc_id, callback = (error) ->) ->
  164. UpdateManager = require "./UpdateManager"
  165. UpdateManager.lockUpdatesAndDo DocumentManager.flushDocIfLoaded, project_id, doc_id, callback
  166. flushAndDeleteDocWithLock: (project_id, doc_id, callback = (error) ->) ->
  167. UpdateManager = require "./UpdateManager"
  168. UpdateManager.lockUpdatesAndDo DocumentManager.flushAndDeleteDoc, project_id, doc_id, callback
  169. acceptChangesWithLock: (project_id, doc_id, change_ids, callback = (error) ->) ->
  170. UpdateManager = require "./UpdateManager"
  171. UpdateManager.lockUpdatesAndDo DocumentManager.acceptChanges, project_id, doc_id, change_ids, callback
  172. deleteCommentWithLock: (project_id, doc_id, thread_id, callback = (error) ->) ->
  173. UpdateManager = require "./UpdateManager"
  174. UpdateManager.lockUpdatesAndDo DocumentManager.deleteComment, project_id, doc_id, thread_id, callback