Skip to content

Commit

Permalink
Merge branch 'main' into hybrid-clock
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jul 15, 2024
2 parents cbd8918 + b468f8b commit cf42a63
Show file tree
Hide file tree
Showing 100 changed files with 4,577 additions and 2,011 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ on:
branches: [ main ]
pull_request:
branches: [ main ]
paths-ignore:
- 'api/docs/**'
- 'build/charts/**'
- '**/*.md'
- '**/*.txt'
- '**/.gitignore'

env:
GO_VERSION: '1.21'
Expand Down Expand Up @@ -50,6 +56,8 @@ jobs:
uses: codecov/codecov-action@v3
with:
file: ./coverage.txt
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

bench:
name: bench
Expand Down
452 changes: 309 additions & 143 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
YORKIE_VERSION := 0.4.15
YORKIE_VERSION := 0.4.27

GO_PROJECT = github.com/yorkie-team/yorkie

Expand Down
31 changes: 31 additions & 0 deletions api/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,35 @@ func TestConverter(t *testing.T) {
clone := converter.FromPresenceChange(pbChange)
assert.Equal(t, change, clone)
})

t.Run("properly encode and decode tree test", func(t *testing.T) {
doc := document.New(helper.TestDocKey(t))
assert.NoError(t, doc.Update(func(root *json.Object, p *presence.Presence) error {
root.SetNewTree("t", &json.TreeNode{
Type: "r",
Children: []json.TreeNode{
{Type: "p", Children: []json.TreeNode{{Type: "text", Value: "12"}}},
{Type: "p", Children: []json.TreeNode{{Type: "text", Value: "34"}}},
},
})
assert.Equal(t, "<r><p>12</p><p>34</p></r>", root.GetTree("t").ToXML())
root.GetTree("t").EditByPath([]int{0, 1}, []int{1, 1}, nil, 0)

root.GetTree("t").Style(0, 1, map[string]string{"b": "t", "i": "t"})
assert.Equal(t, `<r><p b="t" i="t">14</p></r>`, root.GetTree("t").ToXML())

root.GetTree("t").RemoveStyle(0, 1, []string{"i"})
return nil
}))
assert.Equal(t, `<r><p b="t">14</p></r>`, doc.Root().GetTree("t").ToXML())

bytes, err := converter.ObjectToBytes(doc.RootObject())
assert.NoError(t, err)
obj, err := converter.BytesToObject(bytes)
assert.NoError(t, err)

assert.Equal(t, obj.Get("t").(*crdt.Tree).NodeLen(), doc.Root().GetTree("t").NodeLen())
assert.Equal(t, obj.Get("t").(*crdt.Tree).Root().Len(), doc.Root().GetTree("t").Len())
assert.Equal(t, obj.Get("t").(*crdt.Tree).ToXML(), doc.Root().GetTree("t").ToXML())
})
}
27 changes: 27 additions & 0 deletions api/converter/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package converter

import (
"errors"

"connectrpc.com/connect"
"google.golang.org/genproto/googleapis/rpc/errdetails"
)

// ErrorCodeOf returns the error code of the given error.
func ErrorCodeOf(err error) string {
var connectErr *connect.Error
if !errors.As(err, &connectErr) {
return ""
}
for _, detail := range connectErr.Details() {
msg, valueErr := detail.Value()
if valueErr != nil {
continue
}

if errorInfo, ok := msg.(*errdetails.ErrorInfo); ok {
return errorInfo.GetMetadata()["code"]
}
}
return ""
}
36 changes: 28 additions & 8 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,19 @@ func fromTreeStyle(pbTreeStyle *api.Operation_TreeStyle) (*operations.TreeStyle,
return nil, err
}

createdAtMapByActor, err := fromCreatedAtMapByActor(
pbTreeStyle.CreatedAtMapByActor,
)
if err != nil {
return nil, err
}

if len(pbTreeStyle.AttributesToRemove) > 0 {
return operations.NewTreeStyleRemove(
parentCreatedAt,
from,
to,
createdAtMapByActor,
pbTreeStyle.AttributesToRemove,
executedAt,
), nil
Expand All @@ -561,6 +569,7 @@ func fromTreeStyle(pbTreeStyle *api.Operation_TreeStyle) (*operations.TreeStyle,
parentCreatedAt,
from,
to,
createdAtMapByActor,
pbTreeStyle.Attributes,
executedAt,
), nil
Expand Down Expand Up @@ -624,6 +633,8 @@ func FromTreeNodes(pbNodes []*api.TreeNode) (*crdt.TreeNode, error) {
}
}

root.Index.UpdateDescendantsSize()

// build crdt.Tree from root to construct the links between nodes.
return crdt.NewTree(root, nil).Root(), nil
}
Expand All @@ -650,19 +661,27 @@ func FromTreeNodesWhenEdit(pbNodes []*api.TreeNodes) ([]*crdt.TreeNode, error) {
return treeNodes, nil
}

func fromRHT(pbRHT map[string]*api.NodeAttr) (*crdt.RHT, error) {
rht := crdt.NewRHT()
for k, pbAttr := range pbRHT {
updatedAt, err := fromTimeTicket(pbAttr.UpdatedAt)
if err != nil {
return nil, err
}
rht.SetInternal(k, pbAttr.Value, updatedAt, pbAttr.IsRemoved)
}
return rht, nil
}

func fromTreeNode(pbNode *api.TreeNode) (*crdt.TreeNode, error) {
id, err := fromTreeNodeID(pbNode.Id)
if err != nil {
return nil, err
}

attrs := crdt.NewRHT()
for k, pbAttr := range pbNode.Attributes {
updatedAt, err := fromTimeTicket(pbAttr.UpdatedAt)
if err != nil {
return nil, err
}
attrs.Set(k, pbAttr.Value, updatedAt)
attrs, err := fromRHT(pbNode.Attributes)
if err != nil {
return nil, err
}

node := crdt.NewTreeNode(
Expand All @@ -686,10 +705,11 @@ func fromTreeNode(pbNode *api.TreeNode) (*crdt.TreeNode, error) {
}
}

node.RemovedAt, err = fromTimeTicket(pbNode.RemovedAt)
removedAt, err := fromTimeTicket(pbNode.RemovedAt)
if err != nil {
return nil, err
}
node.SetRemovedAt(removedAt)

return node, nil
}
Expand Down
23 changes: 14 additions & 9 deletions api/converter/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,30 @@ func ToTreeNodesWhenEdit(treeNodes []*crdt.TreeNode) []*api.TreeNodes {
return pbTreeNodes
}

func toTreeNode(treeNode *crdt.TreeNode, depth int) *api.TreeNode {
var attrs map[string]*api.NodeAttr
if treeNode.Attrs != nil {
attrs = make(map[string]*api.NodeAttr)
for _, node := range treeNode.Attrs.Nodes() {
attrs[node.Key()] = &api.NodeAttr{
func toRHT(rht *crdt.RHT) map[string]*api.NodeAttr {
var pbAttrs map[string]*api.NodeAttr
if rht != nil {
pbAttrs = make(map[string]*api.NodeAttr)
for _, node := range rht.Nodes() {
pbAttrs[node.Key()] = &api.NodeAttr{
Value: node.Value(),
UpdatedAt: ToTimeTicket(node.UpdatedAt()),
IsRemoved: node.IsRemoved(),
}
}
}

return pbAttrs
}

func toTreeNode(treeNode *crdt.TreeNode, depth int) *api.TreeNode {
pbNode := &api.TreeNode{
Id: toTreeNodeID(treeNode.ID),
Id: toTreeNodeID(treeNode.ID()),
Type: treeNode.Type(),
Value: treeNode.Value,
RemovedAt: ToTimeTicket(treeNode.RemovedAt),
RemovedAt: ToTimeTicket(treeNode.RemovedAt()),
Depth: int32(depth),
Attributes: attrs,
Attributes: toRHT(treeNode.Attrs),
}

if treeNode.InsPrevID != nil {
Expand Down
19 changes: 10 additions & 9 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func toEdit(e *operations.Edit) (*api.Operation_Edit_, error) {
ParentCreatedAt: ToTimeTicket(e.ParentCreatedAt()),
From: toTextNodePos(e.From()),
To: toTextNodePos(e.To()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.CreatedAtMapByActor()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.MaxCreatedAtMapByActor()),
Content: e.Content(),
Attributes: e.Attributes(),
ExecutedAt: ToTimeTicket(e.ExecutedAt()),
Expand All @@ -352,7 +352,7 @@ func toStyle(style *operations.Style) (*api.Operation_Style_, error) {
ParentCreatedAt: ToTimeTicket(style.ParentCreatedAt()),
From: toTextNodePos(style.From()),
To: toTextNodePos(style.To()),
CreatedAtMapByActor: toCreatedAtMapByActor(style.CreatedAtMapByActor()),
CreatedAtMapByActor: toCreatedAtMapByActor(style.MaxCreatedAtMapByActor()),
Attributes: style.Attributes(),
ExecutedAt: ToTimeTicket(style.ExecutedAt()),
},
Expand Down Expand Up @@ -382,7 +382,7 @@ func toTreeEdit(e *operations.TreeEdit) (*api.Operation_TreeEdit_, error) {
To: toTreePos(e.ToPos()),
Contents: ToTreeNodesWhenEdit(e.Contents()),
SplitLevel: int32(e.SplitLevel()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.CreatedAtMapByActor()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.MaxCreatedAtMapByActor()),
ExecutedAt: ToTimeTicket(e.ExecutedAt()),
},
}, nil
Expand All @@ -391,12 +391,13 @@ func toTreeEdit(e *operations.TreeEdit) (*api.Operation_TreeEdit_, error) {
func toTreeStyle(style *operations.TreeStyle) (*api.Operation_TreeStyle_, error) {
return &api.Operation_TreeStyle_{
TreeStyle: &api.Operation_TreeStyle{
ParentCreatedAt: ToTimeTicket(style.ParentCreatedAt()),
From: toTreePos(style.FromPos()),
To: toTreePos(style.ToPos()),
Attributes: style.Attributes(),
ExecutedAt: ToTimeTicket(style.ExecutedAt()),
AttributesToRemove: style.AttributesToRemove(),
ParentCreatedAt: ToTimeTicket(style.ParentCreatedAt()),
From: toTreePos(style.FromPos()),
To: toTreePos(style.ToPos()),
Attributes: style.Attributes(),
ExecutedAt: ToTimeTicket(style.ExecutedAt()),
AttributesToRemove: style.AttributesToRemove(),
CreatedAtMapByActor: toCreatedAtMapByActor(style.MaxCreatedAtMapByActor()),
},
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion api/docs/yorkie.base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.1.0
info:
title: Yorkie
description: "Yorkie is an open source document store for building collaborative editing applications."
version: v0.4.15
version: v0.4.27
servers:
- url: https://api.yorkie.dev
description: Production server
Expand Down
Loading

0 comments on commit cf42a63

Please sign in to comment.