overleaf/services/track-changes/app/coffee/MongoManager.coffee

126 lines
3.8 KiB
CoffeeScript

{db, ObjectId} = require "./mongojs"
async = require "async"
module.exports = MongoManager =
getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
db.docHistory
.find(doc_id: ObjectId(doc_id.toString()))
.sort( v: -1 )
.limit(1)
.toArray (error, compressedUpdates) ->
return callback(error) if error?
return callback null, compressedUpdates[0] or null
deleteCompressedUpdate: (id, callback = (error) ->) ->
db.docHistory.remove({ _id: ObjectId(id.toString()) }, callback)
popLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
return callback(error) if error?
if update?
MongoManager.deleteCompressedUpdate update._id, (error) ->
return callback(error) if error?
callback null, update
else
callback null, null
insertCompressedUpdates: (project_id, doc_id, updates, permanent, callback = (error) ->) ->
jobs = []
for update in updates
do (update) ->
jobs.push (callback) -> MongoManager.insertCompressedUpdate project_id, doc_id, update, permanent, callback
async.series jobs, callback
insertCompressedUpdate: (project_id, doc_id, update, temporary, callback = (error) ->) ->
update = {
doc_id: ObjectId(doc_id.toString())
project_id: ObjectId(project_id.toString())
op: update.op
meta: update.meta
v: update.v
}
if temporary
update.tempCreatedAt = new Date()
db.docHistory.insert update, callback
getDocUpdates:(doc_id, options = {}, callback = (error, updates) ->) ->
query =
doc_id: ObjectId(doc_id.toString())
if options.from?
query["v"] ||= {}
query["v"]["$gte"] = options.from
if options.to?
query["v"] ||= {}
query["v"]["$lte"] = options.to
cursor = db.docHistory
.find( query )
.sort( v: -1 )
if options.limit?
cursor.limit(options.limit)
cursor.toArray callback
getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
query =
project_id: ObjectId(project_id.toString())
if options.before?
query["meta.end_ts"] = { $lt: options.before }
cursor = db.docHistory
.find( query )
.sort( "meta.end_ts": -1 )
if options.limit?
cursor.limit(options.limit)
cursor.toArray callback
deleteOldProjectUpdates: (project_id, before, callback = (error) ->) ->
db.docHistory.remove {
project_id: ObjectId(project_id)
"meta.end_ts": { $lt: before }
}, callback
backportProjectId: (project_id, doc_id, callback = (error) ->) ->
db.docHistory.update {
doc_id: ObjectId(doc_id.toString())
project_id: { $exists: false }
}, {
$set: { project_id: ObjectId(project_id.toString()) }
}, {
multi: true
}, callback
getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
db.projectHistoryMetaData.find {
project_id: ObjectId(project_id.toString())
}, (error, results) ->
return callback(error) if error?
callback null, results[0]
setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
db.projectHistoryMetaData.update {
project_id: ObjectId(project_id)
}, {
$set: metadata
}, {
upsert: true
}, callback
ensureIndices: () ->
# For finding all updates that go into a diff for a doc
db.docHistory.ensureIndex { doc_id: 1, v: 1 }
# For finding all updates that affect a project
db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }
# For finding updates that don't yet have a project_id and need it inserting
db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }
# For finding project meta-data
db.projectHistoryMetaData.ensureIndex { project_id: 1 }
# TTL index for auto deleting week old temporary ops
minutes = 60
hours = 60 * minutes
days = 24 * hours
db.docHistory.ensureIndex { tempCreatedAt: 1 }, { expireAfterSeconds: 7 * days }