compute project state hash from sorted docs/files

This commit is contained in:
Brian Gough 2017-08-16 10:49:29 +01:00
parent c91599bfeb
commit 90ff58b820
2 changed files with 24 additions and 5 deletions

View file

@ -1,6 +1,7 @@
Settings = require "settings-sharelatex" Settings = require "settings-sharelatex"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
crypto = require "crypto" crypto = require "crypto"
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
# The "state" of a project is a hash of the relevant attributes in the # The "state" of a project is a hash of the relevant attributes in the
# project object in this case we only need the rootFolder. # project object in this case we only need the rootFolder.
@ -16,12 +17,15 @@ crypto = require "crypto"
# The docupdater is responsible for setting the key in redis, and # The docupdater is responsible for setting the key in redis, and
# unsetting it if it removes any documents from the doc updater. # unsetting it if it removes any documents from the doc updater.
buildState = (project) -> buildState = (s) ->
json = JSON.stringify(project.rootFolder) return crypto.createHash('sha1').update(s, 'utf8').digest('hex')
return crypto.createHash('sha1').update(json, 'utf8').digest('hex')
module.exports = ClsiStateManager = module.exports = ClsiStateManager =
computeHash: (project, callback = (err, hash) ->) -> computeHash: (project, callback = (err, hash) ->) ->
hash = buildState(project) ProjectEntityHandler.getAllEntitiesFromProject project, (err, docs, files) ->
fileList = ("#{f.file._id}:#{f.file.rev}:#{f.file.created}:#{f.path}" for f in files or [])
docList = ("#{d.doc._id}:#{d.path}" for d in docs or [])
sortedEntityList = [docList..., fileList...].sort()
hash = buildState(sortedEntityList.join("\n"))
callback(null, hash) callback(null, hash)

View file

@ -77,6 +77,21 @@ module.exports = ProjectEntityHandler =
processFolder "/", project.rootFolder[0] processFolder "/", project.rootFolder[0]
callback null, folders callback null, folders
getAllEntitiesFromProject: (project, callback) ->
logger.log project:project, "getting all files for project"
@getAllFoldersFromProject project, (err, folders = {}) ->
return callback(err) if err?
docs = []
files = []
for folderPath, folder of folders
for doc in (folder.docs or [])
if doc?
docs.push({path: path.join(folderPath, doc.name), doc:doc})
for file in (folder.fileRefs or [])
if file?
files.push({path: path.join(folderPath, file.name), file:file})
callback null, docs, files
getAllDocPathsFromProject: (project, callback) -> getAllDocPathsFromProject: (project, callback) ->
logger.log project:project, "getting all docs for project" logger.log project:project, "getting all docs for project"
@getAllFoldersFromProject project, (err, folders = {}) -> @getAllFoldersFromProject project, (err, folders = {}) ->