diff --git a/pkg/document/crdt/rga_tree_split.go b/pkg/document/crdt/rga_tree_split.go index fa704d5a1..fc876f1fd 100644 --- a/pkg/document/crdt/rga_tree_split.go +++ b/pkg/document/crdt/rga_tree_split.go @@ -527,6 +527,8 @@ func (s *RGATreeSplit[V]) deleteNodes( ) (map[string]*time.Ticket, map[string]*RGATreeSplitNode[V]) { createdAtMapByActor := make(map[string]*time.Ticket) removedNodeMap := make(map[string]*RGATreeSplitNode[V]) + isVersionVectorEmpty := len(versionVector) == 0 + isMaxCreatedAtMapByActorEmpty := len(maxCreatedAtMapByActor) == 0 if len(candidates) == 0 { return createdAtMapByActor, removedNodeMap @@ -545,10 +547,11 @@ func (s *RGATreeSplit[V]) deleteNodes( var maxCreatedAt *time.Ticket var clientLamportAtChange int64 - if versionVector == nil && maxCreatedAtMapByActor == nil { - // Local edit - use version vector comparison + if isVersionVectorEmpty && isMaxCreatedAtMapByActorEmpty { + // Case 1: local editing from json package clientLamportAtChange = time.MaxLamport - } else if len(versionVector) > 0 { + } else if !isVersionVectorEmpty { + // Case 2: from operation with version vector(After v0.5.7) lamport, ok := versionVector.Get(actorID) if ok { clientLamportAtChange = lamport @@ -556,6 +559,7 @@ func (s *RGATreeSplit[V]) deleteNodes( clientLamportAtChange = 0 } } else { + // Case 3: from operation without version vector(Before v0.5.6) createdAt, ok := maxCreatedAtMapByActor[actorIDHex] if ok { maxCreatedAt = createdAt diff --git a/pkg/document/crdt/text.go b/pkg/document/crdt/text.go index 18653e80e..deaf90eac 100644 --- a/pkg/document/crdt/text.go +++ b/pkg/document/crdt/text.go @@ -312,6 +312,9 @@ func (t *Text) Style( // 02. style nodes between from and to nodes := t.rgaTreeSplit.findBetween(fromRight, toRight) createdAtMapByActor := make(map[string]*time.Ticket) + isVersionVectorEmpty := len(versionVector) == 0 + isMaxCreatedAtMapByActorEmpty := len(maxCreatedAtMapByActor) == 0 + var toBeStyled []*RGATreeSplitNode[*TextValue] for _, node := range nodes { @@ -320,10 +323,11 @@ func (t *Text) Style( var maxCreatedAt *time.Ticket var clientLamportAtChange int64 - if versionVector == nil && maxCreatedAtMapByActor == nil { - // Local edit - use version vector comparison + if isVersionVectorEmpty && isMaxCreatedAtMapByActorEmpty { + // Case 1: local editing from json package clientLamportAtChange = time.MaxLamport - } else if versionVector != nil { + } else if !isVersionVectorEmpty { + // Case 2: from operation with version vector(After v0.5.7) lamport, ok := versionVector.Get(actorID) if ok { clientLamportAtChange = lamport @@ -331,6 +335,7 @@ func (t *Text) Style( clientLamportAtChange = 0 } } else { + // Case 3: from operation without version vector(Before v0.5.6) createdAt, ok := maxCreatedAtMapByActor[actorIDHex] if ok { maxCreatedAt = createdAt diff --git a/pkg/document/crdt/tree.go b/pkg/document/crdt/tree.go index 3b84d82fa..a6f19d2dc 100644 --- a/pkg/document/crdt/tree.go +++ b/pkg/document/crdt/tree.go @@ -842,6 +842,9 @@ func (t *Tree) collectBetween( var toBeRemoveds []*TreeNode var toBeMovedToFromParents []*TreeNode createdAtMapByActor := make(map[string]*time.Ticket) + isVersionVectorEmpty := len(versionVector) == 0 + isMaxCreatedAtMapByActorEmpty := len(maxCreatedAtMapByActor) == 0 + if err := t.traverseInPosRange( fromParent, fromLeft, toParent, toLeft, @@ -869,10 +872,12 @@ func (t *Tree) collectBetween( var maxCreatedAt *time.Ticket var clientLamportAtChange int64 - if versionVector == nil && maxCreatedAtMapByActor == nil { - // Local edit - use version vector comparison + + if isVersionVectorEmpty && isMaxCreatedAtMapByActorEmpty { + // Case 1: local editing from json package clientLamportAtChange = time.MaxLamport - } else if versionVector != nil { + } else if !isVersionVectorEmpty { + // Case 2: from operation with version vector(After v0.5.7) lamport, ok := versionVector.Get(actorID) if ok { clientLamportAtChange = lamport @@ -880,6 +885,7 @@ func (t *Tree) collectBetween( clientLamportAtChange = 0 } } else { + // Case 3: from operation without version vector(Before v0.5.6) createdAt, ok := maxCreatedAtMapByActor[actorIDHex] if ok { maxCreatedAt = createdAt @@ -1000,6 +1006,9 @@ func (t *Tree) Style( return nil, nil, err } + isVersionVectorEmpty := len(versionVector) == 0 + isMaxCreatedAtMapByActorEmpty := len(maxCreatedAtMapByActor) == 0 + var pairs []GCPair createdAtMapByActor := make(map[string]*time.Ticket) if err = t.traverseInPosRange(fromParent, fromLeft, toParent, toLeft, func(token index.TreeToken[*TreeNode], _ bool) { @@ -1009,10 +1018,11 @@ func (t *Tree) Style( var maxCreatedAt *time.Ticket var clientLamportAtChange int64 - if versionVector == nil && maxCreatedAtMapByActor == nil { - // Local edit - use version vector comparison + if isVersionVectorEmpty && isMaxCreatedAtMapByActorEmpty { + // Case 1: local editing from json package clientLamportAtChange = time.MaxLamport - } else if versionVector != nil { + } else if !isVersionVectorEmpty { + // Case 2: from operation with version vector(After v0.5.7) lamport, ok := versionVector.Get(actorID) if ok { clientLamportAtChange = lamport @@ -1020,6 +1030,7 @@ func (t *Tree) Style( clientLamportAtChange = 0 } } else { + // Case 3: from operation without version vector(Before v0.5.6) createdAt, ok := maxCreatedAtMapByActor[actorIDHex] if ok { maxCreatedAt = createdAt @@ -1069,6 +1080,9 @@ func (t *Tree) RemoveStyle( return nil, nil, err } + isVersionVectorEmpty := len(versionVector) == 0 + isMaxCreatedAtMapByActorEmpty := len(maxCreatedAtMapByActor) == 0 + var pairs []GCPair createdAtMapByActor := make(map[string]*time.Ticket) if err = t.traverseInPosRange(fromParent, fromLeft, toParent, toLeft, func(token index.TreeToken[*TreeNode], _ bool) { @@ -1078,10 +1092,11 @@ func (t *Tree) RemoveStyle( var maxCreatedAt *time.Ticket var clientLamportAtChange int64 - if versionVector == nil && maxCreatedAtMapByActor == nil { - // Local edit - use version vector comparison + if isVersionVectorEmpty && isMaxCreatedAtMapByActorEmpty { + // Case 1: local editing from json package clientLamportAtChange = time.MaxLamport - } else if versionVector != nil { + } else if !isVersionVectorEmpty { + // Case 2: from operation with version vector(After v0.5.7) lamport, ok := versionVector.Get(actorID) if ok { clientLamportAtChange = lamport @@ -1089,6 +1104,7 @@ func (t *Tree) RemoveStyle( clientLamportAtChange = 0 } } else { + // Case 3: from operation without version vector(Before v0.5.6) createdAt, ok := maxCreatedAtMapByActor[actorIDHex] if ok { maxCreatedAt = createdAt