2014-01-27 11:26:58 -05:00
|
|
|
{db, ObjectId} = require "./mongojs"
|
|
|
|
UpdateCompressor = require "./UpdateCompressor"
|
|
|
|
logger = require "logger-sharelatex"
|
2014-01-27 12:51:09 -05:00
|
|
|
async = require "async"
|
2014-01-27 11:26:58 -05:00
|
|
|
|
|
|
|
module.exports = HistoryManager =
|
|
|
|
getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
|
|
|
|
db.docHistory
|
|
|
|
.find(doc_id: ObjectId(doc_id.toString()))
|
|
|
|
.sort(timestamp: -1)
|
|
|
|
.limit(1)
|
|
|
|
.toArray (error, compressedUpdates) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback null, compressedUpdates[0] or null
|
|
|
|
|
|
|
|
deleteCompressedUpdate: (id, callback = (error) ->) ->
|
2014-01-27 13:20:38 -05:00
|
|
|
db.docHistory.remove({ _id: ObjectId(id.toString()) }, callback)
|
2014-01-27 11:26:58 -05:00
|
|
|
|
|
|
|
popLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
|
|
|
|
HistoryManager.getLastCompressedUpdate doc_id, (error, update) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if update?
|
|
|
|
HistoryManager.deleteCompressedUpdate update._id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, update
|
|
|
|
else
|
|
|
|
callback null, null
|
|
|
|
|
2014-01-27 12:51:09 -05:00
|
|
|
insertCompressedUpdates: (doc_id, updates, callback = (error) ->) ->
|
|
|
|
jobs = []
|
|
|
|
for update in updates
|
|
|
|
do (update) ->
|
|
|
|
jobs.push (callback) -> HistoryManager.insertCompressedUpdate doc_id, update, callback
|
|
|
|
async.series jobs, callback
|
|
|
|
|
|
|
|
insertCompressedUpdate: (doc_id, update, callback = (error) ->) ->
|
|
|
|
logger.log doc_id: doc_id, update: update, "inserting compressed update"
|
|
|
|
db.docHistory.insert {
|
|
|
|
doc_id: ObjectId(doc_id.toString())
|
|
|
|
op: update.op
|
|
|
|
meta: update.meta
|
2014-01-27 11:26:58 -05:00
|
|
|
}, callback
|
|
|
|
|
|
|
|
compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) ->
|
|
|
|
length = rawUpdates.length
|
|
|
|
if length == 0
|
|
|
|
return callback()
|
|
|
|
|
|
|
|
HistoryManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
|
|
|
|
HistoryManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"
|
|
|
|
callback()
|
|
|
|
|