2014-02-12 05:40:42 -05:00
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
async = require('async')
|
2016-06-07 12:58:18 -04:00
|
|
|
rclient = require("./RedisBackend").createClient()
|
2014-02-12 05:40:42 -05:00
|
|
|
_ = require('underscore')
|
|
|
|
keys = require('./RedisKeyBuilder')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
metrics = require('./Metrics')
|
2016-05-31 08:24:19 -04:00
|
|
|
Errors = require "./Errors"
|
2015-03-25 12:53:20 -04:00
|
|
|
|
2014-02-10 10:17:08 -05:00
|
|
|
# Make times easy to read
|
|
|
|
minutes = 60 # seconds for Redis expire
|
|
|
|
|
|
|
|
module.exports = RedisManager =
|
2016-06-23 13:00:03 -04:00
|
|
|
rclient: rclient
|
|
|
|
|
2016-11-28 05:14:42 -05:00
|
|
|
putDocInMemory : (project_id, doc_id, docLines, version, track_changes, track_changes_entries, _callback)->
|
2014-02-12 05:40:42 -05:00
|
|
|
timer = new metrics.Timer("redis.put-doc")
|
2016-06-08 11:42:09 -04:00
|
|
|
callback = (error) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
timer.done()
|
2016-06-08 11:42:09 -04:00
|
|
|
_callback(error)
|
|
|
|
logger.log project_id:project_id, doc_id:doc_id, version: version, "putting doc in redis"
|
|
|
|
multi = rclient.multi()
|
|
|
|
multi.set keys.docLines(doc_id:doc_id), JSON.stringify(docLines)
|
|
|
|
multi.set keys.projectKey({doc_id:doc_id}), project_id
|
|
|
|
multi.set keys.docVersion(doc_id:doc_id), version
|
2016-11-28 05:14:42 -05:00
|
|
|
multi.set keys.trackChangesEnabled(doc_id:doc_id), if track_changes then "1" else "0"
|
|
|
|
multi.set keys.trackChangesEntries(doc_id:doc_id), JSON.stringify(track_changes_entries)
|
2016-06-08 11:42:09 -04:00
|
|
|
multi.exec (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
rclient.sadd keys.docsInProject(project_id:project_id), doc_id, callback
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-06-08 11:42:09 -04:00
|
|
|
removeDocFromMemory : (project_id, doc_id, _callback)->
|
2014-02-12 05:40:42 -05:00
|
|
|
logger.log project_id:project_id, doc_id:doc_id, "removing doc from redis"
|
2016-06-08 11:42:09 -04:00
|
|
|
callback = (err) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
if err?
|
|
|
|
logger.err project_id:project_id, doc_id:doc_id, err:err, "error removing doc from redis"
|
2016-06-08 11:42:09 -04:00
|
|
|
_callback(err)
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
2015-03-25 12:54:36 -04:00
|
|
|
logger.log project_id:project_id, doc_id:doc_id, "removed doc from redis"
|
2016-06-08 11:42:09 -04:00
|
|
|
_callback()
|
|
|
|
|
|
|
|
multi = rclient.multi()
|
|
|
|
multi.del keys.docLines(doc_id:doc_id)
|
|
|
|
multi.del keys.projectKey(doc_id:doc_id)
|
|
|
|
multi.del keys.docVersion(doc_id:doc_id)
|
2016-11-28 05:14:42 -05:00
|
|
|
multi.del keys.trackChangesEnabled(doc_id:doc_id)
|
|
|
|
multi.del keys.trackChangesEntries(doc_id:doc_id)
|
2016-06-08 11:42:09 -04:00
|
|
|
multi.exec (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
rclient.srem keys.docsInProject(project_id:project_id), doc_id, callback
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-11-28 05:14:42 -05:00
|
|
|
getDoc : (project_id, doc_id, callback = (error, lines, version, track_changes, track_changes_entries) ->)->
|
2014-02-12 05:40:42 -05:00
|
|
|
timer = new metrics.Timer("redis.get-doc")
|
2016-06-01 05:53:50 -04:00
|
|
|
multi = rclient.multi()
|
|
|
|
multi.get keys.docLines(doc_id:doc_id)
|
2014-02-12 05:40:42 -05:00
|
|
|
multi.get keys.docVersion(doc_id:doc_id)
|
2016-09-02 09:47:41 -04:00
|
|
|
multi.get keys.projectKey(doc_id:doc_id)
|
2016-11-28 05:14:42 -05:00
|
|
|
multi.get keys.trackChangesEnabled(doc_id:doc_id)
|
|
|
|
multi.get keys.trackChangesEntries(doc_id:doc_id)
|
2014-02-12 05:40:42 -05:00
|
|
|
multi.exec (error, result)->
|
|
|
|
timer.done()
|
|
|
|
return callback(error) if error?
|
2016-06-01 05:53:50 -04:00
|
|
|
try
|
|
|
|
docLines = JSON.parse result[0]
|
2016-11-28 05:14:42 -05:00
|
|
|
track_changes_entries = JSON.parse result[4]
|
2016-06-01 05:53:50 -04:00
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
version = parseInt(result[1] or 0, 10)
|
2016-09-02 09:47:41 -04:00
|
|
|
doc_project_id = result[2]
|
2016-11-28 05:14:42 -05:00
|
|
|
track_changes = (result[3] == "1")
|
2016-09-02 09:47:41 -04:00
|
|
|
# check doc is in requested project
|
|
|
|
if doc_project_id? and doc_project_id isnt project_id
|
|
|
|
logger.error project_id: project_id, doc_id: doc_id, doc_project_id: doc_project_id, "doc not in project"
|
|
|
|
return callback(new Errors.NotFoundError("document not found"))
|
2016-11-28 05:14:42 -05:00
|
|
|
callback null, docLines, version, track_changes, track_changes_entries
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
getDocVersion: (doc_id, callback = (error, version) ->) ->
|
|
|
|
rclient.get keys.docVersion(doc_id: doc_id), (error, version) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
version = parseInt(version, 10)
|
|
|
|
callback null, version
|
|
|
|
|
|
|
|
getPreviousDocOps: (doc_id, start, end, callback = (error, jsonOps) ->) ->
|
|
|
|
rclient.llen keys.docOps(doc_id: doc_id), (error, length) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
rclient.get keys.docVersion(doc_id: doc_id), (error, version) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
version = parseInt(version, 10)
|
|
|
|
first_version_in_redis = version - length
|
|
|
|
|
|
|
|
if start < first_version_in_redis or end > version
|
2016-05-31 08:24:19 -04:00
|
|
|
error = new Errors.OpRangeNotAvailableError("doc ops range is not loaded in redis")
|
|
|
|
logger.warn {err: error, doc_id, length, version, start, end}, "doc ops range is not loaded in redis"
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
start = start - first_version_in_redis
|
|
|
|
if end > -1
|
|
|
|
end = end - first_version_in_redis
|
|
|
|
|
|
|
|
if isNaN(start) or isNaN(end)
|
|
|
|
error = new Error("inconsistent version or lengths")
|
2016-05-31 08:24:19 -04:00
|
|
|
logger.error {err: error, doc_id, length, version, start, end}, "inconsistent version or length"
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
rclient.lrange keys.docOps(doc_id: doc_id), start, end, (error, jsonOps) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
try
|
|
|
|
ops = jsonOps.map (jsonOp) -> JSON.parse jsonOp
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
callback null, ops
|
|
|
|
|
2014-02-10 10:17:08 -05:00
|
|
|
DOC_OPS_TTL: 60 * minutes
|
|
|
|
DOC_OPS_MAX_LENGTH: 100
|
2016-11-28 05:14:42 -05:00
|
|
|
updateDocument : (doc_id, docLines, newVersion, appliedOps = [], track_changes_entries, callback = (error) ->)->
|
2016-08-23 11:00:46 -04:00
|
|
|
RedisManager.getDocVersion doc_id, (error, currentVersion) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(error) if error?
|
2016-08-23 11:00:46 -04:00
|
|
|
if currentVersion + appliedOps.length != newVersion
|
|
|
|
error = new Error("Version mismatch. '#{doc_id}' is corrupted.")
|
|
|
|
logger.error {err: error, doc_id, currentVersion, newVersion, opsLength: appliedOps.length}, "version mismatch"
|
|
|
|
return callback(error)
|
|
|
|
jsonOps = appliedOps.map (op) -> JSON.stringify op
|
|
|
|
multi = rclient.multi()
|
|
|
|
multi.set keys.docLines(doc_id:doc_id), JSON.stringify(docLines)
|
|
|
|
multi.set keys.docVersion(doc_id:doc_id), newVersion
|
2016-09-08 06:22:54 -04:00
|
|
|
if jsonOps.length > 0
|
|
|
|
multi.rpush keys.docOps(doc_id: doc_id), jsonOps...
|
2016-08-23 11:00:46 -04:00
|
|
|
multi.expire keys.docOps(doc_id: doc_id), RedisManager.DOC_OPS_TTL
|
|
|
|
multi.ltrim keys.docOps(doc_id: doc_id), -RedisManager.DOC_OPS_MAX_LENGTH, -1
|
2016-11-28 05:14:42 -05:00
|
|
|
multi.set keys.trackChangesEntries(doc_id:doc_id), JSON.stringify(track_changes_entries)
|
2016-08-23 11:00:46 -04:00
|
|
|
multi.exec (error, replys) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
getDocIdsInProject: (project_id, callback = (error, doc_ids) ->) ->
|
|
|
|
rclient.smembers keys.docsInProject(project_id: project_id), callback
|
2016-08-23 06:03:37 -04:00
|
|
|
|
2016-11-28 05:14:42 -05:00
|
|
|
setTrackChanges: (project_id, doc_id, track_changes_on, callback = (error) ->) ->
|
|
|
|
value = (if track_changes_on then "1" else "0")
|
|
|
|
rclient.set keys.trackChangesEnabled({doc_id}), value, callback
|