Merge pull request #2827 from overleaf/em-faster-clones

Make a single Mongo update when cloning projects

GitOrigin-RevId: abd4069bd8854d84c413bc8f890583e647b7c18e
This commit is contained in:
Eric Mc Sween
2020-05-14 03:26:15 +00:00
committed by Copybot
parent 0e9e57cd7c
commit ae82366122
5 changed files with 382 additions and 731 deletions
@@ -8,7 +8,6 @@ const { Doc } = require('../../models/Doc')
const DocstoreManager = require('../Docstore/DocstoreManager')
const DocumentUpdaterHandler = require('../../Features/DocumentUpdater/DocumentUpdaterHandler')
const Errors = require('../Errors/Errors')
const { File } = require('../../models/File')
const FileStoreHandler = require('../FileStore/FileStoreHandler')
const LockManager = require('../../infrastructure/LockManager')
const { Project } = require('../../models/Project')
@@ -71,152 +70,6 @@ function wrapWithLock(methodWithoutLock) {
}
const ProjectEntityUpdateHandler = {
copyFileFromExistingProjectWithProject: wrapWithLock({
beforeLock(next) {
return function(
projectId,
project,
folderId,
originalProjectId,
originalFileRef,
userId,
callback
) {
logger.log(
{ projectId, folderId, originalProjectId, originalFileRef },
'copying file in s3 with project'
)
folderId = ProjectEntityMongoUpdateHandler._confirmFolder(
project,
folderId
)
if (originalFileRef == null) {
logger.err(
{ projectId, folderId, originalProjectId, originalFileRef },
'file trying to copy is null'
)
return callback()
}
// convert any invalid characters in original file to '_'
const fileProperties = {
name: SafePath.clean(originalFileRef.name)
}
if (originalFileRef.linkedFileData != null) {
fileProperties.linkedFileData = originalFileRef.linkedFileData
}
if (originalFileRef.hash != null) {
fileProperties.hash = originalFileRef.hash
}
const fileRef = new File(fileProperties)
FileStoreHandler.copyFile(
originalProjectId,
originalFileRef._id,
project._id,
fileRef._id,
(err, fileStoreUrl) => {
if (err != null) {
logger.warn(
{
err,
projectId,
folderId,
originalProjectId,
originalFileRef
},
'error coping file in s3'
)
return callback(err)
}
next(
projectId,
project,
folderId,
originalProjectId,
originalFileRef,
userId,
fileRef,
fileStoreUrl,
callback
)
}
)
}
},
withLock(
projectId,
project,
folderId,
originalProjectId,
originalFileRef,
userId,
fileRef,
fileStoreUrl,
callback
) {
const projectHistoryId =
project.overleaf &&
project.overleaf.history &&
project.overleaf.history.id
ProjectEntityMongoUpdateHandler._putElement(
project,
folderId,
fileRef,
'file',
(err, result, newProject) => {
if (err != null) {
logger.warn(
{ err, projectId, folderId },
'error putting element as part of copy'
)
return callback(err)
}
TpdsUpdateSender.addFile(
{
project_id: projectId,
file_id: fileRef._id,
path: result && result.path && result.path.fileSystem,
rev: fileRef.rev,
project_name: project.name
},
err => {
if (err != null) {
logger.err(
{
err,
projectId,
folderId,
originalProjectId,
originalFileRef
},
'error sending file to tpds worker'
)
}
const newFiles = [
{
file: fileRef,
path: result && result.path && result.path.fileSystem,
url: fileStoreUrl
}
]
DocumentUpdaterHandler.updateProjectStructure(
projectId,
projectHistoryId,
userId,
{ newFiles, newProject },
error => {
if (error != null) {
return callback(error)
}
callback(null, fileRef, folderId)
}
)
}
)
}
)
}
}),
updateDocLines(
projectId,
docId,
@@ -1796,7 +1649,6 @@ module.exports = ProjectEntityUpdateHandler
module.exports.promises = promisifyAll(ProjectEntityUpdateHandler, {
without: ['isPathValidForRootDoc'],
multiResult: {
copyFileFromExistingProjectWithProject: ['fileRef', 'folderId'],
_addDocAndSendToTpds: ['result', 'project'],
addDoc: ['doc', 'folderId'],
addDocWithRanges: ['doc', 'folderId'],