compress_changes.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Compress changes for projects that have too many text operations.
  3. *
  4. * Usage:
  5. *
  6. * node tasks/compress_changes.js CSV_FILE
  7. *
  8. * where CSV_FILE contains a list of project ids in the first column
  9. */
  10. const fs = require('node:fs')
  11. const BPromise = require('bluebird')
  12. const { History } = require('overleaf-editor-core')
  13. const { historyStore, chunkStore } = require('..')
  14. const CONCURRENCY = 10
  15. async function main() {
  16. const filename = process.argv[2]
  17. const projectIds = await readCsv(filename)
  18. const chunks = []
  19. for (const projectId of projectIds) {
  20. const chunkIds = await chunkStore.getProjectChunkIds(projectId)
  21. chunks.push(...chunkIds.map(id => ({ id, projectId })))
  22. }
  23. let totalCompressed = 0
  24. await BPromise.map(
  25. chunks,
  26. async chunk => {
  27. try {
  28. const history = await getHistory(chunk)
  29. const numCompressed = compressChanges(history)
  30. if (numCompressed > 0) {
  31. await storeHistory(chunk, history)
  32. console.log(
  33. `Compressed project ${chunk.projectId}, chunk ${chunk.id}`
  34. )
  35. }
  36. totalCompressed += numCompressed
  37. } catch (err) {
  38. console.log(err)
  39. }
  40. },
  41. { concurrency: CONCURRENCY }
  42. )
  43. console.log('CHANGES:', totalCompressed)
  44. }
  45. async function readCsv(filename) {
  46. const csv = await fs.promises.readFile(filename, 'utf-8')
  47. const lines = csv.trim().split('\n')
  48. const projectIds = lines.map(line => line.split(',')[0])
  49. return projectIds
  50. }
  51. async function getHistory(chunk) {
  52. const rawHistory = await historyStore.loadRaw(chunk.projectId, chunk.id)
  53. const history = History.fromRaw(rawHistory)
  54. return history
  55. }
  56. async function storeHistory(chunk, history) {
  57. const rawHistory = history.toRaw()
  58. await historyStore.storeRaw(chunk.projectId, chunk.id, rawHistory)
  59. }
  60. function compressChanges(history) {
  61. let numCompressed = 0
  62. for (const change of history.getChanges()) {
  63. const newOperations = compressOperations(change.operations)
  64. if (newOperations.length !== change.operations.length) {
  65. numCompressed++
  66. }
  67. change.setOperations(newOperations)
  68. }
  69. return numCompressed
  70. }
  71. function compressOperations(operations) {
  72. if (!operations.length) return []
  73. const newOperations = []
  74. let currentOperation = operations[0]
  75. for (let operationId = 1; operationId < operations.length; operationId++) {
  76. const nextOperation = operations[operationId]
  77. if (currentOperation.canBeComposedWith(nextOperation)) {
  78. currentOperation = currentOperation.compose(nextOperation)
  79. } else {
  80. // currentOperation and nextOperation cannot be composed. Push the
  81. // currentOperation and start over with nextOperation.
  82. newOperations.push(currentOperation)
  83. currentOperation = nextOperation
  84. }
  85. }
  86. newOperations.push(currentOperation)
  87. return newOperations
  88. }
  89. main()
  90. .then(() => {
  91. process.exit()
  92. })
  93. .catch(err => {
  94. console.error(err)
  95. process.exit(1)
  96. })