MongoManager.coffee 6.6 KB

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