MongoManager.coffee 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) ->) ->
  21. MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
  22. return callback(error) if error?
  23. if update?
  24. MongoManager.deleteCompressedUpdate update._id, (error) ->
  25. return callback(error) if error?
  26. callback null, update
  27. else
  28. callback null, null
  29. insertCompressedUpdates: (project_id, doc_id, updates, temporary, callback = (error) ->) ->
  30. jobs = []
  31. for update in updates
  32. do (update) ->
  33. jobs.push (callback) -> MongoManager.insertCompressedUpdate project_id, doc_id, update, temporary, callback
  34. async.series jobs, (err, results) ->
  35. if not temporary
  36. # keep track of updates to be packed
  37. db.docHistoryStats.update {doc_id:ObjectId(doc_id)}, {
  38. $inc:{update_count:updates.length},
  39. $currentDate:{last_update:true}
  40. }, {upsert:true}, () ->
  41. callback(err,results)
  42. else
  43. callback(err,results)
  44. insertCompressedUpdate: (project_id, doc_id, update, temporary, callback = (error) ->) ->
  45. inS3 = update.inS3?
  46. update = {
  47. doc_id: ObjectId(doc_id.toString())
  48. project_id: ObjectId(project_id.toString())
  49. op: update.op
  50. meta: update.meta
  51. v: update.v
  52. }
  53. if inS3
  54. update.inS3 = true
  55. if temporary
  56. seconds = 1000
  57. minutes = 60 * seconds
  58. hours = 60 * minutes
  59. days = 24 * hours
  60. update.expiresAt = new Date(Date.now() + 7 * days)
  61. # may need to roll over a pack here if we are inserting packs
  62. db.docHistory.insert update, callback
  63. getDocUpdates:(doc_id, options = {}, callback = (error, updates) ->) ->
  64. query =
  65. doc_id: ObjectId(doc_id.toString())
  66. if options.from?
  67. query["v"] ||= {}
  68. query["v"]["$gte"] = options.from
  69. if options.to?
  70. query["v"] ||= {}
  71. query["v"]["$lte"] = options.to
  72. PackManager.findDocResults(db.docHistory, query, options.limit, callback)
  73. getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  74. query =
  75. project_id: ObjectId(project_id.toString())
  76. if options.before?
  77. query["meta.end_ts"] = { $lt: options.before }
  78. PackManager.findProjectResults(db.docHistory, query, options.limit, callback)
  79. backportProjectId: (project_id, doc_id, callback = (error) ->) ->
  80. db.docHistory.update {
  81. doc_id: ObjectId(doc_id.toString())
  82. project_id: { $exists: false }
  83. }, {
  84. $set: { project_id: ObjectId(project_id.toString()) }
  85. }, {
  86. multi: true
  87. }, callback
  88. getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
  89. db.projectHistoryMetaData.find {
  90. project_id: ObjectId(project_id.toString())
  91. }, (error, results) ->
  92. return callback(error) if error?
  93. callback null, results[0]
  94. setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
  95. db.projectHistoryMetaData.update {
  96. project_id: ObjectId(project_id)
  97. }, {
  98. $set: metadata
  99. }, {
  100. upsert: true
  101. }, callback
  102. ensureIndices: () ->
  103. # For finding all updates that go into a diff for a doc
  104. db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
  105. # For finding all updates that affect a project
  106. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }, { background: true }
  107. # For finding all packs that affect a project (use a sparse index so only packs are included)
  108. db.docHistory.ensureIndex { project_id: 1, "pack.0.meta.end_ts": 1, "meta.end_ts": 1}, { background: true, sparse: true }
  109. # For finding updates that don't yet have a project_id and need it inserting
  110. db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }, { background: true }
  111. # For finding project meta-data
  112. db.projectHistoryMetaData.ensureIndex { project_id: 1 }, { background: true }
  113. # TTL index for auto deleting week old temporary ops
  114. db.docHistory.ensureIndex { expiresAt: 1 }, { expireAfterSeconds: 0, background: true }
  115. # For finding documents which need packing
  116. db.docHistoryStats.ensureIndex { doc_id: 1 }, { background: true }
  117. db.docHistoryStats.ensureIndex { updates: -1, doc_id: 1 }, { background: true }
  118. getDocChangesCount: (doc_id, callback)->
  119. db.docHistory.count { doc_id : ObjectId(doc_id.toString()), inS3 : { $exists : false }}, {}, callback
  120. getArchivedDocChanges: (doc_id, callback)->
  121. db.docHistory.count { doc_id: ObjectId(doc_id.toString()) , inS3: true }, {}, callback
  122. markDocHistoryAsArchived: (doc_id, update, callback)->
  123. db.docHistory.update { _id: update._id }, { $set : { inS3 : true } }, (error)->
  124. return callback(error) if error?
  125. db.docHistory.remove { doc_id : ObjectId(doc_id.toString()), inS3 : { $exists : false }, v: { $lt : update.v }, expiresAt: {$exists : false} }, (error)->
  126. return callback(error) if error?
  127. callback(error)
  128. markDocHistoryAsUnarchived: (doc_id, callback)->
  129. db.docHistory.update { doc_id: ObjectId(doc_id.toString()) }, { $unset : { inS3 : true } }, { multi: true }, (error)->
  130. callback(error)