Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove panic method in crdt text #522

Merged
merged 6 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/document/crdt/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package crdt

import (
"fmt"

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

Expand Down Expand Up @@ -95,16 +97,20 @@ func (a *Array) StructureAsString() string {
}

// DeepCopy copies itself deeply.
func (a *Array) DeepCopy() Element {
func (a *Array) DeepCopy() (Element, error) {
elements := NewRGATreeList()

for _, node := range a.elements.Nodes() {
elements.Add(node.elem.DeepCopy())
copiedNode, err := node.elem.DeepCopy()
if err != nil {
return nil, fmt.Errorf("crdt array DeepCopy: %w", err)
}
elements.Add(copiedNode)
}

array := NewArray(elements, a.createdAt)
array.removedAt = a.removedAt
return array
return array, nil
}

// CreatedAt returns the creation time of this array.
Expand Down
4 changes: 2 additions & 2 deletions pkg/document/crdt/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func (p *Counter) Marshal() string {
}

// DeepCopy copies itself deeply.
func (p *Counter) DeepCopy() Element {
func (p *Counter) DeepCopy() (Element, error) {
counter := *p
return &counter
return &counter, nil
}

// CreatedAt returns the creation time.
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/crdt/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Element interface {
Marshal() string

// DeepCopy copies itself deeply.
DeepCopy() Element
DeepCopy() (Element, error)

// CreatedAt returns the creation time of this element.
CreatedAt() *time.Ticket
Expand Down
12 changes: 9 additions & 3 deletions pkg/document/crdt/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package crdt

import (
"fmt"

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

Expand Down Expand Up @@ -94,16 +96,20 @@ func (o *Object) Marshal() string {
}

// DeepCopy copies itself deeply.
func (o *Object) DeepCopy() Element {
func (o *Object) DeepCopy() (Element, error) {
members := NewElementRHT()

for _, node := range o.memberNodes.Nodes() {
members.Set(node.key, node.elem.DeepCopy())
copiedNode, err := node.elem.DeepCopy()
if err != nil {
return nil, fmt.Errorf("crdt object DeepCopy: %w", err)
}
members.Set(node.key, copiedNode)
}

obj := NewObject(members, o.createdAt)
obj.removedAt = o.removedAt
return obj
return obj, nil
}

// CreatedAt returns the creation time of this object.
Expand Down
4 changes: 2 additions & 2 deletions pkg/document/crdt/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ func (p *Primitive) Marshal() string {
}

// DeepCopy copies itself deeply.
func (p *Primitive) DeepCopy() Element {
func (p *Primitive) DeepCopy() (Element, error) {
primitive := *p
return &primitive
return &primitive, nil
}

// CreatedAt returns the creation time.
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/crdt/primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestPrimitive(t *testing.T) {
assert.Equal(t, prim.Value(), crdt.ValueFromBytes(prim.ValueType(), prim.Bytes()))
assert.Equal(t, prim.Marshal(), test.marshal)

copied := prim.DeepCopy()
copied, _ := prim.DeepCopy()
assert.Equal(t, prim.CreatedAt(), copied.CreatedAt())
assert.Equal(t, prim.MovedAt(), copied.MovedAt())
assert.Equal(t, prim.Marshal(), copied.Marshal())
Expand Down
10 changes: 8 additions & 2 deletions pkg/document/crdt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package crdt

import (
"fmt"

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

Expand Down Expand Up @@ -101,8 +103,12 @@ func (r *Root) RegisterTextElementWithGarbage(textType TextElement) {
}

// DeepCopy copies itself deeply.
func (r *Root) DeepCopy() *Root {
return NewRoot(r.object.DeepCopy().(*Object))
func (r *Root) DeepCopy() (*Root, error) {
copiedObject, err := r.object.DeepCopy()
if err != nil {
return nil, fmt.Errorf("crdt root DeepCopy: %w", err)
}
return NewRoot(copiedObject.(*Object)), nil
}

// GarbageCollect purge elements that were removed before the given time.
Expand Down
6 changes: 3 additions & 3 deletions pkg/document/crdt/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (t *Text) Marshal() string {
}

// DeepCopy copies itself deeply.
func (t *Text) DeepCopy() Element {
func (t *Text) DeepCopy() (Element, error) {
rgaTreeSplit := NewRGATreeSplit(InitialTextNode())

current := rgaTreeSplit.InitialHead()
Expand All @@ -176,13 +176,13 @@ func (t *Text) DeepCopy() Element {
if insPrevID != nil {
insPrevNode := rgaTreeSplit.FindNode(insPrevID)
if insPrevNode == nil {
panic("insPrevNode should be presence")
return nil, fmt.Errorf("crdt text DeepCopy: insPrevNode should be presence")
}
current.SetInsPrev(insPrevNode)
}
}

return NewText(rgaTreeSplit, t.createdAt)
return NewText(rgaTreeSplit, t.createdAt), nil
}

// CreatedAt returns the creation time of this Text.
Expand Down
6 changes: 5 additions & 1 deletion pkg/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ func (d *Document) GarbageLen() int {

func (d *Document) ensureClone() {
if d.clone == nil {
d.clone = d.doc.root.DeepCopy()
copiedDoc, err := d.doc.root.DeepCopy()
if err != nil {
panic("document ensureClone: " + err.Error())
}
d.clone = copiedDoc
}
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/document/json/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package json

import (
"fmt"
gotime "time"

"github.com/yorkie-team/yorkie/pkg/document/change"
Expand Down Expand Up @@ -186,10 +187,14 @@ func (p *Array) insertAfterInternal(
elem := creator(ticket)
value := toOriginal(elem)

copiedValue, err := value.DeepCopy()
if err != nil {
panic(fmt.Sprintf("json array insertAfterInternal: %s", err.Error()))
}
p.context.Push(operations.NewAdd(
p.Array.CreatedAt(),
prevCreatedAt,
value.DeepCopy(),
copiedValue,
ticket,
))

Expand Down
7 changes: 6 additions & 1 deletion pkg/document/json/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package json

import (
"fmt"
gotime "time"

"github.com/yorkie-team/yorkie/pkg/document/change"
Expand Down Expand Up @@ -257,10 +258,14 @@ func (p *Object) setInternal(
elem := creator(ticket)
value := toOriginal(elem)

copiedValue, err := value.DeepCopy()
if err != nil {
panic(fmt.Sprintf("json object setInternal: %s", err.Error()))
}
p.context.Push(operations.NewSet(
p.CreatedAt(),
k,
value.DeepCopy(),
copiedValue,
ticket,
))

Expand Down
7 changes: 6 additions & 1 deletion pkg/document/operations/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package operations

import (
"fmt"

"github.com/yorkie-team/yorkie/pkg/document/crdt"
"github.com/yorkie-team/yorkie/pkg/document/time"
)
Expand Down Expand Up @@ -60,7 +62,10 @@ func (o *Add) Execute(root *crdt.Root) error {
return ErrNotApplicableDataType
}

value := o.value.DeepCopy()
value, err := o.value.DeepCopy()
if err != nil {
return fmt.Errorf("add operations: %w", err)
}
obj.InsertAfter(o.prevCreatedAt, value)

root.RegisterElement(value)
Expand Down
7 changes: 6 additions & 1 deletion pkg/document/operations/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package operations

import (
"fmt"

"github.com/yorkie-team/yorkie/pkg/document/crdt"
"github.com/yorkie-team/yorkie/pkg/document/time"
)
Expand Down Expand Up @@ -61,7 +63,10 @@ func (o *Set) Execute(root *crdt.Root) error {
return ErrNotApplicableDataType
}

value := o.value.DeepCopy()
value, err := o.value.DeepCopy()
if err != nil {
return fmt.Errorf("set operations: %w", err)
}
removed := obj.Set(o.key, value)
root.RegisterElement(value)
if removed != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/rpc/auth/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func withExponentialBackoff(ctx context.Context, cfg *backend.Config, webhookFn

select {
case <-ctx.Done():
return ctx.Err()
return fmt.Errorf("%w", ctx.Err())
case <-time.After(waitBeforeRetry):
}

Expand Down