From 64eb794c02441efd7c601853faba7b46c272d85a Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 9 Aug 2013 11:36:19 +0100 Subject: [PATCH] Compress all docs using compressHistory script --- .../app/coffee/ConversionManager.coffee | 22 ++++++--- services/track-changes/compressHistory.coffee | 46 ++++++++++--------- services/track-changes/package.json | 1 + 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/services/track-changes/app/coffee/ConversionManager.coffee b/services/track-changes/app/coffee/ConversionManager.coffee index 5e450fc01d..d7849818c0 100644 --- a/services/track-changes/app/coffee/ConversionManager.coffee +++ b/services/track-changes/app/coffee/ConversionManager.coffee @@ -30,10 +30,9 @@ module.exports = ConversionManager = ConversionManager.getLastRawUpdateAndVersion doc_id, (error, rawUpdate, currentVersion, tailVersion) -> return callback(error) if error? - rawUpdates = ConcatManager.normalizeUpdate(rawUpdate) - - if currentVersion - tailVersion > ConcatManager.OPS_TO_LEAVE - ConversonManager.getLatestCompressedUpdate doc_id, (error, lastCompressedUpdate) -> + if currentVersion - tailVersion > ConversionManager.OPS_TO_LEAVE + rawUpdates = ConcatManager.normalizeUpdate(rawUpdate) + ConversionManager.getLatestCompressedUpdate doc_id, (error, lastCompressedUpdate) -> return callback(error) if error? removeAndModifyPreviousCompressedUpdate = (callback, compressedUpdates) -> @@ -44,7 +43,7 @@ module.exports = ConversionManager = compressedUpdates = compressedUpdates.concat ConcatManager.concatTwoUpdates lastCompressedUpdate, rawUpdate ConversionManager.removeLatestCompressedUpdate doc_id, (error) -> return callback(error) if error? - callback null, compressUpdates + callback null, compressedUpdates else callback null, rawUpdates @@ -54,9 +53,18 @@ module.exports = ConversionManager = return callback(error) if error? ConversionManager.trimLastRawUpdate doc_id, tailVersion, (error) -> return callback(error) if error? - console.log "Pushed op", tailVersion + console.log doc_id, "Pushed op", tailVersion callback null, true else - console.log "Up to date" + console.log doc_id, "Up to date" callback null, false + + convertAllOldRawUpdates: (doc_id, callback = (error) ->) -> + ConversionManager.convertOldestRawUpdate doc_id, (error, converted) -> + return callback(error) if error? + if converted + # Keep going + ConversionManager.convertAllOldRawUpdates doc_id, callback + else + callback() diff --git a/services/track-changes/compressHistory.coffee b/services/track-changes/compressHistory.coffee index d3ed3eae58..95679a4dcd 100644 --- a/services/track-changes/compressHistory.coffee +++ b/services/track-changes/compressHistory.coffee @@ -1,28 +1,32 @@ {db, ObjectId} = require "./app/coffee/mongojs" ConversionManager = require "./app/coffee/ConversionManager" +async = require "async" -doc_id = process.argv.pop() -console.log "DOC ID", doc_id - -done = () -> - console.log "DONE! Here's the history:" - db.docHistory.find { doc_id: ObjectId(doc_id) }, (error, docs) -> +db.docOps.find { }, { doc_id: true }, (error, docs) -> + throw error if error? + jobs = [] + for doc in docs + do (doc) -> + jobs.push (callback) -> + doc_id = doc.doc_id.toString() + ConversionManager.convertAllOldRawUpdates doc_id, (error) -> + return callback(error) if error? + console.log doc_id, "DONE" + db.docHistory.find { doc_id: ObjectId(doc_id) }, (error, docs) -> + return callback(error) if error? + doc = docs[0] + if doc? + for update in doc.docOps + op = update.op[0] + if op.i? + console.log doc_id, update.meta.start_ts, update.meta.end_ts, update.meta.user_id, "INSERT", op.p, op.i + else if op.d? + console.log doc_id, update.meta.start_ts, update.meta.end_ts, update.meta.user_id, "DELETE", op.p, op.d + else + console.log doc_id, "NO HISTORY" + callback() + async.series jobs, (error) -> throw error if error? - doc = docs[0] - for update in doc.docOps - op = update.op[0] - if op.i? - console.log update.meta.start_ts, update.meta.end_ts, update.meta.user_id, "INSERT", op.p, op.i - else if op.d? - console.log update.meta.start_ts, update.meta.end_ts, update.meta.user_id, "DELETE", op.p, op.d process.exit() - -do next = () -> - ConversionManager.convertOldestRawUpdate doc_id, (error, converted) -> - throw error if error? - if converted - next() - else - done() diff --git a/services/track-changes/package.json b/services/track-changes/package.json index 1a6be78455..5698e2dd3b 100644 --- a/services/track-changes/package.json +++ b/services/track-changes/package.json @@ -2,6 +2,7 @@ "name": "history-sharelatex", "version": "0.0.1", "dependencies": { + "async": "", "chai": "", "sandboxed-module": "", "sinon": "",