MongoManager.coffee 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. {db, ObjectId} = require "./mongojs"
  2. PackManager = require "./PackManager"
  3. async = require "async"
  4. _ = require "underscore"
  5. module.exports = MongoManager =
  6. getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
  7. db.docHistory
  8. .find(doc_id: ObjectId(doc_id.toString()), {pack: {$slice:-1}}) # only return the last entry in a pack
  9. .sort( v: -1 )
  10. .limit(1)
  11. .toArray (error, compressedUpdates) ->
  12. return callback(error) if error?
  13. callback null, compressedUpdates[0] or null
  14. peekLastCompressedUpdate: (doc_id, callback = (error, update, version) ->) ->
  15. # under normal use we pass back the last update as
  16. # callback(null,update,version).
  17. #
  18. # when we have an existing last update but want to force a new one
  19. # to start, we pass it back as callback(null,null,version), just
  20. # giving the version so we can check consistency.
  21. MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
  22. return callback(error) if error?
  23. if update?
  24. if update.broken
  25. # the update is marked as broken so we will force a new op
  26. return callback null, null
  27. else if update.pack?
  28. return callback null, update, update.pack[0]?.v
  29. else
  30. return callback null, update, update.v
  31. else
  32. PackManager.getLastPackFromIndex doc_id, (error, pack) ->
  33. return callback(error) if error?
  34. return callback(null, null, pack.v_end) if pack?.inS3? and pack?.v_end?
  35. callback null, null
  36. backportProjectId: (project_id, doc_id, callback = (error) ->) ->
  37. db.docHistory.update {
  38. doc_id: ObjectId(doc_id.toString())
  39. project_id: { $exists: false }
  40. }, {
  41. $set: { project_id: ObjectId(project_id.toString()) }
  42. }, {
  43. multi: true
  44. }, callback
  45. getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
  46. db.projectHistoryMetaData.find {
  47. project_id: ObjectId(project_id.toString())
  48. }, (error, results) ->
  49. return callback(error) if error?
  50. callback null, results[0]
  51. setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
  52. db.projectHistoryMetaData.update {
  53. project_id: ObjectId(project_id)
  54. }, {
  55. $set: metadata
  56. }, {
  57. upsert: true
  58. }, callback
  59. ensureIndices: () ->
  60. # For finding all updates that go into a diff for a doc
  61. db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
  62. # For finding all updates that affect a project
  63. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1, "meta.start_ts": -1 }, { background: true }
  64. # For finding updates that don't yet have a project_id and need it inserting
  65. db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }, { background: true }
  66. # For finding project meta-data
  67. db.projectHistoryMetaData.ensureIndex { project_id: 1 }, { background: true }
  68. # TTL index for auto deleting week old temporary ops
  69. db.docHistory.ensureIndex { expiresAt: 1 }, { expireAfterSeconds: 0, background: true }
  70. # For finding packs to be checked for archiving
  71. db.docHistory.ensureIndex { last_checked: 1 }, { background: true }
  72. # For finding archived packs
  73. db.docHistoryIndex.ensureIndex { project_id: 1 }, { background: true }