HistoryManager.coffee 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. MongoManager = require "./MongoManager"
  2. UpdateCompressor = require "./UpdateCompressor"
  3. logger = require "logger-sharelatex"
  4. module.exports = HistoryManager =
  5. compressAndSaveRawUpdates: (doc_id, rawUpdates, callback = (error) ->) ->
  6. length = rawUpdates.length
  7. if length == 0
  8. return callback()
  9. MongoManager.popLastCompressedUpdate doc_id, (error, lastCompressedUpdate) ->
  10. return callback(error) if error?
  11. # Ensure that raw updates start where lastCompressedUpdate left off
  12. if lastCompressedUpdate?
  13. rawUpdates = rawUpdates.slice(0)
  14. while rawUpdates[0]? and rawUpdates[0].v <= lastCompressedUpdate.v
  15. rawUpdates.shift()
  16. if rawUpdates[0]? and rawUpdates[0].v != lastCompressedUpdate.v + 1
  17. return callback new Error("Tried to apply raw op at version #{rawUpdates[0].v} to last compressed update with version #{lastCompressedUpdate.v}")
  18. compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
  19. MongoManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->
  20. return callback(error) if error?
  21. logger.log doc_id: doc_id, rawUpdatesLength: length, compressedUpdatesLength: compressedUpdates.length, "compressed doc updates"
  22. callback()
  23. processUncompressedUpdates: (doc_id, callback = (error) ->) ->
  24. # Get lock - here or elsewhere?
  25. # Get batch from Redis left hand side (oldest)
  26. # pass batch to compressAndSaveRawUpdates
  27. # Delete batch from redis
  28. # release lock