Skip to content

Commit

Permalink
Implement attributes in Tree.Edit
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Jun 14, 2023
1 parent 990dff3 commit c32193b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/document/json/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ func (t *Tree) Edit(fromIdx, toIdx int, content *TreeNode) bool {
if fromIdx > toIdx {
panic("from should be less than or equal to to")
}
ticket := t.context.IssueTimeTicket()

var node *crdt.TreeNode
if content != nil {
node = crdt.NewTreeNode(crdt.NewTreePos(t.context.IssueTimeTicket(), 0), content.Type, nil, content.Value)
var attributes *crdt.RHT
if content.Attributes != nil {
attributes = crdt.NewRHT()
for key, val := range content.Attributes {
attributes.Set(key, val, ticket)
}
}
node = crdt.NewTreeNode(crdt.NewTreePos(ticket, 0), content.Type, attributes, content.Value)
for _, child := range content.Children {
buildDescendants(t.context, child, node)
}
Expand All @@ -83,7 +91,7 @@ func (t *Tree) Edit(fromIdx, toIdx int, content *TreeNode) bool {
clone = node.DeepCopy()
}

ticket := t.context.LastTimeTicket()
ticket = t.context.LastTimeTicket()
t.Tree.Edit(fromPos, toPos, clone, ticket)

t.context.Push(operations.NewTreeEdit(
Expand All @@ -104,9 +112,18 @@ func (t *Tree) Len() int {

// EditByPath edits this tree with the given path and node.
func (t *Tree) EditByPath(fromPath []int, toPath []int, content *TreeNode) bool {
ticket := t.context.IssueTimeTicket()

var node *crdt.TreeNode
if content != nil {
node = crdt.NewTreeNode(crdt.NewTreePos(t.context.IssueTimeTicket(), 0), content.Type, nil, content.Value)
var attributes *crdt.RHT
if content.Attributes != nil {
attributes = crdt.NewRHT()
for key, val := range content.Attributes {
attributes.Set(key, val, ticket)
}
}
node = crdt.NewTreeNode(crdt.NewTreePos(ticket, 0), content.Type, attributes, content.Value)
for _, child := range content.Children {
buildDescendants(t.context, child, node)
}
Expand All @@ -119,7 +136,7 @@ func (t *Tree) EditByPath(fromPath []int, toPath []int, content *TreeNode) bool
clone = node.DeepCopy()
}

ticket := t.context.LastTimeTicket()
ticket = t.context.LastTimeTicket()
t.Tree.Edit(fromPos, toPos, clone, ticket)

t.context.Push(operations.NewTreeEdit(
Expand Down
29 changes: 29 additions & 0 deletions test/integration/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,35 @@ func TestTree(t *testing.T) {
assert.NoError(t, err)
})

t.Run("edit its content with attributes test", func(t *testing.T) {
doc := document.New(helper.TestDocKey(t))
err := doc.Update(func(root *json.Object) error {
root.SetNewTree("t", &json.TreeNode{Type: "doc"})
assert.Equal(t, "<doc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").Edit(0, 0, &json.TreeNode{
Type: "p",
Attributes: map[string]string{"bold": "true"},
Children: []json.TreeNode{{Type: "text", Value: "ab"}},
})
assert.Equal(t, `<doc><p bold="true">ab</p></doc>`, root.GetTree("t").ToXML())

root.GetTree("t").Edit(4, 4, &json.TreeNode{
Type: "p",
Attributes: map[string]string{"italic": "true"},
Children: []json.TreeNode{{Type: "text", Value: "cd"}},
})
assert.Equal(t, `<doc><p bold="true">ab</p><p italic="true">cd</p></doc>`, root.GetTree("t").ToXML())

root.GetTree("t").Edit(2, 6, nil)
assert.Equal(t, `<doc><p italic="true">ad</p></doc>`, root.GetTree("t").ToXML())

return nil
})
assert.NoError(t, err)
assert.Equal(t, `<doc><p italic="true">ad</p></doc>`, doc.Root().GetTree("t").ToXML())
})

t.Run("sync with other clients test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
Expand Down

1 comment on commit c32193b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: c32193b Previous: 990dff3 Ratio
BenchmarkDocument/constructor_test 1277 ns/op 752 B/op 12 allocs/op 1238 ns/op 752 B/op 12 allocs/op 1.03
BenchmarkDocument/status_test 669.9 ns/op 720 B/op 10 allocs/op 642.8 ns/op 720 B/op 10 allocs/op 1.04
BenchmarkDocument/equals_test 7318 ns/op 5072 B/op 85 allocs/op 9451 ns/op 5072 B/op 85 allocs/op 0.77
BenchmarkDocument/nested_update_test 20908 ns/op 11033 B/op 235 allocs/op 20269 ns/op 11033 B/op 235 allocs/op 1.03
BenchmarkDocument/delete_test 27981 ns/op 14162 B/op 310 allocs/op 27264 ns/op 14161 B/op 310 allocs/op 1.03
BenchmarkDocument/object_test 9577 ns/op 5793 B/op 97 allocs/op 9176 ns/op 5792 B/op 97 allocs/op 1.04
BenchmarkDocument/array_test 34630 ns/op 10889 B/op 251 allocs/op 33501 ns/op 10889 B/op 251 allocs/op 1.03
BenchmarkDocument/text_test 47089 ns/op 14058 B/op 456 allocs/op 36549 ns/op 14058 B/op 456 allocs/op 1.29
BenchmarkDocument/text_composition_test 38935 ns/op 17538 B/op 461 allocs/op 37583 ns/op 17538 B/op 461 allocs/op 1.04
BenchmarkDocument/rich_text_test 101865 ns/op 36007 B/op 1108 allocs/op 98345 ns/op 36013 B/op 1108 allocs/op 1.04
BenchmarkDocument/counter_test 19862 ns/op 9058 B/op 212 allocs/op 19394 ns/op 9057 B/op 212 allocs/op 1.02
BenchmarkDocument/text_edit_gc_100 4032371 ns/op 1552543 B/op 17147 allocs/op 3946133 ns/op 1552358 B/op 17148 allocs/op 1.02
BenchmarkDocument/text_edit_gc_1000 325639951 ns/op 136641328 B/op 210715 allocs/op 312808698 ns/op 136637108 B/op 210704 allocs/op 1.04
BenchmarkDocument/text_split_gc_100 4581367 ns/op 2217232 B/op 16574 allocs/op 4467701 ns/op 2217380 B/op 16578 allocs/op 1.03
BenchmarkDocument/text_split_gc_1000 365566899 ns/op 214850509 B/op 211403 allocs/op 365335058 ns/op 214848552 B/op 211374 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 20089423 ns/op 5903800 B/op 41121 allocs/op 19998314 ns/op 5904483 B/op 41124 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 250404709 ns/op 53835764 B/op 415969 allocs/op 284308384 ns/op 53841252 B/op 415996 allocs/op 0.88
BenchmarkDocument/text_100 322839 ns/op 117749 B/op 5064 allocs/op 308977 ns/op 117748 B/op 5064 allocs/op 1.04
BenchmarkDocument/text_1000 3535210 ns/op 1152331 B/op 50068 allocs/op 3369452 ns/op 1152304 B/op 50068 allocs/op 1.05
BenchmarkDocument/array_1000 1767971 ns/op 1102048 B/op 11854 allocs/op 1707947 ns/op 1102009 B/op 11854 allocs/op 1.04
BenchmarkDocument/array_10000 20324492 ns/op 9907295 B/op 120710 allocs/op 19911839 ns/op 9906610 B/op 120706 allocs/op 1.02
BenchmarkDocument/array_gc_100 181032 ns/op 97408 B/op 1226 allocs/op 179996 ns/op 97409 B/op 1226 allocs/op 1.01
BenchmarkDocument/array_gc_1000 1996347 ns/op 1169620 B/op 12889 allocs/op 1948565 ns/op 1169683 B/op 12889 allocs/op 1.02
BenchmarkDocument/counter_1000 294942 ns/op 197876 B/op 6490 allocs/op 283736 ns/op 197874 B/op 6490 allocs/op 1.04
BenchmarkDocument/counter_10000 3182673 ns/op 2164802 B/op 69497 allocs/op 3091646 ns/op 2164822 B/op 69497 allocs/op 1.03
BenchmarkDocument/object_1000 1990404 ns/op 1450900 B/op 9903 allocs/op 1861441 ns/op 1450626 B/op 9902 allocs/op 1.07
BenchmarkDocument/object_10000 23647171 ns/op 12365849 B/op 101196 allocs/op 22794332 ns/op 12367990 B/op 101205 allocs/op 1.04
BenchmarkRPC/client_to_server 449863309 ns/op 18003133 B/op 310038 allocs/op 449800486 ns/op 17981306 B/op 309606 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server 760108191 ns/op 32922588 B/op 567207 allocs/op 725955530 ns/op 33252820 B/op 573278 allocs/op 1.05
BenchmarkRPC/attach_large_document 1444005322 ns/op 2124483552 B/op 9911 allocs/op 1522104707 ns/op 2134682048 B/op 9879 allocs/op 0.95
BenchmarkRPC/adminCli_to_server 547017930 ns/op 19700612 B/op 321600 allocs/op 543629324 ns/op 19666968 B/op 321599 allocs/op 1.01
BenchmarkLocker 129.3 ns/op 16 B/op 1 allocs/op 123.3 ns/op 16 B/op 1 allocs/op 1.05
BenchmarkLockerParallel 142.8 ns/op 0 B/op 0 allocs/op 149 ns/op 0 B/op 0 allocs/op 0.96
BenchmarkLockerMoreKeys 359.4 ns/op 14 B/op 0 allocs/op 365 ns/op 14 B/op 0 allocs/op 0.98
BenchmarkSync/memory_sync_10_test 7590 ns/op 1338 B/op 39 allocs/op 7479 ns/op 1338 B/op 39 allocs/op 1.01
BenchmarkSync/memory_sync_100_test 68942 ns/op 8793 B/op 280 allocs/op 68557 ns/op 8764 B/op 278 allocs/op 1.01
BenchmarkSync/memory_sync_1000_test 684290 ns/op 81539 B/op 2566 allocs/op 679318 ns/op 81781 B/op 2584 allocs/op 1.01
BenchmarkSync/memory_sync_10000_test 7617809 ns/op 867891 B/op 26722 allocs/op 7250097 ns/op 845969 B/op 26680 allocs/op 1.05
BenchmarkTextEditing 26579560297 ns/op 8436195504 B/op 19835256 allocs/op 26956311518 ns/op 8435971728 B/op 19834182 allocs/op 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.