MongoManager.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. {db, ObjectId} = require "./mongojs"
  2. MongoPackManager = require "./MongoPackManager"
  3. async = require "async"
  4. _ = require "underscore"
  5. module.exports = MongoManager =
  6. # only used in this module
  7. getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
  8. db.docHistory
  9. .find(doc_id: ObjectId(doc_id.toString()))
  10. .sort( v: -1 )
  11. .limit(1)
  12. .toArray (error, compressedUpdates) ->
  13. return callback(error) if error?
  14. return callback null, null if compressedUpdates[0]?.pack? # cannot pop from a pack
  15. return callback null, compressedUpdates[0] or null
  16. # only used in this module
  17. deleteCompressedUpdate: (id, callback = (error) ->) ->
  18. db.docHistory.remove({ _id: ObjectId(id.toString()) }, callback)
  19. # used in UpdatesManager
  20. popLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
  21. MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
  22. return callback(error) if error?
  23. if update?
  24. MongoManager.deleteCompressedUpdate update._id, (error) ->
  25. return callback(error) if error?
  26. callback null, update
  27. else
  28. callback null, null
  29. # used in UpdatesManager
  30. insertCompressedUpdates: (project_id, doc_id, updates, permanent, callback = (error) ->) ->
  31. jobs = []
  32. for update in updates
  33. do (update) ->
  34. jobs.push (callback) -> MongoManager.insertCompressedUpdate project_id, doc_id, update, permanent, callback
  35. async.series jobs, callback
  36. insertCompressedUpdate: (project_id, doc_id, update, temporary, callback = (error) ->) ->
  37. update = {
  38. doc_id: ObjectId(doc_id.toString())
  39. project_id: ObjectId(project_id.toString())
  40. op: update.op
  41. meta: update.meta
  42. v: update.v
  43. }
  44. if temporary
  45. seconds = 1000
  46. minutes = 60 * seconds
  47. hours = 60 * minutes
  48. days = 24 * hours
  49. update.expiresAt = new Date(Date.now() + 7 * days)
  50. # may need to roll over a pack here if we are inserting packs
  51. db.docHistory.insert update, callback
  52. getDocUpdates:(doc_id, options = {}, callback = (error, updates) ->) ->
  53. query =
  54. doc_id: ObjectId(doc_id.toString())
  55. if options.from?
  56. query["v"] ||= {}
  57. query["v"]["$gte"] = options.from
  58. if options.to?
  59. query["v"] ||= {}
  60. query["v"]["$lte"] = options.to
  61. MongoPackManager.findDocResults(db.docHistory, query, options.limit, callback)
  62. getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
  63. query =
  64. project_id: ObjectId(project_id.toString())
  65. if options.before?
  66. query["meta.end_ts"] = { $lt: options.before }
  67. MongoPackManager.findProjectResults(db.docHistory, query, options.limit, 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 (getLastCompressedUpdate, getDocUpdates v > from && v < to)
  93. db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
  94. # For finding all updates that affect a project (getProjectUpdates meta.end_ts < before
  95. db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }, { background: true }
  96. # For finding all packs that affect a project (use a sparse index so only packs are included)
  97. db.docHistory.ensureIndex { project_id: 1, "pack.0.meta.end_ts": 1, "meta.end_ts": 1} , { background: true, sparse: 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 }