Skip to content

Commit

Permalink
Extract errors from index.Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jul 3, 2023
1 parent f256b78 commit 1fda3a6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 87 deletions.
4 changes: 2 additions & 2 deletions pkg/document/crdt/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package crdt

import (
"fmt"
"errors"

"github.com/yorkie-team/yorkie/pkg/document/time"
)

// ErrChildNotFound is returned when the child is not found in the container.
var ErrChildNotFound = fmt.Errorf("child not found")
var ErrChildNotFound = errors.New("child not found")

// Container represents Array or Object.
type Container interface {
Expand Down
63 changes: 23 additions & 40 deletions pkg/document/crdt/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"github.com/yorkie-team/yorkie/pkg/llrb"
)

var (
// ErrNodeNotFound is returned when the node is not found.
ErrNodeNotFound = errors.New("node not found")
)

var (
// DummyTreePos is a dummy position of Tree. It is used to represent the head node of RGASplit.
DummyTreePos = &TreePos{
Expand Down Expand Up @@ -326,13 +331,10 @@ func (t *Tree) purgeRemovedNodesBefore(ticket *time.Ticket) (int, error) {
if parent == nil {
count--
delete(nodesToBeRemoved, node.Value)

return nil
}

err := parent.RemoveChild(node)

if err != nil {
if err := parent.RemoveChild(node); err != nil {
return err
}
}
Expand Down Expand Up @@ -472,28 +474,21 @@ func (t *Tree) ToXML() string {
// EditByIndex edits the given range with the given value.
// This method uses indexes instead of a pair of TreePos for testing.
func (t *Tree) EditByIndex(start, end int, content *TreeNode, editedAt *time.Ticket) error {
fromPos, fromErr := t.FindPos(start)
toPos, toErr := t.FindPos(end)

if fromErr != nil {
return fromErr
} else if toErr != nil {
return toErr
fromPos, err := t.FindPos(start)
if err != nil {
return err
}

err := t.Edit(fromPos, toPos, content, editedAt)

toPos, err := t.FindPos(end)
if err != nil {
return err
}

return nil
return t.Edit(fromPos, toPos, content, editedAt)
}

// FindPos finds the position of the given index in the tree.
func (t *Tree) FindPos(offset int) (*TreePos, error) {
treePos, err := t.IndexTree.FindTreePos(offset)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -551,7 +546,6 @@ func (t *Tree) Edit(from, to *TreePos, content *TreeNode, editedAt *time.Ticket)
if removedBlockNode != nil {
blockNode := toPos.Node
offset, err := blockNode.FindBranchOffset(removedBlockNode.IndexTreeNode)

if err != nil {
return err
}
Expand Down Expand Up @@ -620,17 +614,17 @@ func (t *Tree) StyleByIndex(start, end int, attributes map[string]string, edited

// Style applies the given attributes of the given range.
func (t *Tree) Style(from, to *TreePos, attributes map[string]string, editedAt *time.Ticket) error {
_, toRight, toErr := t.findTreePos(to, editedAt)
_, fromRight, fromErr := t.findTreePos(from, editedAt)

if toErr != nil {
return toErr
} else if fromErr != nil {
return fromErr
_, toRight, err := t.findTreePos(to, editedAt)
if err != nil {
return err
}
_, fromRight, err := t.findTreePos(from, editedAt)
if err != nil {
return err
}

// 02. style the nodes.
err := t.nodesBetween(fromRight, toRight, func(node *TreeNode) {
return t.nodesBetween(fromRight, toRight, func(node *TreeNode) {
if node.IsText() {
return
}
Expand All @@ -642,19 +636,13 @@ func (t *Tree) Style(from, to *TreePos, attributes map[string]string, editedAt *
node.Attrs.Set(key, value, editedAt)
}
})

if err != nil {
return err
}

return nil
}

// findTreePos returns TreePos and the right node of the given index in postorder.
func (t *Tree) findTreePos(pos *TreePos, editedAt *time.Ticket) (*index.TreePos[*TreeNode], *TreeNode, error) {
treePos := t.toTreePos(pos)
if treePos == nil {
return nil, nil, fmt.Errorf("cannot find node at %p", pos)
return nil, nil, fmt.Errorf("%p: %w", pos, ErrNodeNotFound)
}

// Find the appropriate position. This logic is similar to the logical to
Expand All @@ -671,7 +659,6 @@ func (t *Tree) findTreePos(pos *TreePos, editedAt *time.Ticket) (*index.TreePos[

// TODO(hackerwins): Consider to use current instead of treePos.
right, err := t.IndexTree.FindPostorderRight(treePos)

if err != nil {
return nil, nil, err
}
Expand All @@ -685,7 +672,7 @@ func (t *Tree) findTreePosWithSplitText(pos *TreePos, editedAt *time.Ticket) (
) {
treePos := t.toTreePos(pos)
if treePos == nil {
return nil, nil, fmt.Errorf("cannot find node at %p", pos)
return nil, nil, fmt.Errorf("%p: %w", pos, ErrNodeNotFound)
}

// Find the appropriate position. This logic is similar to the logical to
Expand All @@ -702,7 +689,6 @@ func (t *Tree) findTreePosWithSplitText(pos *TreePos, editedAt *time.Ticket) (

if current.Node.IsText() {
split, err := current.Node.Value.Split(current.Offset)

if err != nil {
return nil, nil, err
}
Expand All @@ -714,7 +700,6 @@ func (t *Tree) findTreePosWithSplitText(pos *TreePos, editedAt *time.Ticket) (
}

right, err := t.IndexTree.FindPostorderRight(treePos)

if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -747,13 +732,12 @@ func (t *Tree) toIndex(pos *TreePos) (int, error) {
return -1, nil
}

index, err := t.IndexTree.IndexOf(treePos.Node)

idx, err := t.IndexTree.IndexOf(treePos.Node)
if err != nil {
return 0, err
}

return index + treePos.Offset, nil
return idx + treePos.Offset, nil
}

// nodesBetween returns the nodes between the given range.
Expand All @@ -780,7 +764,6 @@ func (t *Tree) Structure() TreeNodeForTest {
// PathToPos returns the position of the given path
func (t *Tree) PathToPos(path []int) (*TreePos, error) {
treePos, err := t.IndexTree.PathToTreePos(path)

if err != nil {
return nil, err
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/document/json/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,16 @@ func (t *Tree) Edit(fromIdx, toIdx int, content *TreeNode) bool {
}
}

fromPos, fromErr := t.Tree.FindPos(fromIdx)
toPos, toErr := t.Tree.FindPos(toIdx)

if fromErr != nil {
panic(fromErr)
} else if toErr != nil {
panic(toErr)
fromPos, err := t.Tree.FindPos(fromIdx)
if err != nil {
panic(err)
}
toPos, err := t.Tree.FindPos(toIdx)
if err != nil {
panic(err)
}

var clone *crdt.TreeNode
var err error
if node != nil {
clone, err = node.DeepCopy()
if err != nil {
Expand Down
Loading

1 comment on commit 1fda3a6

@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: 1fda3a6 Previous: 4ea648c Ratio
BenchmarkDocument/constructor_test 1251 ns/op 752 B/op 12 allocs/op 1545 ns/op 752 B/op 12 allocs/op 0.81
BenchmarkDocument/status_test 640.1 ns/op 720 B/op 10 allocs/op 1168 ns/op 720 B/op 10 allocs/op 0.55
BenchmarkDocument/equals_test 7163 ns/op 5072 B/op 85 allocs/op 8895 ns/op 5072 B/op 85 allocs/op 0.81
BenchmarkDocument/nested_update_test 20958 ns/op 11033 B/op 235 allocs/op 26694 ns/op 11033 B/op 235 allocs/op 0.79
BenchmarkDocument/delete_test 27385 ns/op 14161 B/op 310 allocs/op 34117 ns/op 14163 B/op 310 allocs/op 0.80
BenchmarkDocument/object_test 9692 ns/op 5792 B/op 97 allocs/op 11859 ns/op 5792 B/op 97 allocs/op 0.82
BenchmarkDocument/array_test 34682 ns/op 10889 B/op 251 allocs/op 42645 ns/op 10890 B/op 251 allocs/op 0.81
BenchmarkDocument/text_test 37143 ns/op 14058 B/op 456 allocs/op 45440 ns/op 14058 B/op 456 allocs/op 0.82
BenchmarkDocument/text_composition_test 37793 ns/op 17541 B/op 461 allocs/op 46261 ns/op 17538 B/op 461 allocs/op 0.82
BenchmarkDocument/rich_text_test 99566 ns/op 36006 B/op 1108 allocs/op 125421 ns/op 36016 B/op 1108 allocs/op 0.79
BenchmarkDocument/counter_test 19393 ns/op 9057 B/op 212 allocs/op 23699 ns/op 9057 B/op 212 allocs/op 0.82
BenchmarkDocument/text_edit_gc_100 4044845 ns/op 1552475 B/op 17148 allocs/op 4695110 ns/op 1552945 B/op 17150 allocs/op 0.86
BenchmarkDocument/text_edit_gc_1000 315162167 ns/op 136627452 B/op 210676 allocs/op 373729523 ns/op 136614104 B/op 210613 allocs/op 0.84
BenchmarkDocument/text_split_gc_100 4647187 ns/op 2217127 B/op 16576 allocs/op 5310214 ns/op 2217268 B/op 16575 allocs/op 0.88
BenchmarkDocument/text_split_gc_1000 374239992 ns/op 214845629 B/op 211367 allocs/op 456030707 ns/op 214829213 B/op 211274 allocs/op 0.82
BenchmarkDocument/text_delete_all_10000 16336016 ns/op 5903922 B/op 41122 allocs/op 20758627 ns/op 5903529 B/op 41120 allocs/op 0.79
BenchmarkDocument/text_delete_all_100000 191406484 ns/op 53849352 B/op 416022 allocs/op 277125629 ns/op 53842246 B/op 415985 allocs/op 0.69
BenchmarkDocument/text_100 302282 ns/op 117748 B/op 5064 allocs/op 386317 ns/op 117750 B/op 5064 allocs/op 0.78
BenchmarkDocument/text_1000 3294365 ns/op 1152327 B/op 50068 allocs/op 4349761 ns/op 1152368 B/op 50068 allocs/op 0.76
BenchmarkDocument/array_1000 1699470 ns/op 1102035 B/op 11854 allocs/op 2228492 ns/op 1102265 B/op 11855 allocs/op 0.76
BenchmarkDocument/array_10000 18754874 ns/op 9905945 B/op 120704 allocs/op 24525672 ns/op 9906337 B/op 120705 allocs/op 0.76
BenchmarkDocument/array_gc_100 178954 ns/op 97414 B/op 1226 allocs/op 223669 ns/op 97414 B/op 1226 allocs/op 0.80
BenchmarkDocument/array_gc_1000 1976991 ns/op 1169512 B/op 12889 allocs/op 2323875 ns/op 1169572 B/op 12889 allocs/op 0.85
BenchmarkDocument/counter_1000 270892 ns/op 197876 B/op 6490 allocs/op 330728 ns/op 197877 B/op 6490 allocs/op 0.82
BenchmarkDocument/counter_10000 2935239 ns/op 2164817 B/op 69497 allocs/op 3709166 ns/op 2164835 B/op 69497 allocs/op 0.79
BenchmarkDocument/object_1000 1847159 ns/op 1450812 B/op 9902 allocs/op 2176832 ns/op 1450706 B/op 9902 allocs/op 0.85
BenchmarkDocument/object_10000 21797172 ns/op 12368756 B/op 101207 allocs/op 25235487 ns/op 12366848 B/op 101199 allocs/op 0.86
BenchmarkRPC/client_to_server 423986133 ns/op 18835013 B/op 306850 allocs/op 531950074 ns/op 18897104 B/op 304291 allocs/op 0.80
BenchmarkRPC/client_to_client_via_server 723887996 ns/op 34433488 B/op 563108 allocs/op 915461624 ns/op 34319028 B/op 560752 allocs/op 0.79
BenchmarkRPC/attach_large_document 1562174819 ns/op 2159513896 B/op 10508 allocs/op 1680941992 ns/op 2155547248 B/op 9927 allocs/op 0.93
BenchmarkRPC/adminCli_to_server 520585772 ns/op 20396500 B/op 322107 allocs/op 672776222 ns/op 20400180 B/op 322105 allocs/op 0.77
BenchmarkLocker 120.6 ns/op 16 B/op 1 allocs/op 141.3 ns/op 16 B/op 1 allocs/op 0.85
BenchmarkLockerParallel 108.7 ns/op 0 B/op 0 allocs/op 180.8 ns/op 0 B/op 0 allocs/op 0.60
BenchmarkLockerMoreKeys 328.6 ns/op 14 B/op 0 allocs/op 479.8 ns/op 13 B/op 0 allocs/op 0.68
BenchmarkSync/memory_sync_10_test 7276 ns/op 1340 B/op 39 allocs/op 8725 ns/op 1338 B/op 39 allocs/op 0.83
BenchmarkSync/memory_sync_100_test 60365 ns/op 9093 B/op 299 allocs/op 82836 ns/op 8713 B/op 275 allocs/op 0.73
BenchmarkSync/memory_sync_1000_test 610986 ns/op 84219 B/op 2732 allocs/op 790827 ns/op 81867 B/op 2581 allocs/op 0.77
BenchmarkSync/memory_sync_10000_test 6517364 ns/op 862181 B/op 27630 allocs/op 8059162 ns/op 869299 B/op 27283 allocs/op 0.81
BenchmarkTextEditing 24471320713 ns/op 8436056320 B/op 19834769 allocs/op 30303944825 ns/op 8435794576 B/op 19834770 allocs/op 0.81

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

Please sign in to comment.