2014-02-24 12:43:27 -05:00
|
|
|
{db, ObjectId} = require "./mongojs"
|
2015-02-17 06:14:13 -05:00
|
|
|
PackManager = require "./PackManager"
|
2014-02-24 12:43:27 -05:00
|
|
|
async = require "async"
|
2015-12-11 10:56:47 -05:00
|
|
|
_ = require "underscore"
|
2017-03-16 11:17:38 -04:00
|
|
|
metrics = require 'metrics-sharelatex'
|
2017-03-17 10:58:35 -04:00
|
|
|
logger = require 'logger-sharelatex'
|
2014-02-24 12:43:27 -05:00
|
|
|
|
|
|
|
module.exports = MongoManager =
|
|
|
|
getLastCompressedUpdate: (doc_id, callback = (error, update) ->) ->
|
|
|
|
db.docHistory
|
2015-12-11 10:56:47 -05:00
|
|
|
.find(doc_id: ObjectId(doc_id.toString()), {pack: {$slice:-1}}) # only return the last entry in a pack
|
2014-03-07 09:02:16 -05:00
|
|
|
.sort( v: -1 )
|
2014-02-24 12:43:27 -05:00
|
|
|
.limit(1)
|
|
|
|
.toArray (error, compressedUpdates) ->
|
|
|
|
return callback(error) if error?
|
2015-12-11 10:56:47 -05:00
|
|
|
callback null, compressedUpdates[0] or null
|
2014-02-24 12:43:27 -05:00
|
|
|
|
2015-10-08 11:10:48 -04:00
|
|
|
peekLastCompressedUpdate: (doc_id, callback = (error, update, version) ->) ->
|
2015-09-23 08:22:38 -04:00
|
|
|
# under normal use we pass back the last update as
|
2016-01-15 10:02:09 -05:00
|
|
|
# callback(null,update,version).
|
2015-09-23 08:22:38 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2014-02-24 12:43:27 -05:00
|
|
|
MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if update?
|
2016-04-06 08:30:09 -04:00
|
|
|
if update.broken # marked as broken so we will force a new op
|
2015-10-16 06:24:50 -04:00
|
|
|
return callback null, null
|
2016-01-15 10:02:09 -05:00
|
|
|
else if update.pack?
|
2016-04-06 08:30:09 -04:00
|
|
|
if update.finalised # no more ops can be appended
|
|
|
|
return callback null, null, update.pack[0]?.v
|
|
|
|
else
|
|
|
|
return callback null, update, update.pack[0]?.v
|
2015-09-23 08:22:38 -04:00
|
|
|
else
|
2016-01-15 10:02:09 -05:00
|
|
|
return callback null, update, update.v
|
2014-02-24 12:43:27 -05:00
|
|
|
else
|
2016-02-08 11:22:42 -05:00
|
|
|
PackManager.getLastPackFromIndex doc_id, (error, pack) ->
|
2016-01-15 10:02:09 -05:00
|
|
|
return callback(error) if error?
|
2016-02-08 11:22:42 -05:00
|
|
|
return callback(null, null, pack.v_end) if pack?.inS3? and pack?.v_end?
|
2016-01-15 10:02:09 -05:00
|
|
|
callback null, null
|
2014-02-24 12:43:27 -05:00
|
|
|
|
2014-03-21 10:40:51 -04:00
|
|
|
backportProjectId: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
db.docHistory.update {
|
|
|
|
doc_id: ObjectId(doc_id.toString())
|
|
|
|
project_id: { $exists: false }
|
|
|
|
}, {
|
|
|
|
$set: { project_id: ObjectId(project_id.toString()) }
|
|
|
|
}, {
|
|
|
|
multi: true
|
|
|
|
}, callback
|
|
|
|
|
2014-03-28 12:01:34 -04:00
|
|
|
getProjectMetaData: (project_id, callback = (error, metadata) ->) ->
|
|
|
|
db.projectHistoryMetaData.find {
|
|
|
|
project_id: ObjectId(project_id.toString())
|
|
|
|
}, (error, results) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, results[0]
|
|
|
|
|
|
|
|
setProjectMetaData: (project_id, metadata, callback = (error) ->) ->
|
|
|
|
db.projectHistoryMetaData.update {
|
|
|
|
project_id: ObjectId(project_id)
|
|
|
|
}, {
|
|
|
|
$set: metadata
|
|
|
|
}, {
|
|
|
|
upsert: true
|
|
|
|
}, callback
|
|
|
|
|
2016-04-07 09:37:53 -04:00
|
|
|
upgradeHistory: (project_id, callback = (error) ->) ->
|
|
|
|
# preserve the project's existing history
|
|
|
|
db.docHistory.update {
|
|
|
|
project_id: ObjectId(project_id)
|
|
|
|
temporary: true
|
|
|
|
expiresAt: {$exists: true}
|
|
|
|
}, {
|
|
|
|
$set: {temporary: false}
|
|
|
|
$unset: {expiresAt: ""}
|
|
|
|
}, {
|
|
|
|
multi: true
|
|
|
|
}, callback
|
|
|
|
|
2014-03-28 12:01:34 -04:00
|
|
|
ensureIndices: () ->
|
2015-02-17 08:41:31 -05:00
|
|
|
# For finding all updates that go into a diff for a doc
|
2014-05-16 12:00:30 -04:00
|
|
|
db.docHistory.ensureIndex { doc_id: 1, v: 1 }, { background: true }
|
2015-02-17 08:41:31 -05:00
|
|
|
# For finding all updates that affect a project
|
2016-09-30 09:34:13 -04:00
|
|
|
db.docHistory.ensureIndex { project_id: 1, "meta.end_ts": 1 }, { background: true }
|
2015-08-31 17:13:18 -04:00
|
|
|
# For finding updates that don't yet have a project_id and need it inserting
|
2014-05-16 12:00:30 -04:00
|
|
|
db.docHistory.ensureIndex { doc_id: 1, project_id: 1 }, { background: true }
|
2014-03-28 12:01:34 -04:00
|
|
|
# For finding project meta-data
|
2014-05-16 12:00:30 -04:00
|
|
|
db.projectHistoryMetaData.ensureIndex { project_id: 1 }, { background: true }
|
2014-05-16 10:59:12 -04:00
|
|
|
# TTL index for auto deleting week old temporary ops
|
2014-05-16 12:00:30 -04:00
|
|
|
db.docHistory.ensureIndex { expiresAt: 1 }, { expireAfterSeconds: 0, background: true }
|
2016-03-01 06:38:23 -05:00
|
|
|
# For finding packs to be checked for archiving
|
|
|
|
db.docHistory.ensureIndex { last_checked: 1 }, { background: true }
|
|
|
|
# For finding archived packs
|
|
|
|
db.docHistoryIndex.ensureIndex { project_id: 1 }, { background: true }
|
2017-03-16 11:17:38 -04:00
|
|
|
|
|
|
|
|
2017-03-17 10:58:35 -04:00
|
|
|
[
|
|
|
|
'getLastCompressedUpdate',
|
|
|
|
'getProjectMetaData',
|
|
|
|
'setProjectMetaData'
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
|