From b805849328068ff9c5fcdcf0f7a799b96f4e1f0c Mon Sep 17 00:00:00 2001 From: sejongk Date: Mon, 14 Aug 2023 11:54:49 +0900 Subject: [PATCH] Apply the name convention in converter and crdt.Tree --- api/converter/from_pb.go | 12 ++++---- api/converter/to_bytes.go | 14 +++++----- pkg/document/crdt/tree.go | 51 +++++++++++++++++++--------------- pkg/document/crdt/tree_test.go | 16 +++++------ pkg/document/json/tree.go | 12 ++++---- 5 files changed, 56 insertions(+), 49 deletions(-) diff --git a/api/converter/from_pb.go b/api/converter/from_pb.go index b97f4e91b..1d15f8539 100644 --- a/api/converter/from_pb.go +++ b/api/converter/from_pb.go @@ -620,7 +620,7 @@ func FromTreeNodesWhenEdit(pbNodes []*api.TreeNodes) ([]*crdt.TreeNode, error) { } func fromTreeNode(pbNode *api.TreeNode) (*crdt.TreeNode, error) { - pos, err := fromTreeNodeId(pbNode.Pos) + pos, err := fromTreeNodeID(pbNode.Pos) if err != nil { return nil, err } @@ -643,26 +643,26 @@ func fromTreeNode(pbNode *api.TreeNode) (*crdt.TreeNode, error) { } func fromTreePos(pbPos *api.TreePos) (*crdt.TreePos, error) { - parentId, err := fromTreeNodeId(pbPos.ParentId) + parentID, err := fromTreeNodeID(pbPos.ParentId) if err != nil { return nil, err } - leftSiblingId, err := fromTreeNodeId(pbPos.LeftSiblingId) + leftSiblingID, err := fromTreeNodeID(pbPos.LeftSiblingId) if err != nil { return nil, err } - return crdt.NewTreePos(parentId, leftSiblingId), nil + return crdt.NewTreePos(parentID, leftSiblingID), nil } -func fromTreeNodeId(pbPos *api.TreeNodeID) (*crdt.TreeNodeID, error) { +func fromTreeNodeID(pbPos *api.TreeNodeID) (*crdt.TreeNodeID, error) { createdAt, err := fromTimeTicket(pbPos.CreatedAt) if err != nil { return nil, err } - return crdt.NewTreeNodeId( + return crdt.NewTreeNodeID( createdAt, int(pbPos.Offset), ), nil diff --git a/api/converter/to_bytes.go b/api/converter/to_bytes.go index cfd85ae67..e1cbd8038 100644 --- a/api/converter/to_bytes.go +++ b/api/converter/to_bytes.go @@ -298,7 +298,7 @@ func toTreeNode(treeNode *crdt.TreeNode, depth int) *api.TreeNode { } pbNode := &api.TreeNode{ - Pos: toTreeNodeId(treeNode.Pos), + Pos: toTreeNodeID(treeNode.Pos), Type: treeNode.Type(), Value: treeNode.Value, RemovedAt: ToTimeTicket(treeNode.RemovedAt), @@ -307,13 +307,13 @@ func toTreeNode(treeNode *crdt.TreeNode, depth int) *api.TreeNode { } if treeNode.InsPrev != nil { - pbNode.InsPrevPos = toTreeNodeId(treeNode.InsPrev.Pos) + pbNode.InsPrevPos = toTreeNodeID(treeNode.InsPrev.Pos) } return pbNode } -func toTreeNodeId(pos *crdt.TreeNodeID) *api.TreeNodeID { +func toTreeNodeID(pos *crdt.TreeNodeID) *api.TreeNodeID { return &api.TreeNodeID{ CreatedAt: ToTimeTicket(pos.CreatedAt), Offset: int32(pos.Offset), @@ -323,12 +323,12 @@ func toTreeNodeId(pos *crdt.TreeNodeID) *api.TreeNodeID { func toTreePos(pos *crdt.TreePos) *api.TreePos { return &api.TreePos{ ParentId: &api.TreeNodeID{ - CreatedAt: ToTimeTicket(pos.ParentId.CreatedAt), - Offset: int32(pos.ParentId.Offset), + CreatedAt: ToTimeTicket(pos.ParentID.CreatedAt), + Offset: int32(pos.ParentID.Offset), }, LeftSiblingId: &api.TreeNodeID{ - CreatedAt: ToTimeTicket(pos.LeftSiblingId.CreatedAt), - Offset: int32(pos.LeftSiblingId.Offset), + CreatedAt: ToTimeTicket(pos.LeftSiblingID.CreatedAt), + Offset: int32(pos.LeftSiblingID.Offset), }, } } diff --git a/pkg/document/crdt/tree.go b/pkg/document/crdt/tree.go index aaa2a053e..a567a97a9 100644 --- a/pkg/document/crdt/tree.go +++ b/pkg/document/crdt/tree.go @@ -35,7 +35,7 @@ var ( var ( // DummyTreePos is a dummy position of Tree. It is used to represent the head node of RGASplit. - DummyTreeNodeId = &TreeNodeID{ + DummyTreeNodeID = &TreeNodeID{ CreatedAt: time.InitialTicket, Offset: 0, } @@ -73,34 +73,41 @@ type TreeNode struct { // TreePos represents the position of Tree. type TreePos struct { - ParentId *TreeNodeID - LeftSiblingId *TreeNodeID + ParentID *TreeNodeID + LeftSiblingID *TreeNodeID } // NewTreePos creates a new instance of TreePos. -func NewTreePos(parentId *TreeNodeID, leftSiblingId *TreeNodeID) *TreePos { +func NewTreePos(parentID *TreeNodeID, leftSiblingID *TreeNodeID) *TreePos { return &TreePos{ - ParentId: parentId, - LeftSiblingId: leftSiblingId, + ParentID: parentID, + LeftSiblingID: leftSiblingID, } } // Compare compares the given two CRDTTreePos. func (t *TreePos) Equals(other *TreePos) bool { - return (t.ParentId.CreatedAt.Compare(other.ParentId.CreatedAt) == 0 && - t.ParentId.Offset == other.ParentId.Offset && - t.LeftSiblingId.CreatedAt.Compare(other.LeftSiblingId.CreatedAt) == 0 && - t.LeftSiblingId.Offset == other.LeftSiblingId.Offset) + return (t.ParentID.CreatedAt.Compare(other.ParentID.CreatedAt) == 0 && + t.ParentID.Offset == other.ParentID.Offset && + t.LeftSiblingID.CreatedAt.Compare(other.LeftSiblingID.CreatedAt) == 0 && + t.LeftSiblingID.Offset == other.LeftSiblingID.Offset) } -// TreePos represents the position of Tree. +/** + * `TreeNodeID` represent an ID of a node in the tree. It is used to + * identify a node in the tree. It is composed of the creation time of the node + * and the offset from the beginning of the node if the node is split. + * + * Some of replicas may have nodes that are not split yet. In this case, we can + * use `map.floorEntry()` to find the adjacent node. + */ type TreeNodeID struct { CreatedAt *time.Ticket Offset int } // NewTreePos creates a new instance of TreePos. -func NewTreeNodeId(createdAt *time.Ticket, offset int) *TreeNodeID { +func NewTreeNodeID(createdAt *time.Ticket, offset int) *TreeNodeID { return &TreeNodeID{ CreatedAt: createdAt, Offset: offset, @@ -569,8 +576,8 @@ func (t *Tree) FindPos(offset int) (*TreePos, error) { } return &TreePos{ - ParentId: node.Value.Pos, - LeftSiblingId: &TreeNodeID{ + ParentID: node.Value.Pos, + LeftSiblingID: &TreeNodeID{ CreatedAt: leftSibling.Pos.CreatedAt, Offset: leftSibling.Pos.Offset + offset, }, @@ -774,7 +781,7 @@ func (t *Tree) findTreeNodesWithSplitText(pos *TreePos, editedAt *time.Ticket) ( // handle the same position insertion of RGA. if leftSiblingNode.IsText() { absOffset := leftSiblingNode.Pos.Offset - split, err := leftSiblingNode.Split(pos.LeftSiblingId.Offset-absOffset, absOffset) + split, err := leftSiblingNode.Split(pos.LeftSiblingID.Offset-absOffset, absOffset) if err != nil { return nil, nil, err } @@ -811,7 +818,7 @@ func (t *Tree) findTreeNodesWithSplitText(pos *TreePos, editedAt *time.Ticket) ( // toTreePos converts the given crdt.TreePos to local index.TreePos. func (t *Tree) toTreePos(pos *TreePos) *index.TreePos[*TreeNode] { - if pos.ParentId == nil || pos.LeftSiblingId == nil { + if pos.ParentID == nil || pos.LeftSiblingID == nil { return nil } @@ -867,18 +874,18 @@ func (t *Tree) toIndex(pos *TreePos) (int, error) { } func (t *Tree) toTreeNodes(pos *TreePos) (*TreeNode, *TreeNode) { - parentKey, parentNode := t.NodeMapByPos.Floor(pos.ParentId) - leftSiblingKey, leftSiblingNode := t.NodeMapByPos.Floor(pos.LeftSiblingId) + parentKey, parentNode := t.NodeMapByPos.Floor(pos.ParentID) + leftSiblingKey, leftSiblingNode := t.NodeMapByPos.Floor(pos.LeftSiblingID) if parentNode == nil || leftSiblingNode == nil || - parentKey.CreatedAt.Compare(pos.ParentId.CreatedAt) != 0 || - leftSiblingKey.CreatedAt.Compare(pos.LeftSiblingId.CreatedAt) != 0 { + parentKey.CreatedAt.Compare(pos.ParentID.CreatedAt) != 0 || + leftSiblingKey.CreatedAt.Compare(pos.LeftSiblingID.CreatedAt) != 0 { return nil, nil } - if pos.LeftSiblingId.Offset > 0 && - pos.LeftSiblingId.Offset == leftSiblingNode.Pos.Offset && + if pos.LeftSiblingID.Offset > 0 && + pos.LeftSiblingID.Offset == leftSiblingNode.Pos.Offset && leftSiblingNode.InsPrev != nil { return parentNode, leftSiblingNode.InsPrev } diff --git a/pkg/document/crdt/tree_test.go b/pkg/document/crdt/tree_test.go index 39695db58..67b44c850 100644 --- a/pkg/document/crdt/tree_test.go +++ b/pkg/document/crdt/tree_test.go @@ -28,8 +28,8 @@ import ( func TestTreeNode(t *testing.T) { t.Run("text node test", func(t *testing.T) { - node := crdt.NewTreeNode(crdt.DummyTreeNodeId, "text", nil, "hello") - assert.Equal(t, crdt.DummyTreeNodeId, node.Pos) + node := crdt.NewTreeNode(crdt.DummyTreeNodeID, "text", nil, "hello") + assert.Equal(t, crdt.DummyTreeNodeID, node.Pos) assert.Equal(t, "text", node.Type()) assert.Equal(t, "hello", node.Value) assert.Equal(t, 5, node.Len()) @@ -38,8 +38,8 @@ func TestTreeNode(t *testing.T) { }) t.Run("element node test", func(t *testing.T) { - para := crdt.NewTreeNode(crdt.DummyTreeNodeId, "p", nil) - err := para.Append(crdt.NewTreeNode(crdt.DummyTreeNodeId, "text", nil, "helloyorkie")) + para := crdt.NewTreeNode(crdt.DummyTreeNodeID, "p", nil) + err := para.Append(crdt.NewTreeNode(crdt.DummyTreeNodeID, "text", nil, "helloyorkie")) assert.NoError(t, err) assert.Equal(t, "

helloyorkie

", crdt.ToXML(para)) assert.Equal(t, 11, para.Len()) @@ -61,8 +61,8 @@ func TestTreeNode(t *testing.T) { t.Run("element node with attributes test", func(t *testing.T) { attrs := crdt.NewRHT() attrs.Set("font-weight", "bold", time.InitialTicket) - node := crdt.NewTreeNode(crdt.DummyTreeNodeId, "span", attrs) - err := node.Append(crdt.NewTreeNode(crdt.DummyTreeNodeId, "text", nil, "helloyorkie")) + node := crdt.NewTreeNode(crdt.DummyTreeNodeID, "span", attrs) + err := node.Append(crdt.NewTreeNode(crdt.DummyTreeNodeID, "text", nil, "helloyorkie")) assert.NoError(t, err) assert.Equal(t, `helloyorkie`, crdt.ToXML(node)) }) @@ -79,8 +79,8 @@ func TestTreeNode(t *testing.T) { {12, "🌷🎁💩😜👍🏳"}, } for _, test := range tests { - para := crdt.NewTreeNode(crdt.DummyTreeNodeId, "p", nil) - err := para.Append(crdt.NewTreeNode(crdt.DummyTreeNodeId, "text", nil, test.value)) + para := crdt.NewTreeNode(crdt.DummyTreeNodeID, "p", nil) + err := para.Append(crdt.NewTreeNode(crdt.DummyTreeNodeID, "text", nil, test.value)) assert.NoError(t, err) left, err := para.Child(0) assert.NoError(t, err) diff --git a/pkg/document/json/tree.go b/pkg/document/json/tree.go index ad0e91a1f..38d3040ec 100644 --- a/pkg/document/json/tree.go +++ b/pkg/document/json/tree.go @@ -153,7 +153,7 @@ func (t *Tree) edit(fromPos, toPos *crdt.TreePos, contents []*TreeNode) bool { value += content.Value } - nodes = append(nodes, crdt.NewTreeNode(crdt.NewTreeNodeId(ticket, 0), index.DefaultTextType, nil, value)) + nodes = append(nodes, crdt.NewTreeNode(crdt.NewTreeNodeID(ticket, 0), index.DefaultTextType, nil, value)) } else { for _, content := range contents { var attributes *crdt.RHT @@ -165,7 +165,7 @@ func (t *Tree) edit(fromPos, toPos *crdt.TreePos, contents []*TreeNode) bool { } var node *crdt.TreeNode - node = crdt.NewTreeNode(crdt.NewTreeNodeId(ticket, 0), content.Type, attributes, content.Value) + node = crdt.NewTreeNode(crdt.NewTreeNodeID(ticket, 0), content.Type, attributes, content.Value) for _, child := range content.Children { if err := buildDescendants(t.context, child, node); err != nil { @@ -269,10 +269,10 @@ func (t *Tree) Style(fromIdx, toIdx int, attributes map[string]string) bool { // node is nil, it creates a default root node. func buildRoot(ctx *change.Context, node *TreeNode, createdAt *time.Ticket) *crdt.TreeNode { if node == nil { - return crdt.NewTreeNode(crdt.NewTreeNodeId(createdAt, 0), DefaultRootNodeType, nil) + return crdt.NewTreeNode(crdt.NewTreeNodeID(createdAt, 0), DefaultRootNodeType, nil) } - root := crdt.NewTreeNode(crdt.NewTreeNodeId(createdAt, 0), node.Type, nil) + root := crdt.NewTreeNode(crdt.NewTreeNodeID(createdAt, 0), node.Type, nil) for _, child := range node.Children { if err := buildDescendants(ctx, child, root); err != nil { panic(err) @@ -290,7 +290,7 @@ func buildDescendants(ctx *change.Context, n TreeNode, parent *crdt.TreeNode) er return err } - treeNode := crdt.NewTreeNode(crdt.NewTreeNodeId(ctx.IssueTimeTicket(), 0), n.Type, nil, n.Value) + treeNode := crdt.NewTreeNode(crdt.NewTreeNodeID(ctx.IssueTimeTicket(), 0), n.Type, nil, n.Value) return parent.Append(treeNode) } @@ -304,7 +304,7 @@ func buildDescendants(ctx *change.Context, n TreeNode, parent *crdt.TreeNode) er } } - treeNode := crdt.NewTreeNode(crdt.NewTreeNodeId(ticket, 0), n.Type, attributes) + treeNode := crdt.NewTreeNode(crdt.NewTreeNodeID(ticket, 0), n.Type, attributes) if err := parent.Append(treeNode); err != nil { return err }