RedisManager.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Settings = require "settings-sharelatex"
  2. redis = require("redis-sharelatex")
  3. rclient = redis.createClient(Settings.redis.history)
  4. Keys = Settings.redis.history.key_schema
  5. module.exports = RedisManager =
  6. getOldestDocUpdates: (doc_id, batchSize, callback = (error, jsonUpdates) ->) ->
  7. key = Keys.uncompressedHistoryOps({doc_id})
  8. rclient.lrange key, 0, batchSize - 1, callback
  9. expandDocUpdates: (jsonUpdates, callback = (error, rawUpdates) ->) ->
  10. try
  11. rawUpdates = ( JSON.parse(update) for update in jsonUpdates or [] )
  12. catch e
  13. return callback(e)
  14. callback null, rawUpdates
  15. deleteAppliedDocUpdates: (project_id, doc_id, docUpdates, callback = (error) ->) ->
  16. multi = rclient.multi()
  17. # Delete all the updates which have been applied (exact match)
  18. for update in docUpdates or []
  19. multi.lrem Keys.uncompressedHistoryOps({doc_id}), 0, update
  20. multi.exec (error, results) ->
  21. return callback(error) if error?
  22. # It's ok to delete the doc_id from the set here. Even though the list
  23. # of updates may not be empty, we will continue to process it until it is.
  24. rclient.srem Keys.docsWithHistoryOps({project_id}), doc_id, (error) ->
  25. return callback(error) if error?
  26. callback null
  27. getDocIdsWithHistoryOps: (project_id, callback = (error, doc_ids) ->) ->
  28. rclient.smembers Keys.docsWithHistoryOps({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. return callback(new Error("not supported")) if rclient.nodes?
  52. RedisManager._getKeys Keys.docsWithHistoryOps({project_id:"*"}), (error, project_keys) ->
  53. return callback(error) if error?
  54. project_ids = RedisManager._extractIds project_keys
  55. callback(error, project_ids)
  56. # this will only work on single node redis, not redis cluster
  57. getAllDocIdsWithHistoryOps: (callback = (error, doc_ids) ->) ->
  58. return callback(new Error("not supported")) if rclient.nodes?
  59. # return all the docids, to find dangling history entries after
  60. # everything is flushed.
  61. RedisManager._getKeys Keys.uncompressedHistoryOps({doc_id:"*"}), (error, doc_keys) ->
  62. return callback(error) if error?
  63. doc_ids = RedisManager._extractIds doc_keys
  64. callback(error, doc_ids)