MongoManager.coffee 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 # marked as broken so we will force a new op
  25. return callback null, null
  26. else if update.pack?
  27. if update.finalised # no more ops can be appended
  28. return callback null, null, update.pack[0]?.v
  29. else
  30. return callback null, update, update.pack[0]?.v
  31. else
  32. return callback null, update, update.v
  33. else
  34. PackManager.getLastPackFromIndex doc_id, (error, pack) ->
  35. return callback(error) if error?
  36. return callback(null, null, pack.v_end) if pack?.inS3? and pack?.v_end?
  37. callback null, null
  38. backportProjectId: (project_id, doc_id, callback = (error) ->) ->
  39. db.docHistory.update {
  40. doc_id: ObjectId(doc_id.toString())
  41. project_id: { $exists: false }
  42. }, {
  43. $set: { project_id: ObjectId(project_id.toString()) }
  44. }, {
  45. multi: true
  46. }, callback
  47. getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
  48. db.projectHistoryMetaData.find {
  49. project_id: ObjectId(project_id.toString())
  50. }, (error, results) ->
  51. return callback(error) if error?
  52. callback null, results[0]
  53. setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
  54. db.projectHistoryMetaData.update {
  55. project_id: ObjectId(project_id)
  56. }, {
  57. $set: metadata
  58. }, {
  59. upsert: true
  60. }, callback
  61. upgradeHistory: (project_id, callback = (error) ->) ->
  62. # preserve the project's existing history
  63. db.docHistory.update {
  64. project_id: ObjectId(project_id)
  65. temporary: true
  66. expiresAt: {$exists: true}
  67. }, {
  68. $set: {temporary: false}
  69. $unset: {expiresAt: ""}
  70. }, {
  71. multi: true
  72. }, callback
  73. ensureIndices: () ->
  74. # For finding all updates that go into a diff for a doc
  75. db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
  76. # For finding all updates that affect a project
  77. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1, "meta.start_ts": -1 }, { background: true }
  78. # For finding updates that don't yet have a project_id and need it inserting
  79. db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }, { background: true }
  80. # For finding project meta-data
  81. db.projectHistoryMetaData.ensureIndex { project_id: 1 }, { background: true }
  82. # TTL index for auto deleting week old temporary ops
  83. db.docHistory.ensureIndex { expiresAt: 1 }, { expireAfterSeconds: 0, background: true }
  84. # For finding packs to be checked for archiving
  85. db.docHistory.ensureIndex { last_checked: 1 }, { background: true }
  86. # For finding archived packs
  87. db.docHistoryIndex.ensureIndex { project_id: 1 }, { background: true }