UpdateCompressor.coffee 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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?.op?.length > 1
  56. # if the last previous update was an array op, don't compress onto it.
  57. # The avoids cases where array length changes but version number doesn't
  58. return [lastPreviousUpdate].concat UpdateCompressor.compressRawUpdates(null,rawUpdates)
  59. if lastPreviousUpdate?
  60. rawUpdates = [lastPreviousUpdate].concat(rawUpdates)
  61. updates = UpdateCompressor.convertToSingleOpUpdates(rawUpdates)
  62. updates = UpdateCompressor.compressUpdates(updates)
  63. return UpdateCompressor.concatUpdatesWithSameVersion(updates)
  64. compressUpdates: (updates) ->
  65. return [] if updates.length == 0
  66. compressedUpdates = [updates.shift()]
  67. for update in updates
  68. lastCompressedUpdate = compressedUpdates.pop()
  69. if lastCompressedUpdate?
  70. compressedUpdates = compressedUpdates.concat UpdateCompressor._concatTwoUpdates lastCompressedUpdate, update
  71. else
  72. compressedUpdates.push update
  73. return compressedUpdates
  74. MAX_TIME_BETWEEN_UPDATES: oneMinute = 60 * 1000
  75. _concatTwoUpdates: (firstUpdate, secondUpdate) ->
  76. firstUpdate =
  77. op: firstUpdate.op
  78. meta:
  79. user_id: firstUpdate.meta.user_id or null
  80. start_ts: firstUpdate.meta.start_ts or firstUpdate.meta.ts
  81. end_ts: firstUpdate.meta.end_ts or firstUpdate.meta.ts
  82. v: firstUpdate.v
  83. secondUpdate =
  84. op: secondUpdate.op
  85. meta:
  86. user_id: secondUpdate.meta.user_id or null
  87. start_ts: secondUpdate.meta.start_ts or secondUpdate.meta.ts
  88. end_ts: secondUpdate.meta.end_ts or secondUpdate.meta.ts
  89. v: secondUpdate.v
  90. if firstUpdate.meta.user_id != secondUpdate.meta.user_id
  91. return [firstUpdate, secondUpdate]
  92. if secondUpdate.meta.start_ts - firstUpdate.meta.end_ts > UpdateCompressor.MAX_TIME_BETWEEN_UPDATES
  93. return [firstUpdate, secondUpdate]
  94. firstOp = firstUpdate.op
  95. secondOp = secondUpdate.op
  96. # Two inserts
  97. if firstOp.i? and secondOp.i? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  98. return [
  99. meta:
  100. start_ts: firstUpdate.meta.start_ts
  101. end_ts: secondUpdate.meta.end_ts
  102. user_id: firstUpdate.meta.user_id
  103. op:
  104. p: firstOp.p
  105. i: strInject(firstOp.i, secondOp.p - firstOp.p, secondOp.i)
  106. v: secondUpdate.v
  107. ]
  108. # Two deletes
  109. else if firstOp.d? and secondOp.d? and secondOp.p <= firstOp.p <= (secondOp.p + secondOp.d.length)
  110. return [
  111. meta:
  112. start_ts: firstUpdate.meta.start_ts
  113. end_ts: secondUpdate.meta.end_ts
  114. user_id: firstUpdate.meta.user_id
  115. op:
  116. p: secondOp.p
  117. d: strInject(secondOp.d, firstOp.p - secondOp.p, firstOp.d)
  118. v: secondUpdate.v
  119. ]
  120. # An insert and then a delete
  121. else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  122. offset = secondOp.p - firstOp.p
  123. insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
  124. if insertedText == secondOp.d
  125. insert = strRemove(firstOp.i, offset, secondOp.d.length)
  126. return [
  127. meta:
  128. start_ts: firstUpdate.meta.start_ts
  129. end_ts: secondUpdate.meta.end_ts
  130. user_id: firstUpdate.meta.user_id
  131. op:
  132. p: firstOp.p
  133. i: insert
  134. v: secondUpdate.v
  135. ]
  136. else
  137. # This shouldn't be possible!
  138. return [firstUpdate, secondUpdate]
  139. else
  140. return [firstUpdate, secondUpdate]