2024-01-30 10:35:54 -05:00
|
|
|
const { promisifyAll } = require('@overleaf/promise-utils')
|
2020-05-06 06:09:33 -04:00
|
|
|
const RedisManager = require('./RedisManager')
|
|
|
|
const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
|
|
|
|
const PersistenceManager = require('./PersistenceManager')
|
|
|
|
const DiffCodec = require('./DiffCodec')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
const Metrics = require('./Metrics')
|
|
|
|
const HistoryManager = require('./HistoryManager')
|
|
|
|
const Errors = require('./Errors')
|
|
|
|
const RangesManager = require('./RangesManager')
|
|
|
|
|
|
|
|
const MAX_UNFLUSHED_AGE = 300 * 1000 // 5 mins, document should be flushed to mongo this time after a change
|
|
|
|
|
2024-01-30 10:35:54 -05:00
|
|
|
const DocumentManager = {
|
2021-11-30 08:26:20 -05:00
|
|
|
getDoc(projectId, docId, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.getDoc')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(
|
2021-07-13 07:04:42 -04:00
|
|
|
error,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
unflushedTime
|
2021-11-30 08:26:20 -05:00
|
|
|
) => {
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (lines == null || version == null) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ projectId, docId },
|
2021-07-13 07:04:42 -04:00
|
|
|
'doc not in redis so getting from persistence API'
|
|
|
|
)
|
2023-09-05 17:07:11 -04:00
|
|
|
PersistenceManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(error, lines, version, ranges, pathname, projectHistoryId) => {
|
2021-11-30 08:26:20 -05:00
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2023-09-05 17:07:11 -04:00
|
|
|
logger.debug(
|
|
|
|
{
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
},
|
|
|
|
'got doc from persistence API'
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
2023-09-05 17:07:11 -04:00
|
|
|
RedisManager.putDocInMemory(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
callback(
|
|
|
|
null,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges || {},
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
null,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:42 -04:00
|
|
|
} else {
|
2021-11-30 08:26:20 -05:00
|
|
|
callback(
|
2021-07-13 07:04:42 -04:00
|
|
|
null,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
unflushedTime,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
getDocAndRecentOps(projectId, docId, fromVersion, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.getDocAndRecentOps')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
DocumentManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(error, lines, version, ranges, pathname, projectHistoryId) => {
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (fromVersion === -1) {
|
2021-11-30 08:26:20 -05:00
|
|
|
callback(null, lines, version, [], ranges, pathname, projectHistoryId)
|
2021-07-13 07:04:42 -04:00
|
|
|
} else {
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.getPreviousDocOps(
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
fromVersion,
|
|
|
|
version,
|
2021-11-30 08:26:20 -05:00
|
|
|
(error, ops) => {
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
callback(
|
2021-07-13 07:04:42 -04:00
|
|
|
null,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ops,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
setDoc(projectId, docId, newLines, source, userId, undoing, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.setDoc')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newLines == null) {
|
|
|
|
return callback(new Error('No lines were provided to setDoc'))
|
|
|
|
}
|
|
|
|
|
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
DocumentManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(
|
2020-05-06 06:09:33 -04:00
|
|
|
error,
|
2021-07-13 07:04:42 -04:00
|
|
|
oldLines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
unflushedTime,
|
|
|
|
alreadyLoaded
|
2021-11-30 08:26:20 -05:00
|
|
|
) => {
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
|
|
|
|
if (
|
|
|
|
oldLines != null &&
|
|
|
|
oldLines.length > 0 &&
|
|
|
|
oldLines[0].text != null
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ docId, projectId, oldLines, newLines },
|
2021-07-13 07:04:42 -04:00
|
|
|
'document is JSON so not updating'
|
|
|
|
)
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ docId, projectId, oldLines, newLines },
|
2021-07-13 07:04:42 -04:00
|
|
|
'setting a document via http'
|
|
|
|
)
|
2021-11-30 08:26:20 -05:00
|
|
|
DiffCodec.diffAsShareJsOp(oldLines, newLines, (error, op) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (undoing) {
|
|
|
|
for (const o of op || []) {
|
|
|
|
o.u = true
|
|
|
|
} // Turn on undo flag for each op for track changes
|
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
doc: docId,
|
|
|
|
op,
|
|
|
|
v: version,
|
|
|
|
meta: {
|
|
|
|
type: 'external',
|
|
|
|
source,
|
|
|
|
user_id: userId,
|
|
|
|
},
|
|
|
|
}
|
2022-09-15 10:26:54 -04:00
|
|
|
// Keep track of external updates, whether they are for live documents
|
|
|
|
// (flush) or unloaded documents (evict), and whether the update is a no-op.
|
|
|
|
Metrics.inc('external-update', 1, {
|
|
|
|
status: op.length > 0 ? 'diff' : 'noop',
|
|
|
|
method: alreadyLoaded ? 'flush' : 'evict',
|
|
|
|
path: source,
|
|
|
|
})
|
2022-11-03 06:20:18 -04:00
|
|
|
const applyUpdateIfNeeded = cb => {
|
|
|
|
if (op.length === 0) {
|
|
|
|
// Do not notify the frontend about a noop update.
|
|
|
|
// We still want to execute the callback code below
|
|
|
|
// to evict the doc if we loaded it into redis for
|
|
|
|
// this update, otherwise the doc would never be
|
|
|
|
// removed from redis.
|
|
|
|
return cb(null)
|
|
|
|
}
|
|
|
|
UpdateManager.applyUpdate(projectId, docId, update, cb)
|
|
|
|
}
|
|
|
|
applyUpdateIfNeeded(error => {
|
2021-11-30 08:26:20 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
// If the document was loaded already, then someone has it open
|
|
|
|
// in a project, and the usual flushing mechanism will happen.
|
|
|
|
// Otherwise we should remove it immediately since nothing else
|
|
|
|
// is using it.
|
|
|
|
if (alreadyLoaded) {
|
2022-09-21 08:01:32 -04:00
|
|
|
DocumentManager.flushDocIfLoaded(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
callback(null, result)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2022-09-21 08:01:32 -04:00
|
|
|
)
|
2021-11-30 08:26:20 -05:00
|
|
|
} else {
|
2022-09-21 08:01:32 -04:00
|
|
|
DocumentManager.flushAndDeleteDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
{},
|
|
|
|
(error, result) => {
|
|
|
|
// There is no harm in flushing project history if the previous
|
|
|
|
// call failed and sometimes it is required
|
|
|
|
HistoryManager.flushProjectChangesAsync(projectId)
|
2021-11-30 08:26:20 -05:00
|
|
|
|
2022-09-21 08:01:32 -04:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
callback(null, result)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2022-09-21 08:01:32 -04:00
|
|
|
)
|
2021-11-30 08:26:20 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
flushDocIfLoaded(projectId, docId, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.flushDocIfLoaded')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(
|
2021-07-13 07:04:42 -04:00
|
|
|
error,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
unflushedTime,
|
|
|
|
lastUpdatedAt,
|
|
|
|
lastUpdatedBy
|
2021-11-30 08:26:20 -05:00
|
|
|
) => {
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (lines == null || version == null) {
|
2023-09-06 10:23:23 -04:00
|
|
|
Metrics.inc('flush-doc-if-loaded', 1, { status: 'not-loaded' })
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ projectId, docId },
|
2021-07-13 07:04:42 -04:00
|
|
|
'doc is not loaded so not flushing'
|
|
|
|
)
|
2021-11-30 08:26:20 -05:00
|
|
|
// TODO: return a flag to bail out, as we go on to remove doc from memory?
|
|
|
|
callback(null)
|
2023-09-06 10:23:23 -04:00
|
|
|
} else if (unflushedTime == null) {
|
|
|
|
Metrics.inc('flush-doc-if-loaded', 1, { status: 'unmodified' })
|
|
|
|
logger.debug(
|
|
|
|
{ projectId, docId },
|
|
|
|
'doc is not modified so not flushing'
|
|
|
|
)
|
|
|
|
callback(null)
|
2021-07-13 07:04:42 -04:00
|
|
|
} else {
|
2021-11-30 08:26:20 -05:00
|
|
|
logger.debug({ projectId, docId, version }, 'flushing doc')
|
2023-09-06 10:23:23 -04:00
|
|
|
Metrics.inc('flush-doc-if-loaded', 1, { status: 'modified' })
|
2021-11-30 08:26:20 -05:00
|
|
|
PersistenceManager.setDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
lastUpdatedAt,
|
|
|
|
lastUpdatedBy,
|
2022-09-21 08:01:32 -04:00
|
|
|
(error, result) => {
|
2021-11-30 08:26:20 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2022-09-21 08:01:32 -04:00
|
|
|
RedisManager.clearUnflushedTime(docId, err => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, result)
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
flushAndDeleteDoc(projectId, docId, options, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.flushAndDeleteDoc')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2022-09-21 08:01:32 -04:00
|
|
|
DocumentManager.flushDocIfLoaded(projectId, docId, (error, result) => {
|
2021-11-30 08:26:20 -05:00
|
|
|
if (error) {
|
|
|
|
if (options.ignoreFlushErrors) {
|
|
|
|
logger.warn(
|
|
|
|
{ projectId, docId, err: error },
|
|
|
|
'ignoring flush error while deleting document'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.removeDocFromMemory(projectId, docId, error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2022-09-21 08:01:32 -04:00
|
|
|
callback(null, result)
|
2021-11-30 08:26:20 -05:00
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
acceptChanges(projectId, docId, changeIds, _callback) {
|
|
|
|
if (changeIds == null) {
|
|
|
|
changeIds = []
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
const timer = new Metrics.Timer('docManager.acceptChanges')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
DocumentManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(error, lines, version, ranges) => {
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
if (lines == null || version == null) {
|
|
|
|
return callback(
|
2021-11-30 08:26:20 -05:00
|
|
|
new Errors.NotFoundError(`document not found: ${docId}`)
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
RangesManager.acceptChanges(changeIds, ranges, (error, newRanges) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.updateDocument(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
[],
|
|
|
|
newRanges,
|
|
|
|
{},
|
|
|
|
error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
deleteComment(projectId, docId, commentId, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.deleteComment')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
DocumentManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(error, lines, version, ranges) => {
|
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
if (lines == null || version == null) {
|
|
|
|
return callback(
|
2021-11-30 08:26:20 -05:00
|
|
|
new Errors.NotFoundError(`document not found: ${docId}`)
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
RangesManager.deleteComment(commentId, ranges, (error, newRanges) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.updateDocument(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
[],
|
|
|
|
newRanges,
|
|
|
|
{},
|
|
|
|
error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
renameDoc(projectId, docId, userId, update, projectHistoryId, _callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const timer = new Metrics.Timer('docManager.updateProject')
|
2021-11-30 08:26:20 -05:00
|
|
|
const callback = (...args) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2021-11-30 08:26:20 -05:00
|
|
|
_callback(...args)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
RedisManager.renameDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
userId,
|
2020-05-06 06:09:33 -04:00
|
|
|
update,
|
|
|
|
projectHistoryId,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
getDocAndFlushIfOld(projectId, docId, callback) {
|
|
|
|
DocumentManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(
|
2021-07-13 07:04:42 -04:00
|
|
|
error,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname,
|
|
|
|
projectHistoryId,
|
|
|
|
unflushedTime,
|
|
|
|
alreadyLoaded
|
2021-11-30 08:26:20 -05:00
|
|
|
) => {
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
// if doc was already loaded see if it needs to be flushed
|
|
|
|
if (
|
|
|
|
alreadyLoaded &&
|
|
|
|
unflushedTime != null &&
|
|
|
|
Date.now() - unflushedTime > MAX_UNFLUSHED_AGE
|
2020-05-06 06:09:33 -04:00
|
|
|
) {
|
2021-11-30 08:26:20 -05:00
|
|
|
DocumentManager.flushDocIfLoaded(projectId, docId, error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
callback(null, lines, version)
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
} else {
|
2021-11-30 08:26:20 -05:00
|
|
|
callback(null, lines, version)
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
resyncDocContents(projectId, docId, path, callback) {
|
|
|
|
logger.debug({ projectId, docId, path }, 'start resyncing doc contents')
|
|
|
|
RedisManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(error, lines, version, ranges, pathname, projectHistoryId) => {
|
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
// To avoid issues where the same docId appears with different paths,
|
2021-11-22 09:15:36 -05:00
|
|
|
// we use the path from the resyncProjectStructure update. If we used
|
|
|
|
// the path from the getDoc call to web then the two occurences of the
|
2021-11-30 08:26:20 -05:00
|
|
|
// docId would map to the same path, and this would be rejected by
|
2021-11-22 09:15:36 -05:00
|
|
|
// project-history as an unexpected resyncDocContent update.
|
2021-07-13 07:04:42 -04:00
|
|
|
if (lines == null || version == null) {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ projectId, docId },
|
2021-07-13 07:04:42 -04:00
|
|
|
'resyncing doc contents - not found in redis - retrieving from web'
|
|
|
|
)
|
2021-11-30 08:26:20 -05:00
|
|
|
PersistenceManager.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
2021-08-19 10:40:31 -04:00
|
|
|
{ peek: true },
|
2023-09-05 17:07:11 -04:00
|
|
|
(error, lines, version, ranges, pathname, projectHistoryId) => {
|
2021-11-30 08:26:20 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:42 -04:00
|
|
|
logger.error(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ projectId, docId, getDocError: error },
|
2021-07-13 07:04:42 -04:00
|
|
|
'resyncing doc contents - error retrieving from web'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
ProjectHistoryRedisManager.queueResyncDocContent(
|
|
|
|
projectId,
|
2023-09-05 17:07:11 -04:00
|
|
|
projectHistoryId,
|
2021-11-30 08:26:20 -05:00
|
|
|
docId,
|
2023-09-05 17:07:11 -04:00
|
|
|
lines,
|
|
|
|
version,
|
2021-11-22 09:15:36 -05:00
|
|
|
path, // use the path from the resyncProjectStructure update
|
2021-07-13 07:04:42 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2021-11-30 08:26:20 -05:00
|
|
|
{ projectId, docId },
|
2021-07-13 07:04:42 -04:00
|
|
|
'resyncing doc contents - doc in redis - will queue in redis'
|
|
|
|
)
|
2021-11-30 08:26:20 -05:00
|
|
|
ProjectHistoryRedisManager.queueResyncDocContent(
|
|
|
|
projectId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectHistoryId,
|
2021-11-30 08:26:20 -05:00
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
lines,
|
|
|
|
version,
|
2021-11-22 09:15:36 -05:00
|
|
|
path, // use the path from the resyncProjectStructure update
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
getDocWithLock(projectId, docId, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.getDoc,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
getDocAndRecentOpsWithLock(projectId, docId, fromVersion, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.getDocAndRecentOps,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
fromVersion,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
getDocAndFlushIfOldWithLock(projectId, docId, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.getDocAndFlushIfOld,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
setDocWithLock(projectId, docId, lines, source, userId, undoing, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.setDoc,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
lines,
|
|
|
|
source,
|
2021-11-30 08:26:20 -05:00
|
|
|
userId,
|
2020-05-06 06:09:33 -04:00
|
|
|
undoing,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
flushDocIfLoadedWithLock(projectId, docId, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.flushDocIfLoaded,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
flushAndDeleteDocWithLock(projectId, docId, options, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.flushAndDeleteDoc,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
options,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
acceptChangesWithLock(projectId, docId, changeIds, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.acceptChanges,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
changeIds,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
deleteCommentWithLock(projectId, docId, threadId, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.deleteComment,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
threadId,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
renameDocWithLock(
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
userId,
|
2020-05-06 06:09:33 -04:00
|
|
|
update,
|
|
|
|
projectHistoryId,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.renameDoc,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
userId,
|
2020-05-06 06:09:33 -04:00
|
|
|
update,
|
|
|
|
projectHistoryId,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
resyncDocContentsWithLock(projectId, docId, path, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
const UpdateManager = require('./UpdateManager')
|
2021-11-30 08:26:20 -05:00
|
|
|
UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:09:33 -04:00
|
|
|
DocumentManager.resyncDocContents,
|
2021-11-30 08:26:20 -05:00
|
|
|
projectId,
|
|
|
|
docId,
|
2021-11-22 09:15:36 -05:00
|
|
|
path,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
)
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-01-30 10:35:54 -05:00
|
|
|
|
|
|
|
module.exports = DocumentManager
|
|
|
|
module.exports.promises = promisifyAll(DocumentManager, {
|
|
|
|
multiResult: {
|
|
|
|
getDoc: [
|
|
|
|
'lines',
|
|
|
|
'version',
|
|
|
|
'ranges',
|
|
|
|
'pathname',
|
|
|
|
'projectHistoryId',
|
|
|
|
'unflushedTime',
|
|
|
|
'alreadyLoaded',
|
|
|
|
],
|
|
|
|
getDocWithLock: [
|
|
|
|
'lines',
|
|
|
|
'version',
|
|
|
|
'ranges',
|
|
|
|
'pathname',
|
|
|
|
'projectHistoryId',
|
|
|
|
'unflushedTime',
|
|
|
|
'alreadyLoaded',
|
|
|
|
],
|
|
|
|
getDocAndFlushIfOld: ['lines', 'version'],
|
|
|
|
getDocAndFlushIfOldWithLock: ['lines', 'version'],
|
|
|
|
getDocAndRecentOps: [
|
|
|
|
'lines',
|
|
|
|
'version',
|
|
|
|
'ops',
|
|
|
|
'ranges',
|
|
|
|
'pathname',
|
|
|
|
'projectHistoryId',
|
|
|
|
],
|
|
|
|
getDocAndRecentOpsWithLock: [
|
|
|
|
'lines',
|
|
|
|
'version',
|
|
|
|
'ops',
|
|
|
|
'ranges',
|
|
|
|
'pathname',
|
|
|
|
'projectHistoryId',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|