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 getGarbageLen to retrun correct size #743

Merged
merged 7 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 5 additions & 2 deletions pkg/document/crdt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,22 @@ func (r *Root) RemovedElementLen() int {
// GarbageLen returns the count of removed elements.
func (r *Root) GarbageLen() int {
count := 0
seen := make(map[string]bool)

for _, pair := range r.removedElementPairMapByCreatedAt {
count++
seen[pair.elem.CreatedAt().ToTestString()] = true
hackerwins marked this conversation as resolved.
Show resolved Hide resolved

switch elem := pair.elem.(type) {
case Container:
elem.Descendants(func(elem Element, parent Container) bool {
count++
seen[elem.CreatedAt().ToTestString()] = true
return false
})
}
}

count += len(seen)

for _, element := range r.elementHasRemovedNodesSetByCreatedAt {
count += element.removedNodesLen()
}
Expand Down
48 changes: 48 additions & 0 deletions test/integration/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,52 @@ func TestGarbageCollection(t *testing.T) {
assert.Equal(t, 0, d1.GarbageLen())
assert.Equal(t, 6, d2.GarbageLen())
})

t.Run("GarbageLen should return the actual number of elements garbage-collected", func(t *testing.T) {
ctx := context.Background()
docKey := helper.TestDocKey(t)
d1 := document.New(docKey)
d2 := document.New(docKey)

err := c1.Attach(ctx, d1)
assert.NoError(t, err)

err = d1.Update(func(root *json.Object, p *presence.Presence) error {
root.SetNewObject("point").
SetInteger("point.x", 0).
SetInteger("point.y", 0)
return nil
})
err = c1.Sync(ctx)
assert.NoError(t, err)
err = c2.Attach(ctx, d2)
assert.NoError(t, err)

err = d1.Update(func(root *json.Object, p *presence.Presence) error {
root.Delete("point")
return nil
})
assert.NoError(t, err)
assert.Equal(t, 3, d1.GarbageLen())

err = d2.Update(func(root *json.Object, p *presence.Presence) error {
root.GetObject("point").Delete("point.x")
return nil
})
assert.NoError(t, err)
assert.Equal(t, 1, d2.GarbageLen())

err = c1.Sync(ctx)
assert.NoError(t, err)
err = c2.Sync(ctx)
assert.NoError(t, err)
err = c1.Sync(ctx)
assert.NoError(t, err)

assert.Equal(t, 3, d1.GarbageLen())
assert.Equal(t, 3, d2.GarbageLen())

assert.Equal(t, d1.GarbageCollect(time.MaxTicket), 3)
assert.Equal(t, d2.GarbageCollect(time.MaxTicket), 3)
})
}