MongoManager.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, callback = (error) ->) ->
  24. jobs = []
  25. for update in updates
  26. do (update) ->
  27. jobs.push (callback) -> MongoManager.insertCompressedUpdate project_id, doc_id, update, callback
  28. async.series jobs, callback
  29. insertCompressedUpdate: (project_id, doc_id, update, callback = (error) ->) ->
  30. db.docHistory.insert {
  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. }, callback
  37. getDocUpdates:(doc_id, options = {}, callback = (error, updates) ->) ->
  38. query =
  39. doc_id: ObjectId(doc_id.toString())
  40. if options.from?
  41. query["v"] ||= {}
  42. query["v"]["$gte"] = options.from
  43. if options.to?
  44. query["v"] ||= {}
  45. query["v"]["$lte"] = options.to
  46. cursor = db.docHistory
  47. .find( query )
  48. .sort( v: -1 )
  49. if options.limit?
  50. cursor.limit(options.limit)
  51. cursor.toArray callback
  52. getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  53. query =
  54. project_id: ObjectId(project_id.toString())
  55. if options.before?
  56. query["meta.end_ts"] = { $lt: options.before }
  57. cursor = db.docHistory
  58. .find( query )
  59. .sort( "meta.end_ts": -1 )
  60. if options.limit?
  61. cursor.limit(options.limit)
  62. cursor.toArray callback
  63. deleteOldProjectUpdates: (project_id, before, callback = (error) ->) ->
  64. db.docHistory.remove {
  65. project_id: ObjectId(project_id)
  66. "meta.end_ts": { $lt: before }
  67. }, callback
  68. backportProjectId: (project_id, doc_id, callback = (error) ->) ->
  69. db.docHistory.update {
  70. doc_id: ObjectId(doc_id.toString())
  71. project_id: { $exists: false }
  72. }, {
  73. $set: { project_id: ObjectId(project_id.toString()) }
  74. }, {
  75. multi: true
  76. }, callback
  77. getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
  78. db.projectHistoryMetaData.find {
  79. project_id: ObjectId(project_id.toString())
  80. }, (error, results) ->
  81. return callback(error) if error?
  82. callback null, results[0]
  83. setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
  84. db.projectHistoryMetaData.update {
  85. project_id: ObjectId(project_id)
  86. }, {
  87. $set: metadata
  88. }, {
  89. upsert: true
  90. }, callback
  91. ensureIndices: () ->
  92. # For finding all updates that go into a diff for a doc
  93. db.docHistory.ensureIndex { doc_id: 1, v: 1 }
  94. # For finding all updates that affect a project
  95. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }
  96. # For finding updates that don't yet have a project_id and need it inserting
  97. db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }
  98. # For finding project meta-data
  99. db.projectHistoryMetaData.ensureIndex { project_id: 1 }