Skip to content

Commit

Permalink
Clean up code (yorkie-team#595)
Browse files Browse the repository at this point in the history
- Remove unused functions and variables
- Remove redundant type casting
- Add comments to describe deprecated operation
  • Loading branch information
hackerwins authored and Wu22e committed Sep 3, 2023
1 parent 0c52862 commit a307f8d
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 101 deletions.
29 changes: 0 additions & 29 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/pkg/document/operations"
"github.com/yorkie-team/yorkie/pkg/document/time"
"github.com/yorkie-team/yorkie/server/backend/sync"
)

// FromUser converts the given Protobuf formats to model format.
Expand Down Expand Up @@ -192,16 +191,6 @@ func fromChangeID(id *api.ChangeID) (change.ID, error) {
), nil
}

// FromDocumentKey converts the given Protobuf formats to model format.
func FromDocumentKey(pbKey string) (key.Key, error) {
k := key.Key(pbKey)
if err := k.Validate(); err != nil {
return "", err
}

return k, nil
}

// FromDocumentID converts the given Protobuf formats to model format.
func FromDocumentID(pbID string) (types.ID, error) {
id := types.ID(pbID)
Expand All @@ -225,24 +214,6 @@ func FromEventType(pbDocEventType api.DocEventType) (types.DocEventType, error)
return "", fmt.Errorf("%v: %w", pbDocEventType, ErrUnsupportedEventType)
}

// FromDocEvent converts the given Protobuf formats to model format.
func FromDocEvent(docEvent *api.DocEvent) (*sync.DocEvent, error) {
client, err := time.ActorIDFromBytes(docEvent.Publisher)
if err != nil {
return nil, err
}

eventType, err := FromEventType(docEvent.Type)
if err != nil {
return nil, err
}

return &sync.DocEvent{
Type: eventType,
Publisher: client,
}, nil
}

// FromOperations converts the given Protobuf formats to model format.
func FromOperations(pbOps []*api.Operation) ([]operations.Operation, error) {
var ops []operations.Operation
Expand Down
14 changes: 0 additions & 14 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/yorkie-team/yorkie/pkg/document/innerpresence"
"github.com/yorkie-team/yorkie/pkg/document/operations"
"github.com/yorkie-team/yorkie/pkg/document/time"
"github.com/yorkie-team/yorkie/server/backend/sync"
)

// ToUser converts the given model format to Protobuf format.
Expand Down Expand Up @@ -214,19 +213,6 @@ func ToDocEventType(eventType types.DocEventType) (api.DocEventType, error) {
}
}

// ToDocEvent converts the given model to Protobuf format.
func ToDocEvent(docEvent sync.DocEvent) (*api.DocEvent, error) {
eventType, err := ToDocEventType(docEvent.Type)
if err != nil {
return nil, err
}

return &api.DocEvent{
Type: eventType,
Publisher: docEvent.Publisher.Bytes(),
}, nil
}

// ToOperations converts the given model format to Protobuf format.
func ToOperations(ops []operations.Operation) ([]*api.Operation, error) {
var pbOperations []*api.Operation
Expand Down
4 changes: 2 additions & 2 deletions api/types/change_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestChangeSummary(t *testing.T) {
PageSize: 3,
IsForward: true,
}, lastSeq)
assert.Equal(t, int64(lastSeq+1), from)
assert.Equal(t, int64(lastSeq+1), to)
assert.Equal(t, lastSeq+1, from)
assert.Equal(t, lastSeq+1, to)
})
}
4 changes: 4 additions & 0 deletions api/yorkie/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ message Operation {
TimeTicket executed_at = 6;
map<string, string> attributes = 7;
}
// NOTE(hackerwins): Select Operation is not used in the current version.
// In the previous version, it was used to represent selection of Text.
// However, it has been replaced by Presence now. It is retained for backward
// compatibility purposes.
message Select {
TimeTicket parent_created_at = 1;
TextNodePos from = 2;
Expand Down
6 changes: 0 additions & 6 deletions cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ var (
authWebhookCacheAuthTTL time.Duration
authWebhookCacheUnauthTTL time.Duration

etcdEndpoints []string
etcdDialTimeout time.Duration
etcdUsername string
etcdPassword string
etcdLockLeaseTime time.Duration

conf = server.NewConfig()
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/document/crdt/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ValueFromBytes(valueType ValueType, value []byte) interface{} {
return false
case Integer:
val := int32(binary.LittleEndian.Uint32(value))
return int32(val)
return val
case Long:
return int64(binary.LittleEndian.Uint64(value))
case Double:
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/crdt/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewTreeNode(pos *TreePos, nodeType string, attributes *RHT, value ...string

// Type returns the type of the Node.
func (n *TreeNode) Type() string {
return string(n.IndexTreeNode.Type)
return n.IndexTreeNode.Type
}

// Len returns the length of the Node.
Expand Down
20 changes: 0 additions & 20 deletions pkg/index/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,6 @@ func postorderTraversal[V Value](node *Node[V], callback func(node *Node[V], dep
callback(node, depth)
}

// postorderTraversalAll traverses the whole tree (include tombstones) with postorder traversal.
func postorderTraversalAll[V Value](node *Node[V], callback func(node *Node[V], depth int) error, depth int) error {
if node == nil {
return nil
}

for _, child := range node.children {
if err := postorderTraversalAll(child, callback, depth+1); err != nil {
return err
}
}

return callback(node, depth)
}

// nodesBetween iterates the nodes between the given range.
// If the given range is collapsed, the callback is not called.
// It traverses the tree with postorder traversal.
Expand Down Expand Up @@ -193,11 +178,6 @@ func Traverse[V Value](tree *Tree[V], callback func(node *Node[V], depth int)) {
postorderTraversal(tree.root, callback, 0)
}

// TraverseAll traverses the whole tree (include tombstones) with postorder traversal.
func TraverseAll[V Value](tree *Tree[V], callback func(node *Node[V], depth int) error) error {
return postorderTraversalAll(tree.root, callback, 0)
}

// Value represents the data stored in the nodes of Tree.
type Value interface {
IsRemoved() bool
Expand Down
3 changes: 0 additions & 3 deletions server/backend/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ var (
// ErrDocumentNotFound is returned when the document could not be found.
ErrDocumentNotFound = errors.New("document not found")

// ErrClientDocNotFound is returned when mapping between client and document could not be found.
ErrClientDocNotFound = errors.New("client document not found")

// ErrConflictOnUpdate is returned when a conflict occurs during update.
ErrConflictOnUpdate = errors.New("conflict on update")

Expand Down
16 changes: 0 additions & 16 deletions server/backend/database/project_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,6 @@ func NewProjectInfo(name string, owner types.ID, clientDeactivateThreshold strin
}
}

// ToProjectInfo converts the given types.Project to ProjectInfo.
func ToProjectInfo(project *types.Project) *ProjectInfo {
return &ProjectInfo{
ID: project.ID,
Name: project.Name,
Owner: project.Owner,
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
AuthWebhookURL: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
ClientDeactivateThreshold: project.ClientDeactivateThreshold,
CreatedAt: project.CreatedAt,
UpdatedAt: project.UpdatedAt,
}
}

// DeepCopy returns a deep copy of the ProjectInfo.
func (i *ProjectInfo) DeepCopy() *ProjectInfo {
if i == nil {
Expand Down
2 changes: 1 addition & 1 deletion server/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func SignUp(
return nil, fmt.Errorf("cannot hash password: %w", err)
}

info, err := be.DB.CreateUserInfo(ctx, username, string(hashed))
info, err := be.DB.CreateUserInfo(ctx, username, hashed)
if err != nil {
return nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions test/bench/grpc_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/yorkie-team/yorkie/client"
"github.com/yorkie-team/yorkie/pkg/document"
"github.com/yorkie-team/yorkie/pkg/document/json"
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/pkg/document/presence"
"github.com/yorkie-team/yorkie/server"
"github.com/yorkie-team/yorkie/server/backend/database"
Expand Down Expand Up @@ -105,7 +104,7 @@ func benchmarkUpdateProject(ctx context.Context, b *testing.B, cnt int, adminCli
for i := 0; i < cnt; i++ {
name := fmt.Sprintf("name%d", i)
authWebhookURL := fmt.Sprintf("http://authWebhookURL%d", i)
authWebhookMethods := []string{}
var authWebhookMethods []string
for _, m := range types.AuthMethods() {
authWebhookMethods = append(authWebhookMethods, string(m))
}
Expand Down Expand Up @@ -203,7 +202,7 @@ func BenchmarkRPC(b *testing.B) {

ctx := context.Background()

d1 := document.New(key.Key(helper.TestDocKey(b)))
d1 := document.New(helper.TestDocKey(b))
err := c1.Attach(ctx, d1)
assert.NoError(b, err)
testKey1 := "testKey1"
Expand All @@ -213,7 +212,7 @@ func BenchmarkRPC(b *testing.B) {
})
assert.NoError(b, err)

d2 := document.New(key.Key(helper.TestDocKey(b)))
d2 := document.New(helper.TestDocKey(b))
err = c2.Attach(ctx, d2)
assert.NoError(b, err)
testKey2 := "testKey2"
Expand Down Expand Up @@ -268,8 +267,8 @@ func BenchmarkRPC(b *testing.B) {
defer cleanupClients(b, clients)

ctx := context.Background()
doc1 := document.New(key.Key(helper.TestDocKey(b)))
doc2 := document.New(key.Key(helper.TestDocKey(b)))
doc1 := document.New(helper.TestDocKey(b))
doc2 := document.New(helper.TestDocKey(b))

err := doc1.Update(func(root *json.Object, p *presence.Presence) error {
text := root.SetNewText("k1")
Expand Down
3 changes: 1 addition & 2 deletions test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/yorkie-team/yorkie/pkg/document"
"github.com/yorkie-team/yorkie/pkg/document/innerpresence"
"github.com/yorkie-team/yorkie/pkg/document/json"
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/pkg/document/presence"
"github.com/yorkie-team/yorkie/test/helper"
)
Expand Down Expand Up @@ -68,7 +67,7 @@ func TestDocument(t *testing.T) {
assert.True(t, doc2.IsAttached())
assert.Equal(t, `{"k1":"v2"}`, doc2.Marshal())

doc3 := document.New(key.Key("invalid$key"))
doc3 := document.New("invalid$key")
err = c1.Attach(ctx, doc3)
assert.Error(t, err)
})
Expand Down

0 comments on commit a307f8d

Please sign in to comment.