MongoManager.coffee 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {db, ObjectId} = require "./mongojs"
  2. PackManager = require "./PackManager"
  3. async = require "async"
  4. logger = require "logger-sharelatex"
  5. module.exports = MongoManager =
  6. getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
  7. db.docHistory
  8. .find(doc_id: ObjectId(doc_id.toString()))
  9. .sort( v: -1 )
  10. .limit(1)
  11. .toArray (error, compressedUpdates) ->
  12. return callback(error) if error?
  13. if compressedUpdates[0]?.pack?
  14. # cannot pop from a pack, throw error
  15. error = new Error("last compressed update is a pack")
  16. return callback error, null
  17. return callback null, compressedUpdates[0] or null
  18. deleteCompressedUpdate: (id, callback = (error) ->) ->
  19. db.docHistory.remove({ _id: ObjectId(id.toString()) }, callback)
  20. popLastCompressedUpdate: (doc_id, callback = (error, update, version) ->) ->
  21. # under normal use we pass back the last update as
  22. # callback(null,update).
  23. #
  24. # when we have an existing last update but want to force a new one
  25. # to start, we pass it back as callback(null,null,version), just
  26. # giving the version so we can check consistency.
  27. MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
  28. return callback(error) if error?
  29. if update?
  30. if update.inS3?
  31. # we want to force a new update, but ensure that it is
  32. # consistent with the version of the existing one in S3
  33. return callback null, null, update.v
  34. else
  35. MongoManager.deleteCompressedUpdate update._id, (error) ->
  36. return callback(error) if error?
  37. callback null, update
  38. else
  39. callback null, null
  40. insertCompressedUpdates: (project_id, doc_id, updates, temporary, callback = (error) ->) ->
  41. jobs = []
  42. for update in updates
  43. do (update) ->
  44. jobs.push (callback) -> MongoManager.insertCompressedUpdate project_id, doc_id, update, temporary, callback
  45. async.series jobs, (err, results) ->
  46. if not temporary
  47. # keep track of updates to be packed
  48. db.docHistoryStats.update {doc_id:ObjectId(doc_id)}, {
  49. $inc:{update_count:updates.length},
  50. $currentDate:{last_update:true}
  51. }, {upsert:true}, () ->
  52. callback(err,results)
  53. else
  54. callback(err,results)
  55. insertCompressedUpdate: (project_id, doc_id, update, temporary, callback = (error) ->) ->
  56. update = {
  57. doc_id: ObjectId(doc_id.toString())
  58. project_id: ObjectId(project_id.toString())
  59. op: update.op
  60. meta: update.meta
  61. v: update.v
  62. }
  63. if temporary
  64. seconds = 1000
  65. minutes = 60 * seconds
  66. hours = 60 * minutes
  67. days = 24 * hours
  68. update.expiresAt = new Date(Date.now() + 7 * days)
  69. # may need to roll over a pack here if we are inserting packs
  70. db.docHistory.insert update, callback
  71. getDocUpdates:(doc_id, options = {}, callback = (error, updates) ->) ->
  72. query =
  73. doc_id: ObjectId(doc_id.toString())
  74. if options.from?
  75. query["v"] ||= {}
  76. query["v"]["$gte"] = options.from
  77. if options.to?
  78. query["v"] ||= {}
  79. query["v"]["$lte"] = options.to
  80. PackManager.findDocResults(db.docHistory, query, options.limit, callback)
  81. getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  82. query =
  83. project_id: ObjectId(project_id.toString())
  84. if options.before?
  85. query["meta.end_ts"] = { $lt: options.before }
  86. PackManager.findProjectResults(db.docHistory, query, options.limit, callback)
  87. backportProjectId: (project_id, doc_id, callback = (error) ->) ->
  88. db.docHistory.update {
  89. doc_id: ObjectId(doc_id.toString())
  90. project_id: { $exists: false }
  91. }, {
  92. $set: { project_id: ObjectId(project_id.toString()) }
  93. }, {
  94. multi: true
  95. }, callback
  96. getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
  97. db.projectHistoryMetaData.find {
  98. project_id: ObjectId(project_id.toString())
  99. }, (error, results) ->
  100. return callback(error) if error?
  101. callback null, results[0]
  102. setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
  103. db.projectHistoryMetaData.update {
  104. project_id: ObjectId(project_id)
  105. }, {
  106. $set: metadata
  107. }, {
  108. upsert: true
  109. }, callback
  110. ensureIndices: () ->
  111. # For finding all updates that go into a diff for a doc
  112. db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
  113. # For finding all updates that affect a project
  114. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }, { background: true }
  115. # For finding all packs that affect a project (use a sparse index so only packs are included)
  116. db.docHistory.ensureIndex { project_id: 1, "pack.0.meta.end_ts": 1, "meta.end_ts": 1}, { background: true, sparse: true }
  117. # For finding updates that don't yet have a project_id and need it inserting
  118. db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }, { background: true }
  119. # For finding project meta-data
  120. db.projectHistoryMetaData.ensureIndex { project_id: 1 }, { background: true }
  121. # TTL index for auto deleting week old temporary ops
  122. db.docHistory.ensureIndex { expiresAt: 1 }, { expireAfterSeconds: 0, background: true }
  123. # For finding documents which need packing
  124. db.docHistoryStats.ensureIndex { doc_id: 1 }, { background: true }
  125. db.docHistoryStats.ensureIndex { updates: -1, doc_id: 1 }, { background: true }
  126. getDocChangesCount: (doc_id, callback)->
  127. db.docHistory.count { doc_id : ObjectId(doc_id.toString()), inS3 : { $exists : false }}, {}, callback
  128. getArchivedDocChanges: (doc_id, callback)->
  129. db.docHistory.count { doc_id: ObjectId(doc_id.toString()) , inS3: { $exists: true }}, {}, callback
  130. markDocHistoryAsArchiveInProgress: (doc_id, update, callback) ->
  131. db.docHistory.update { _id: update._id }, { $set : { inS3 : false } }, callback
  132. clearDocHistoryAsArchiveInProgress: (doc_id, update, callback) ->
  133. db.docHistory.update { _id: update._id }, { $unset : { inS3 : true } }, callback
  134. markDocHistoryAsArchived: (doc_id, update, callback)->
  135. db.docHistory.update { _id: update._id }, { $set : { inS3 : true } }, (error)->
  136. return callback(error) if error?
  137. db.docHistory.remove { doc_id : ObjectId(doc_id.toString()), inS3 : { $exists : false }, v: { $lt : update.v }, expiresAt: {$exists : false} }, (error)->
  138. return callback(error) if error?
  139. callback(error)
  140. markDocHistoryAsUnarchived: (doc_id, callback)->
  141. # note this removes any inS3 field, regardless of its value (true/false/null)
  142. db.docHistory.update { doc_id: ObjectId(doc_id.toString()) }, { $unset : { inS3 : true } }, { multi: true }, (error)->
  143. callback(error)