RedisManager.coffee 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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
  29. # iterate over keys asynchronously using redis scan (non-blocking)
  30. _getKeys: (pattern, callback) ->
  31. cursor = 0 # redis iterator
  32. keySet = {} # use hash to avoid duplicate results
  33. # scan over all keys looking for pattern
  34. doIteration = (cb) ->
  35. rclient.scan cursor, "MATCH", pattern, "COUNT", 1000, (error, reply) ->
  36. return callback(error) if error?
  37. [cursor, keys] = reply
  38. for key in keys
  39. keySet[key] = true
  40. if cursor == '0' # note redis returns string result not numeric
  41. return callback(null, Object.keys(keySet))
  42. else
  43. doIteration()
  44. doIteration()
  45. # extract ids from keys like DocsWithHistoryOps:57fd0b1f53a8396d22b2c24b
  46. _extractIds: (keyList) ->
  47. ids = (key.split(":")[1] for key in keyList)
  48. return ids
  49. # this will only work on single node redis, not redis cluster
  50. getProjectIdsWithHistoryOps: (callback = (error, project_ids) ->) ->
  51. RedisManager._getKeys docsWithHistoryOpsKey("*"), (error, project_keys) ->
  52. return callback(error) if error?
  53. project_ids = RedisManager._extractIds project_keys
  54. callback(error, project_ids)
  55. # this will only work on single node redis, not redis cluster
  56. getAllDocIdsWithHistoryOps: (callback = (error, doc_ids) ->) ->
  57. # return all the docids, to find dangling history entries after
  58. # everything is flushed.
  59. RedisManager._getKeys rawUpdatesKey("*"), (error, doc_keys) ->
  60. return callback(error) if error?
  61. doc_ids = RedisManager._extractIds doc_keys
  62. callback(error, doc_ids)