UpdateCompressor.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. strInject = (s1, pos, s2) -> s1[...pos] + s2 + s1[pos..]
  2. strRemove = (s1, pos, length) -> s1[...pos] + s1[(pos + length)..]
  3. module.exports = UpdateCompressor =
  4. # Updates come from the doc updater in format
  5. # {
  6. # op: [ { ... op1 ... }, { ... op2 ... } ]
  7. # meta: { ts: ..., user_id: ... }
  8. # }
  9. # but it's easier to work with on op per update, so convert these updates to
  10. # our compressed format
  11. # [{
  12. # op: op1
  13. # meta: { start_ts: ... , end_ts: ..., user_id: ... }
  14. # }, {
  15. # op: op2
  16. # meta: { start_ts: ... , end_ts: ..., user_id: ... }
  17. # }]
  18. convertRawUpdatesToCompressedFormat: (updates) ->
  19. normalizedUpdates = []
  20. for update in updates
  21. for op in update.op
  22. normalizedUpdates.push
  23. op: op
  24. meta:
  25. start_ts: update.meta.start_ts or update.meta.ts
  26. end_ts: update.meta.end_ts or update.meta.ts
  27. user_id: update.meta.user_id
  28. return normalizedUpdates
  29. compressRawUpdates: (lastPreviousUpdate, rawUpdates) ->
  30. updates = UpdateCompressor.convertRawUpdatesToCompressedFormat(rawUpdates)
  31. if lastPreviousUpdate?
  32. updates.unshift(lastPreviousUpdate)
  33. return UpdateCompressor.compressUpdates(updates)
  34. compressUpdates: (updates) ->
  35. return [] if updates.length == 0
  36. compressedUpdates = [updates.shift()]
  37. for update in updates
  38. lastCompressedUpdate = compressedUpdates.pop()
  39. if lastCompressedUpdate?
  40. compressedUpdates = compressedUpdates.concat UpdateCompressor._concatTwoUpdates lastCompressedUpdate, update
  41. else
  42. compressedUpdates.push update
  43. return compressedUpdates
  44. MAX_TIME_BETWEEN_UPDATES: oneMinute = 60 * 1000
  45. _concatTwoUpdates: (firstUpdate, secondUpdate) ->
  46. firstUpdate =
  47. op: firstUpdate.op
  48. meta:
  49. user_id: firstUpdate.meta.user_id or null
  50. start_ts: firstUpdate.meta.start_ts or firstUpdate.meta.ts
  51. end_ts: firstUpdate.meta.end_ts or firstUpdate.meta.ts
  52. secondUpdate =
  53. op: secondUpdate.op
  54. meta:
  55. user_id: secondUpdate.meta.user_id or null
  56. start_ts: secondUpdate.meta.start_ts or secondUpdate.meta.ts
  57. end_ts: secondUpdate.meta.end_ts or secondUpdate.meta.ts
  58. if firstUpdate.meta.user_id != secondUpdate.meta.user_id
  59. return [firstUpdate, secondUpdate]
  60. if secondUpdate.meta.start_ts - firstUpdate.meta.end_ts > UpdateCompressor.MAX_TIME_BETWEEN_UPDATES
  61. return [firstUpdate, secondUpdate]
  62. firstOp = firstUpdate.op
  63. secondOp = secondUpdate.op
  64. # Two inserts
  65. if firstOp.i? and secondOp.i? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  66. return [
  67. meta:
  68. start_ts: firstUpdate.meta.start_ts
  69. end_ts: secondUpdate.meta.end_ts
  70. user_id: firstUpdate.meta.user_id
  71. op:
  72. p: firstOp.p
  73. i: strInject(firstOp.i, secondOp.p - firstOp.p, secondOp.i)
  74. ]
  75. # Two deletes
  76. else if firstOp.d? and secondOp.d? and secondOp.p <= firstOp.p <= (secondOp.p + secondOp.d.length)
  77. return [
  78. meta:
  79. start_ts: firstUpdate.meta.start_ts
  80. end_ts: secondUpdate.meta.end_ts
  81. user_id: firstUpdate.meta.user_id
  82. op:
  83. p: secondOp.p
  84. d: strInject(secondOp.d, firstOp.p - secondOp.p, firstOp.d)
  85. ]
  86. # An insert and then a delete
  87. else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  88. offset = secondOp.p - firstOp.p
  89. insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
  90. if insertedText == secondOp.d
  91. insert = strRemove(firstOp.i, offset, secondOp.d.length)
  92. return [] if insert == ""
  93. return [
  94. meta:
  95. start_ts: firstUpdate.meta.start_ts
  96. end_ts: secondUpdate.meta.end_ts
  97. user_id: firstUpdate.meta.user_id
  98. op:
  99. p: firstOp.p
  100. i: insert
  101. ]
  102. else
  103. # This shouldn't be possible!
  104. return [firstUpdate, secondUpdate]
  105. else
  106. return [firstUpdate, secondUpdate]