RedisManager.coffee 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. Settings = require "settings-sharelatex"
  2. redis = require("redis-sharelatex")
  3. rclient = redis.createClient(Settings.redis.web)
  4. rawUpdatesKey = (doc_id) -> "UncompressedHistoryOps:#{doc_id}"
  5. docsWithHistoryOpsKey = (project_id) -> "DocsWithHistoryOps:#{project_id}"
  6. module.exports = RedisManager =
  7. getOldestDocUpdates: (doc_id, batchSize, callback = (error, jsonUpdates) ->) ->
  8. key = rawUpdatesKey(doc_id)
  9. rclient.lrange key, 0, batchSize - 1, callback
  10. expandDocUpdates: (jsonUpdates, callback = (error, rawUpdates) ->) ->
  11. try
  12. rawUpdates = ( JSON.parse(update) for update in jsonUpdates or [] )
  13. catch e
  14. return callback(e)
  15. callback null, rawUpdates
  16. deleteAppliedDocUpdates: (project_id, doc_id, docUpdates, callback = (error) ->) ->
  17. multi = rclient.multi()
  18. # Delete all the updates which have been applied (exact match)
  19. for update in docUpdates or []
  20. multi.lrem rawUpdatesKey(doc_id), 0, update
  21. # It's ok to delete the doc_id from the set here. Even though the list
  22. # of updates may not be empty, we will continue to process it until it is.
  23. multi.srem docsWithHistoryOpsKey(project_id), doc_id
  24. multi.exec (error, results) ->
  25. return callback(error) if error?
  26. callback null
  27. getDocIdsWithHistoryOps: (project_id, callback = (error, doc_ids) ->) ->
  28. rclient.smembers docsWithHistoryOpsKey(project_id), callback