UpdateCompressor.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. v: update.v
  29. return normalizedUpdates
  30. compressRawUpdates: (lastPreviousUpdate, rawUpdates) ->
  31. updates = UpdateCompressor.convertRawUpdatesToCompressedFormat(rawUpdates)
  32. if lastPreviousUpdate?
  33. updates.unshift(lastPreviousUpdate)
  34. return UpdateCompressor.compressUpdates(updates)
  35. compressUpdates: (updates) ->
  36. return [] if updates.length == 0
  37. compressedUpdates = [updates.shift()]
  38. for update in updates
  39. lastCompressedUpdate = compressedUpdates.pop()
  40. if lastCompressedUpdate?
  41. compressedUpdates = compressedUpdates.concat UpdateCompressor._concatTwoUpdates lastCompressedUpdate, update
  42. else
  43. compressedUpdates.push update
  44. return compressedUpdates
  45. MAX_TIME_BETWEEN_UPDATES: oneMinute = 60 * 1000
  46. _concatTwoUpdates: (firstUpdate, secondUpdate) ->
  47. firstUpdate =
  48. op: firstUpdate.op
  49. meta:
  50. user_id: firstUpdate.meta.user_id or null
  51. start_ts: firstUpdate.meta.start_ts or firstUpdate.meta.ts
  52. end_ts: firstUpdate.meta.end_ts or firstUpdate.meta.ts
  53. v: firstUpdate.v
  54. secondUpdate =
  55. op: secondUpdate.op
  56. meta:
  57. user_id: secondUpdate.meta.user_id or null
  58. start_ts: secondUpdate.meta.start_ts or secondUpdate.meta.ts
  59. end_ts: secondUpdate.meta.end_ts or secondUpdate.meta.ts
  60. v: secondUpdate.v
  61. if firstUpdate.meta.user_id != secondUpdate.meta.user_id
  62. return [firstUpdate, secondUpdate]
  63. if secondUpdate.meta.start_ts - firstUpdate.meta.end_ts > UpdateCompressor.MAX_TIME_BETWEEN_UPDATES
  64. return [firstUpdate, secondUpdate]
  65. firstOp = firstUpdate.op
  66. secondOp = secondUpdate.op
  67. # Two inserts
  68. if firstOp.i? and secondOp.i? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  69. return [
  70. meta:
  71. start_ts: firstUpdate.meta.start_ts
  72. end_ts: secondUpdate.meta.end_ts
  73. user_id: firstUpdate.meta.user_id
  74. op:
  75. p: firstOp.p
  76. i: strInject(firstOp.i, secondOp.p - firstOp.p, secondOp.i)
  77. v: secondUpdate.v
  78. ]
  79. # Two deletes
  80. else if firstOp.d? and secondOp.d? and secondOp.p <= firstOp.p <= (secondOp.p + secondOp.d.length)
  81. return [
  82. meta:
  83. start_ts: firstUpdate.meta.start_ts
  84. end_ts: secondUpdate.meta.end_ts
  85. user_id: firstUpdate.meta.user_id
  86. op:
  87. p: secondOp.p
  88. d: strInject(secondOp.d, firstOp.p - secondOp.p, firstOp.d)
  89. v: secondUpdate.v
  90. ]
  91. # An insert and then a delete
  92. else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
  93. offset = secondOp.p - firstOp.p
  94. insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
  95. if insertedText == secondOp.d
  96. insert = strRemove(firstOp.i, offset, secondOp.d.length)
  97. return [
  98. meta:
  99. start_ts: firstUpdate.meta.start_ts
  100. end_ts: secondUpdate.meta.end_ts
  101. user_id: firstUpdate.meta.user_id
  102. op:
  103. p: firstOp.p
  104. i: insert
  105. v: secondUpdate.v
  106. ]
  107. else
  108. # This shouldn't be possible!
  109. return [firstUpdate, secondUpdate]
  110. else
  111. return [firstUpdate, secondUpdate]