fix update-in-place bug for array ops

This commit is contained in:
Brian Gough 2015-12-03 15:47:55 +00:00
parent 11be8c3733
commit be2136de7c
3 changed files with 28 additions and 1 deletions

View file

@ -56,6 +56,10 @@ module.exports = UpdateCompressor =
return concattedUpdates
compressRawUpdates: (lastPreviousUpdate, rawUpdates) ->
if lastPreviousUpdate?.op?.length > 1
# if the last previous update was an array op, don't compress onto it.
# The avoids cases where array length changes but version number doesn't
return [lastPreviousUpdate].concat UpdateCompressor.compressRawUpdates(null,rawUpdates)
if lastPreviousUpdate?
rawUpdates = [lastPreviousUpdate].concat(rawUpdates)
updates = UpdateCompressor.convertToSingleOpUpdates(rawUpdates)

View file

@ -7,6 +7,7 @@ UpdateTrimmer = require "./UpdateTrimmer"
logger = require "logger-sharelatex"
async = require "async"
DocArchiveManager = require "./DocArchiveManager"
_ = require "underscore"
module.exports = UpdatesManager =
compressAndSaveRawUpdates: (project_id, doc_id, rawUpdates, temporary, callback = (error) ->) ->
@ -50,7 +51,7 @@ module.exports = UpdatesManager =
# compress them together with the new ones
[firstUpdate, additionalUpdates...] = compressedUpdates
if firstUpdate.v == lastCompressedUpdate.v
if firstUpdate.v == lastCompressedUpdate.v and _.isEqual(firstUpdate, lastCompressedUpdate)
# first update version hasn't changed, skip it and insert remaining updates
# this is an optimisation, we could update the existing op with itself
updateToModify = null

View file

@ -304,3 +304,25 @@ describe "UpdateCompressor", ->
meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
v: 43
}]
describe "compressRawUpdates", ->
describe "merging in-place with an array op", ->
it "should not change the existing last updates", ->
expect(@UpdateCompressor.compressRawUpdates {
op: [ {"p":1000,"d":"hello"}, {"p":1000,"i":"HELLO()"} ]
meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
v: 42
}, [{
op: [{ p: 1006, i: "WORLD" }]
meta: ts: @ts2, user_id: @user_id
v: 43
}])
.to.deep.equal [{
op: [{"p":1000,"d":"hello"}, {"p":1000,"i":"HELLO()"} ]
meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
v: 42
},{
op: [{"p":1006,"i":"WORLD"}]
meta: start_ts: @ts2, end_ts: @ts2, user_id: @user_id
v: 43
}]