2014-02-24 12:43:27 -05:00
|
|
|
MongoManager = require "./MongoManager"
|
2014-02-25 11:48:42 -05:00
|
|
|
RedisManager = require "./RedisManager"
|
2014-01-27 11:26:58 -05:00
|
|
|
UpdateCompressor = require "./UpdateCompressor"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
module.exports = HistoryManager =
|
|
|
|
compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) ->
|
|
|
|
length = rawUpdates.length
|
|
|
|
if length == 0
|
|
|
|
return callback()
|
|
|
|
|
2014-02-24 12:43:27 -05:00
|
|
|
MongoManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) ->
|
2014-01-27 11:26:58 -05:00
|
|
|
return callback(error) if error?
|
2014-02-25 07:27:42 -05:00
|
|
|
|
|
|
|
# Ensure that raw updates start where lastCompressedUpdate left off
|
|
|
|
if lastCompressedUpdate?
|
|
|
|
rawUpdates = rawUpdates.slice(0)
|
|
|
|
while rawUpdates[0]? and rawUpdates[0].v <= lastCompressedUpdate.v
|
|
|
|
rawUpdates.shift()
|
|
|
|
|
|
|
|
if rawUpdates[0]? and rawUpdates[0].v != lastCompressedUpdate.v + 1
|
|
|
|
return callback new Error("Tried to apply raw op at version #{rawUpdates[0].v} to last compressed update with version #{lastCompressedUpdate.v}")
|
|
|
|
|
2014-01-27 11:26:58 -05:00
|
|
|
compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
|
2014-02-24 12:43:27 -05:00
|
|
|
MongoManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->
|
2014-01-27 11:26:58 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"
|
|
|
|
callback()
|
|
|
|
|
2014-02-25 11:48:42 -05:00
|
|
|
REDIS_READ_BATCH_SIZE: 100
|
2014-02-25 07:27:42 -05:00
|
|
|
processUncompressedUpdates: (doc_id, callback = (error) ->) ->
|
2014-02-25 11:48:42 -05:00
|
|
|
RedisManager.getOldestRawUpdates doc_id, HistoryManager.REDIS_READ_BATCH_SIZE, (error, rawUpdates) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
HistoryManager.compressAndSaveRawUpdates doc_id, rawUpdates, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
RedisManager.deleteOldestRawUpdates doc_id, HistoryManager.REDIS_READ_BATCH_SIZE, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
|
|
|
processUncompressUpdatesWithLock: (doc_id, callback = (error) ->) ->
|
|
|
|
|