Skip to content

Commit

Permalink
Apply the name convention in converter and crdt.Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sejongk committed Aug 14, 2023
1 parent 2b3e7f8 commit b805849
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 49 deletions.
12 changes: 6 additions & 6 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions api/converter/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
},
}
}
51 changes: 29 additions & 22 deletions pkg/document/crdt/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (

var (
// DummyTreePos is a dummy position of Tree. It is used to represent the head node of RGASplit.

Check failure on line 37 in pkg/document/crdt/tree.go

View workflow job for this annotation

GitHub Actions / build (1.19.2)

exported: comment on exported var DummyTreeNodeID should be of the form "DummyTreeNodeID ..." (revive)
DummyTreeNodeId = &TreeNodeID{
DummyTreeNodeID = &TreeNodeID{
CreatedAt: time.InitialTicket,
Offset: 0,
}
Expand Down Expand Up @@ -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.

Check failure on line 88 in pkg/document/crdt/tree.go

View workflow job for this annotation

GitHub Actions / build (1.19.2)

exported: comment on exported method TreePos.Equals should be of the form "Equals ..." (revive)
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,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -811,7 +818,7 @@ func (t *Tree) findTreeNodesWithSplitText(pos *TreePos, editedAt *time.Ticket) (

// toTreePos converts the given crdt.TreePos to local index.TreePos<CRDTTreeNode>.
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
}

Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/document/crdt/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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, "<p>helloyorkie</p>", crdt.ToXML(para))
assert.Equal(t, 11, para.Len())
Expand All @@ -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, `<span font-weight="bold">helloyorkie</span>`, crdt.ToXML(node))
})
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pkg/document/json/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand All @@ -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
}
Expand Down

0 comments on commit b805849

Please sign in to comment.