Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid version vector check logic for backward compatibility #1105

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/document/crdt/rga_tree_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -545,17 +547,19 @@ 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
} else {
clientLamportAtChange = 0
}
} else {
// Case 3: from operation without version vector(Before v0.5.6)
createdAt, ok := maxCreatedAtMapByActor[actorIDHex]
if ok {
maxCreatedAt = createdAt
Expand Down
11 changes: 8 additions & 3 deletions pkg/document/crdt/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -320,17 +323,19 @@ 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
} else {
clientLamportAtChange = 0
}
} else {
// Case 3: from operation without version vector(Before v0.5.6)
createdAt, ok := maxCreatedAtMapByActor[actorIDHex]
if ok {
maxCreatedAt = createdAt
Expand Down
34 changes: 25 additions & 9 deletions pkg/document/crdt/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -869,17 +872,20 @@ 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
} else {
clientLamportAtChange = 0
}
} else {
// Case 3: from operation without version vector(Before v0.5.6)
createdAt, ok := maxCreatedAtMapByActor[actorIDHex]
if ok {
maxCreatedAt = createdAt
Expand Down Expand Up @@ -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) {
Expand All @@ -1009,17 +1018,19 @@ 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
} else {
clientLamportAtChange = 0
}
} else {
// Case 3: from operation without version vector(Before v0.5.6)
createdAt, ok := maxCreatedAtMapByActor[actorIDHex]
if ok {
maxCreatedAt = createdAt
Expand Down Expand Up @@ -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) {
Expand All @@ -1078,17 +1092,19 @@ 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
} else {
clientLamportAtChange = 0
}
} else {
// Case 3: from operation without version vector(Before v0.5.6)
createdAt, ok := maxCreatedAtMapByActor[actorIDHex]
if ok {
maxCreatedAt = createdAt
Expand Down
Loading