UpdateCompressor.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. NOOP: "noop"
  5. # Updates come from the doc updater in format
  6. # {
  7. # op: [ { ... op1 ... }, { ... op2 ... } ]
  8. # meta: { ts: ..., user_id: ... }
  9. # }
  10. # but it's easier to work with on op per update, so convert these updates to
  11. # our compressed format
  12. # [{
  13. # op: op1
  14. # meta: { start_ts: ... , end_ts: ..., user_id: ... }
  15. # }, {
  16. # op: op2
  17. # meta: { start_ts: ... , end_ts: ..., user_id: ... }
  18. # }]
  19. convertToSingleOpUpdates: (updates) ->
  20. splitUpdates = []
  21. for update in updates
  22. if update.op.length == 0
  23. splitUpdates.push
  24. op: UpdateCompressor.NOOP
  25. meta:
  26. start_ts: update.meta.start_ts or update.meta.ts
  27. end_ts: update.meta.end_ts or update.meta.ts
  28. user_id: update.meta.user_id
  29. v: update.v
  30. else
  31. for op in update.op
  32. splitUpdates.push
  33. op: op
  34. meta:
  35. start_ts: update.meta.start_ts or update.meta.ts
  36. end_ts: update.meta.end_ts or update.meta.ts
  37. user_id: update.meta.user_id
  38. v: update.v
  39. return splitUpdates
  40. concatUpdatesWithSameVersion: (updates) ->
  41. concattedUpdates = []
  42. for update in updates
  43. lastUpdate = concattedUpdates[concattedUpdates.length - 1]
  44. if lastUpdate? and lastUpdate.v == update.v
  45. lastUpdate.op.push update.op unless update.op == UpdateCompressor.NOOP
  46. else
  47. nextUpdate =
  48. op: []
  49. meta: update.meta
  50. v: update.v
  51. nextUpdate.op.push update.op unless update.op == UpdateCompressor.NOOP
  52. concattedUpdates.push nextUpdate
  53. return concattedUpdates
  54. compressRawUpdates: (lastPreviousUpdate, rawUpdates) ->
  55. if lastPreviousUpdate?
  56. rawUpdates = [lastPreviousUpdate].concat(rawUpdates)
  57. updates = UpdateCompressor.convertToSingleOpUpdates(rawUpdates)
  58. updates = UpdateCompressor.compressUpdates(updates)
  59. return UpdateCompressor.concatUpdatesWithSameVersion(updates)
  60. compressUpdates: (updates) ->
  61. return [] if updates.length == 0
  62. compressedUpdates = [updates.shift()]
  63. for update in updates
  64. lastCompressedUpdate = compressedUpdates.pop()
  65. if lastCompressedUpdate?
  66. compressedUpdates = compressedUpdates.concat UpdateCompressor._concatTwoUpdates lastCompressedUpdate, update
  67. else
  68. compressedUpdates.push update
  69. return compressedUpdates
  70. MAX_TIME_BETWEEN_UPDATES: oneMinute = 60 * 1000
  71. _concatTwoUpdates: (firstUpdate, secondUpdate) ->
  72. firstUpdate =
  73. op: firstUpdate.op
  74. meta:
  75. user_id: firstUpdate.meta.user_id or null
  76. start_ts: firstUpdate.meta.start_ts or firstUpdate.meta.ts
  77. end_ts: firstUpdate.meta.end_ts or firstUpdate.meta.ts
  78. v: firstUpdate.v
  79. secondUpdate =
  80. op: secondUpdate.op
  81. meta:
  82. user_id: secondUpdate.meta.user_id or null
  83. start_ts: secondUpdate.meta.start_ts or secondUpdate.meta.ts
  84. end_ts: secondUpdate.meta.end_ts or secondUpdate.meta.ts
  85. v: secondUpdate.v
  86. if firstUpdate.meta.user_id != secondUpdate.meta.user_id
  87. return [firstUpdate, secondUpdate]
  88. if secondUpdate.meta.start_ts - firstUpdate.meta.end_ts > UpdateCompressor.MAX_TIME_BETWEEN_UPDATES
  89. return [firstUpdate, secondUpdate]
  90. firstOp = firstUpdate.op
  91. secondOp = secondUpdate.op
  92. # Two inserts
  93. if firstOp.i? and secondOp.i? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  94. return [
  95. meta:
  96. start_ts: firstUpdate.meta.start_ts
  97. end_ts: secondUpdate.meta.end_ts
  98. user_id: firstUpdate.meta.user_id
  99. op:
  100. p: firstOp.p
  101. i: strInject(firstOp.i, secondOp.p - firstOp.p, secondOp.i)
  102. v: secondUpdate.v
  103. ]
  104. # Two deletes
  105. else if firstOp.d? and secondOp.d? and secondOp.p <= firstOp.p <= (secondOp.p + secondOp.d.length)
  106. return [
  107. meta:
  108. start_ts: firstUpdate.meta.start_ts
  109. end_ts: secondUpdate.meta.end_ts
  110. user_id: firstUpdate.meta.user_id
  111. op:
  112. p: secondOp.p
  113. d: strInject(secondOp.d, firstOp.p - secondOp.p, firstOp.d)
  114. v: secondUpdate.v
  115. ]
  116. # An insert and then a delete
  117. else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  118. offset = secondOp.p - firstOp.p
  119. insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
  120. if insertedText == secondOp.d
  121. insert = strRemove(firstOp.i, offset, secondOp.d.length)
  122. return [
  123. meta:
  124. start_ts: firstUpdate.meta.start_ts
  125. end_ts: secondUpdate.meta.end_ts
  126. user_id: firstUpdate.meta.user_id
  127. op:
  128. p: firstOp.p
  129. i: insert
  130. v: secondUpdate.v
  131. ]
  132. else
  133. # This shouldn't be possible!
  134. return [firstUpdate, secondUpdate]
  135. else
  136. return [firstUpdate, secondUpdate]