2014-02-25 11:27:14 -05:00
|
|
|
Settings = require "settings-sharelatex"
|
2014-09-26 12:21:33 -04:00
|
|
|
redis = require("redis-sharelatex")
|
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
2014-02-25 11:27:14 -05:00
|
|
|
|
2014-03-21 09:17:58 -04:00
|
|
|
rawUpdatesKey = (doc_id) -> "UncompressedHistoryOps:#{doc_id}"
|
|
|
|
docsWithHistoryOpsKey = (project_id) -> "DocsWithHistoryOps:#{project_id}"
|
2014-02-25 11:27:14 -05:00
|
|
|
|
|
|
|
module.exports = RedisManager =
|
2015-10-08 09:40:42 -04:00
|
|
|
|
|
|
|
getOldestDocUpdates: (doc_id, batchSize, callback = (error, jsonUpdates) ->) ->
|
2014-03-21 09:17:58 -04:00
|
|
|
key = rawUpdatesKey(doc_id)
|
2015-10-08 09:40:42 -04:00
|
|
|
rclient.lrange key, 0, batchSize - 1, callback
|
|
|
|
|
|
|
|
expandDocUpdates: (jsonUpdates, callback = (error, rawUpdates) ->) ->
|
|
|
|
try
|
|
|
|
rawUpdates = ( JSON.parse(update) for update in jsonUpdates or [] )
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
callback null, rawUpdates
|
2014-02-25 11:27:14 -05:00
|
|
|
|
2015-10-08 09:40:42 -04:00
|
|
|
deleteAppliedDocUpdates: (project_id, doc_id, docUpdates, callback = (error) ->) ->
|
2014-03-21 09:17:58 -04:00
|
|
|
multi = rclient.multi()
|
2015-11-26 10:16:54 -05:00
|
|
|
# Delete all the updates which have been applied (exact match)
|
2015-10-08 09:40:42 -04:00
|
|
|
for update in docUpdates or []
|
|
|
|
multi.lrem rawUpdatesKey(doc_id), 0, update
|
2015-11-26 10:16:54 -05:00
|
|
|
# It's ok to delete the doc_id from the set here. Even though the list
|
|
|
|
# of updates may not be empty, we will continue to process it until it is.
|
2014-03-21 09:17:58 -04:00
|
|
|
multi.srem docsWithHistoryOpsKey(project_id), doc_id
|
|
|
|
multi.exec (error, results) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null
|
2014-03-21 09:48:14 -04:00
|
|
|
|
|
|
|
getDocIdsWithHistoryOps: (project_id, callback = (error, doc_ids) ->) ->
|
|
|
|
rclient.smembers docsWithHistoryOpsKey(project_id), callback
|