MongoManager.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. {db, ObjectId} = require "./mongojs"
  2. async = require "async"
  3. module.exports = MongoManager =
  4. getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
  5. db.docHistory
  6. .find(doc_id: ObjectId(doc_id.toString()))
  7. .sort( v: -1 )
  8. .limit(1)
  9. .toArray (error, compressedUpdates) ->
  10. return callback(error) if error?
  11. return callback null, compressedUpdates[0] or null
  12. deleteCompressedUpdate: (id, callback = (error) ->) ->
  13. db.docHistory.remove({ _id: ObjectId(id.toString()) }, callback)
  14. popLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
  15. MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
  16. return callback(error) if error?
  17. if update?
  18. MongoManager.deleteCompressedUpdate update._id, (error) ->
  19. return callback(error) if error?
  20. callback null, update
  21. else
  22. callback null, null
  23. insertCompressedUpdates: (project_id, doc_id, updates, permanent, callback = (error) ->) ->
  24. jobs = []
  25. for update in updates
  26. do (update) ->
  27. jobs.push (callback) -> MongoManager.insertCompressedUpdate project_id, doc_id, update, permanent, callback
  28. async.series jobs, callback
  29. insertCompressedUpdate: (project_id, doc_id, update, temporary, callback = (error) ->) ->
  30. update = {
  31. doc_id: ObjectId(doc_id.toString())
  32. project_id: ObjectId(project_id.toString())
  33. op: update.op
  34. meta: update.meta
  35. v: update.v
  36. }
  37. if temporary
  38. seconds = 1000
  39. minutes = 60 * seconds
  40. hours = 60 * minutes
  41. days = 24 * hours
  42. update.expiresAt = new Date(Date.now() + 7 * days)
  43. db.docHistory.insert update, callback
  44. getDocUpdates:(doc_id, options = {}, callback = (error, updates) ->) ->
  45. query =
  46. doc_id: ObjectId(doc_id.toString())
  47. if options.from?
  48. query["v"] ||= {}
  49. query["v"]["$gte"] = options.from
  50. if options.to?
  51. query["v"] ||= {}
  52. query["v"]["$lte"] = options.to
  53. cursor = db.docHistory
  54. .find( query )
  55. .sort( v: -1 )
  56. if options.limit?
  57. cursor.limit(options.limit)
  58. cursor.toArray callback
  59. getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  60. query =
  61. project_id: ObjectId(project_id.toString())
  62. if options.before?
  63. query["meta.end_ts"] = { $lt: options.before }
  64. cursor = db.docHistory
  65. .find( query )
  66. .sort( "meta.end_ts": -1 )
  67. if options.limit?
  68. cursor.limit(options.limit)
  69. cursor.toArray callback
  70. backportProjectId: (project_id, doc_id, callback = (error) ->) ->
  71. db.docHistory.update {
  72. doc_id: ObjectId(doc_id.toString())
  73. project_id: { $exists: false }
  74. }, {
  75. $set: { project_id: ObjectId(project_id.toString()) }
  76. }, {
  77. multi: true
  78. }, callback
  79. getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
  80. db.projectHistoryMetaData.find {
  81. project_id: ObjectId(project_id.toString())
  82. }, (error, results) ->
  83. return callback(error) if error?
  84. callback null, results[0]
  85. setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
  86. db.projectHistoryMetaData.update {
  87. project_id: ObjectId(project_id)
  88. }, {
  89. $set: metadata
  90. }, {
  91. upsert: true
  92. }, callback
  93. ensureIndices: () ->
  94. # For finding all updates that go into a diff for a doc
  95. db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
  96. # For finding all updates that affect a project
  97. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }, { background: true }
  98. # For finding updates that don't yet have a project_id and need it inserting
  99. db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }, { background: true }
  100. # For finding project meta-data
  101. db.projectHistoryMetaData.ensureIndex { project_id: 1 }, { background: true }
  102. # TTL index for auto deleting week old temporary ops
  103. db.docHistory.ensureIndex { expiresAt: 1 }, { expireAfterSeconds: 0, background: true }