2020-02-17 12:34:21 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:04 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:34:28 -05:00
|
|
|
let MongoManager
|
2020-09-10 07:58:06 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2020-02-17 12:34:28 -05:00
|
|
|
const PackManager = require('./PackManager')
|
|
|
|
const async = require('async')
|
|
|
|
const _ = require('underscore')
|
2020-11-25 06:57:20 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2014-02-24 12:43:27 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
module.exports = MongoManager = {
|
2023-03-21 08:21:51 -04:00
|
|
|
getLastCompressedUpdate(docId, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
return db.docHistory
|
2020-09-10 07:58:06 -04:00
|
|
|
.find(
|
2023-03-21 08:21:51 -04:00
|
|
|
{ doc_id: ObjectId(docId.toString()) },
|
2020-09-10 07:58:06 -04:00
|
|
|
// only return the last entry in a pack
|
|
|
|
{ projection: { pack: { $slice: -1 } } }
|
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
.sort({ v: -1 })
|
|
|
|
.limit(1)
|
2020-06-04 04:24:21 -04:00
|
|
|
.toArray(function (error, compressedUpdates) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, compressedUpdates[0] || null)
|
|
|
|
})
|
|
|
|
},
|
2014-02-24 12:43:27 -05:00
|
|
|
|
2023-03-21 08:21:51 -04:00
|
|
|
peekLastCompressedUpdate(docId, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
// under normal use we pass back the last update as
|
|
|
|
// callback(null,update,version).
|
|
|
|
//
|
|
|
|
// when we have an existing last update but want to force a new one
|
|
|
|
// to start, we pass it back as callback(null,null,version), just
|
|
|
|
// giving the version so we can check consistency.
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
return MongoManager.getLastCompressedUpdate(
|
2023-03-21 08:21:51 -04:00
|
|
|
docId,
|
2021-07-13 07:04:43 -04:00
|
|
|
function (error, update) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (update != null) {
|
|
|
|
if (update.broken) {
|
|
|
|
// marked as broken so we will force a new op
|
|
|
|
return callback(null, null)
|
|
|
|
} else if (update.pack != null) {
|
|
|
|
if (update.finalised) {
|
|
|
|
// no more ops can be appended
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
update.pack[0] != null ? update.pack[0].v : undefined
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
update,
|
|
|
|
update.pack[0] != null ? update.pack[0].v : undefined
|
|
|
|
)
|
|
|
|
}
|
2020-02-17 12:34:28 -05:00
|
|
|
} else {
|
2021-07-13 07:04:43 -04:00
|
|
|
return callback(null, update, update.v)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-13 07:04:43 -04:00
|
|
|
return PackManager.getLastPackFromIndex(
|
2023-03-21 08:21:51 -04:00
|
|
|
docId,
|
2021-07-13 07:04:43 -04:00
|
|
|
function (error, pack) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(pack != null ? pack.inS3 : undefined) != null &&
|
|
|
|
(pack != null ? pack.v_end : undefined) != null
|
|
|
|
) {
|
|
|
|
return callback(null, null, pack.v_end)
|
|
|
|
}
|
|
|
|
return callback(null, null)
|
|
|
|
}
|
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
)
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
2014-02-24 12:43:27 -05:00
|
|
|
|
2023-03-21 08:21:51 -04:00
|
|
|
backportProjectId(projectId, docId, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-09-10 07:58:06 -04:00
|
|
|
return db.docHistory.updateMany(
|
2020-02-17 12:34:28 -05:00
|
|
|
{
|
2023-03-21 08:21:51 -04:00
|
|
|
doc_id: ObjectId(docId.toString()),
|
2021-07-13 07:04:43 -04:00
|
|
|
project_id: { $exists: false },
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
{
|
2023-03-21 08:21:51 -04:00
|
|
|
$set: { project_id: ObjectId(projectId.toString()) },
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-03-21 10:40:51 -04:00
|
|
|
|
2023-03-21 08:21:51 -04:00
|
|
|
getProjectMetaData(projectId, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-09-10 09:54:18 -04:00
|
|
|
return db.projectHistoryMetaData.findOne(
|
2020-02-17 12:34:28 -05:00
|
|
|
{
|
2023-03-21 08:21:51 -04:00
|
|
|
project_id: ObjectId(projectId.toString()),
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
2020-09-10 09:54:18 -04:00
|
|
|
callback
|
2020-02-17 12:34:28 -05:00
|
|
|
)
|
|
|
|
},
|
2014-03-28 12:01:34 -04:00
|
|
|
|
2023-03-21 08:21:51 -04:00
|
|
|
setProjectMetaData(projectId, metadata, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-09-10 07:58:06 -04:00
|
|
|
return db.projectHistoryMetaData.updateOne(
|
2020-02-17 12:34:28 -05:00
|
|
|
{
|
2023-03-21 08:21:51 -04:00
|
|
|
project_id: ObjectId(projectId),
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:43 -04:00
|
|
|
$set: metadata,
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:43 -04:00
|
|
|
upsert: true,
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-03-28 12:01:34 -04:00
|
|
|
|
2023-03-21 08:21:51 -04:00
|
|
|
upgradeHistory(projectId, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
// preserve the project's existing history
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2020-09-10 07:58:06 -04:00
|
|
|
return db.docHistory.updateMany(
|
2020-02-17 12:34:28 -05:00
|
|
|
{
|
2023-03-21 08:21:51 -04:00
|
|
|
project_id: ObjectId(projectId),
|
2020-02-17 12:34:28 -05:00
|
|
|
temporary: true,
|
2021-07-13 07:04:43 -04:00
|
|
|
expiresAt: { $exists: true },
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: { temporary: false },
|
2021-07-13 07:04:43 -04:00
|
|
|
$unset: { expiresAt: '' },
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2016-04-07 09:37:53 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
ensureIndices() {
|
|
|
|
// For finding all updates that go into a diff for a doc
|
|
|
|
db.docHistory.ensureIndex({ doc_id: 1, v: 1 }, { background: true })
|
|
|
|
// For finding all updates that affect a project
|
|
|
|
db.docHistory.ensureIndex(
|
|
|
|
{ project_id: 1, 'meta.end_ts': 1 },
|
|
|
|
{ background: true }
|
|
|
|
)
|
|
|
|
// For finding updates that don't yet have a project_id and need it inserting
|
|
|
|
db.docHistory.ensureIndex(
|
|
|
|
{ doc_id: 1, project_id: 1 },
|
|
|
|
{ background: true }
|
|
|
|
)
|
|
|
|
// For finding project meta-data
|
|
|
|
db.projectHistoryMetaData.ensureIndex(
|
|
|
|
{ project_id: 1 },
|
|
|
|
{ background: true }
|
|
|
|
)
|
|
|
|
// TTL index for auto deleting week old temporary ops
|
|
|
|
db.docHistory.ensureIndex(
|
|
|
|
{ expiresAt: 1 },
|
|
|
|
{ expireAfterSeconds: 0, background: true }
|
|
|
|
)
|
|
|
|
// For finding packs to be checked for archiving
|
|
|
|
db.docHistory.ensureIndex({ last_checked: 1 }, { background: true })
|
|
|
|
// For finding archived packs
|
|
|
|
return db.docHistoryIndex.ensureIndex(
|
|
|
|
{ project_id: 1 },
|
|
|
|
{ background: true }
|
|
|
|
)
|
2021-07-13 07:04:43 -04:00
|
|
|
},
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-07-13 07:04:43 -04:00
|
|
|
;['getLastCompressedUpdate', 'getProjectMetaData', 'setProjectMetaData'].map(
|
|
|
|
method =>
|
|
|
|
metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
|
2020-02-17 12:34:28 -05:00
|
|
|
)
|