Skip to content

Commit

Permalink
Clean up codes
Browse files Browse the repository at this point in the history
- Extract Paging
- Move project logic to projects
- Remove unused codes
  • Loading branch information
hackerwins committed May 4, 2022
1 parent 149693a commit a53bf0d
Show file tree
Hide file tree
Showing 22 changed files with 300 additions and 181 deletions.
11 changes: 7 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
linters-settings:
goimports:
local-prefixes: github.com/yorkie-team/yorkie

linters:
enable:
- bodyclose
Expand All @@ -16,5 +12,12 @@ linters:
- misspell
- nakedret

linters-settings:
goimports:
local-prefixes: github.com/yorkie-team/yorkie
gosec:
excludes:
- G107 # Potential HTTP request made with variable url

issues:
exclude-use-default: false
25 changes: 25 additions & 0 deletions api/types/paging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2022 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package types

// Paging is the paging information for the document.
type Paging struct {
PreviousID ID
PageSize int
IsForward bool
}
19 changes: 19 additions & 0 deletions api/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,22 @@ type Project struct {
// CreatedAt is the time when the project was created.
CreatedAt time.Time `json:"created_at"`
}

// RequireAuth returns whether the given method requires authorization.
func (p *Project) RequireAuth(method Method) bool {
if len(p.AuthWebhookURL) == 0 {
return false
}

if len(p.AuthWebhookMethods) == 0 {
return true
}

for _, m := range p.AuthWebhookMethods {
if Method(m) == method {
return true
}
}

return false
}
51 changes: 51 additions & 0 deletions api/types/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2022 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package types_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/yorkie-team/yorkie/api/types"
)

func TestProjectInfo(t *testing.T) {
t.Run("require auth test", func(t *testing.T) {
// 1. Specify which methods to allow
info := &types.Project{
AuthWebhookURL: "ValidWebhookURL",
AuthWebhookMethods: []string{string(types.ActivateClient)},
}
assert.True(t, info.RequireAuth(types.ActivateClient))
assert.False(t, info.RequireAuth(types.DetachDocument))

// 2. Allow all
info2 := &types.Project{
AuthWebhookURL: "ValidWebhookURL",
AuthWebhookMethods: []string{},
}
assert.True(t, info2.RequireAuth(types.ActivateClient))
assert.True(t, info2.RequireAuth(types.DetachDocument))

// 3. Empty webhook URL
info3 := &types.Project{
AuthWebhookURL: "",
}
assert.False(t, info3.RequireAuth(types.ActivateClient))
})
}
8 changes: 5 additions & 3 deletions server/admin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ func (s *Server) ListDocuments(
docs, err := documents.ListDocumentSummaries(
ctx,
s.backend,
types.ID(req.PreviousId),
int(req.PageSize),
req.IsForward,
types.Paging{
PreviousID: types.ID(req.PreviousId),
PageSize: int(req.PageSize),
IsForward: req.IsForward,
},
)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions server/backend/db/client_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type ClientInfo struct {
// ID is the unique ID of the client.
ID types.ID `bson:"_id"`

// ProjectID is the ID of the project the client belongs to.
ProjectID types.ID `bson:"project_id"`

// Key is the key of the client. It is used to identify the client by users.
Key string `bson:"key"`

Expand Down
8 changes: 3 additions & 5 deletions server/backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ type DB interface {
serverSeq uint64,
) error

// FindDocInfosByPreviousIDAndPageSize returns the documentInfos of the given previousID and pageSize.
FindDocInfosByPreviousIDAndPageSize(
// FindDocInfosByPaging returns the documentInfos of the given paging.
FindDocInfosByPaging(
ctx context.Context,
previousID types.ID,
pageSize int,
isForward bool,
paging types.Paging,
) ([]*DocInfo, error)
}
28 changes: 22 additions & 6 deletions server/backend/db/doc_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,29 @@ import (

// DocInfo is a structure representing information of the document.
type DocInfo struct {
ID types.ID `bson:"_id"`
Key key.Key `bson:"key"`
ServerSeq uint64 `bson:"server_seq"`
Owner types.ID `bson:"owner"`
CreatedAt time.Time `bson:"created_at"`
// ID is the unique ID of the document.
ID types.ID `bson:"_id"`

// ProjectID is the ID of the project that the document belongs to.
ProjectID types.ID `bson:"project_id"`

// Key is the key of the document.
Key key.Key `bson:"key"`

// ServerSeq is the sequence number of the last change of the document on the server.
ServerSeq uint64 `bson:"server_seq"`

// Owner is the owner(ID of the client) of the document.
Owner types.ID `bson:"owner"`

// CreatedAt is the time when the document is created.
CreatedAt time.Time `bson:"created_at"`

// AccessedAt is the time when the document is accessed.
AccessedAt time.Time `bson:"accessed_at"`
UpdatedAt time.Time `bson:"updated_at"`

// UpdatedAt is the time when the document is updated.
UpdatedAt time.Time `bson:"updated_at"`
}

// IncreaseServerSeq increases server sequence of the document.
Expand Down
20 changes: 9 additions & 11 deletions server/backend/db/memory/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,26 +655,24 @@ func (d *DB) UpdateSyncedSeq(
return nil
}

// FindDocInfosByPreviousIDAndPageSize returns the docInfos of the given previousID and pageSize.
func (d *DB) FindDocInfosByPreviousIDAndPageSize(
// FindDocInfosByPaging returns the documentInfos of the given paging.
func (d *DB) FindDocInfosByPaging(
ctx context.Context,
previousID types.ID,
pageSize int,
isForward bool,
paging types.Paging,
) ([]*db.DocInfo, error) {
txn := d.db.Txn(false)
defer txn.Abort()

var iterator memdb.ResultIterator
var err error
if isForward {
if paging.IsForward {
iterator, err = txn.LowerBound(
tblDocuments,
"id",
previousID.String(),
paging.PreviousID.String(),
)
} else {
if previousID == "" {
if paging.PreviousID == "" {
iterator, err = txn.GetReverse(
tblDocuments,
"id",
Expand All @@ -683,7 +681,7 @@ func (d *DB) FindDocInfosByPreviousIDAndPageSize(
iterator, err = txn.ReverseLowerBound(
tblDocuments,
"id",
previousID.String(),
paging.PreviousID.String(),
)
}
}
Expand All @@ -695,11 +693,11 @@ func (d *DB) FindDocInfosByPreviousIDAndPageSize(
var docInfos []*db.DocInfo
for raw := iterator.Next(); raw != nil; raw = iterator.Next() {
info := raw.(*db.DocInfo)
if len(docInfos) >= pageSize {
if len(docInfos) >= paging.PageSize {
break
}

if info.ID != previousID {
if info.ID != paging.PreviousID {
docInfos = append(docInfos, info)
}
}
Expand Down
24 changes: 19 additions & 5 deletions server/backend/db/memory/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,41 @@ func TestDB(t *testing.T) {
}

// initial page, previousID is empty
infos, err := localDB.FindDocInfosByPreviousIDAndPageSize(ctx, "", pageSize, false)
infos, err := localDB.FindDocInfosByPaging(ctx, types.Paging{PageSize: pageSize})
assert.NoError(t, err)
assertKeys([]key.Key{"8", "7", "6", "5", "4"}, infos)

// backward
infos, err = localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[len(infos)-1].ID, pageSize, false)
infos, err = localDB.FindDocInfosByPaging(ctx, types.Paging{
PreviousID: infos[len(infos)-1].ID,
PageSize: pageSize,
})
assert.NoError(t, err)
assertKeys([]key.Key{"3", "2", "1", "0"}, infos)

// backward again
emptyInfos, err := localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[len(infos)-1].ID, pageSize, false)
emptyInfos, err := localDB.FindDocInfosByPaging(ctx, types.Paging{
PreviousID: infos[len(infos)-1].ID,
PageSize: pageSize,
})
assert.NoError(t, err)
assertKeys(nil, emptyInfos)

// forward
infos, err = localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[0].ID, pageSize, true)
infos, err = localDB.FindDocInfosByPaging(ctx, types.Paging{
PreviousID: infos[0].ID,
PageSize: pageSize,
IsForward: true,
})
assert.NoError(t, err)
assertKeys([]key.Key{"4", "5", "6", "7", "8"}, infos)

// forward again
emptyInfos, err = localDB.FindDocInfosByPreviousIDAndPageSize(ctx, infos[len(infos)-1].ID, pageSize, true)
emptyInfos, err = localDB.FindDocInfosByPaging(ctx, types.Paging{
PreviousID: infos[len(infos)-1].ID,
PageSize: pageSize,
IsForward: true,
})
assert.NoError(t, err)
assertKeys(nil, emptyInfos)
})
Expand Down
24 changes: 11 additions & 13 deletions server/backend/db/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,31 +669,29 @@ func (c *Client) UpdateAndFindMinSyncedTicket(
), nil
}

// FindDocInfosByPreviousIDAndPageSize returns the docInfos of the given previousID and pageSize.
func (c *Client) FindDocInfosByPreviousIDAndPageSize(
// FindDocInfosByPaging returns the docInfos of the given paging.
func (c *Client) FindDocInfosByPaging(
ctx context.Context,
previousID types.ID,
pageSize int,
isForward bool,
paging types.Paging,
) ([]*db.DocInfo, error) {
filter := bson.M{}
if previousID != "" {
encodedPreviousID, err := encodeID(previousID)
if paging.PreviousID != "" {
encodedPreviousID, err := encodeID(paging.PreviousID)
if err != nil {
return nil, err
}

key := "$lt"
if isForward {
key = "$gt"
k := "$lt"
if paging.IsForward {
k = "$gt"
}
filter = bson.M{
"_id": bson.M{key: encodedPreviousID},
"_id": bson.M{k: encodedPreviousID},
}
}

opts := options.Find().SetLimit(int64(pageSize))
if !isForward {
opts := options.Find().SetLimit(int64(paging.PageSize))
if !paging.IsForward {
opts = opts.SetSort(map[string]int{"_id": -1})
}

Expand Down
19 changes: 0 additions & 19 deletions server/backend/db/project_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,6 @@ func (i *ProjectInfo) Validate() error {
return nil
}

// RequireAuth returns whether the given method requires authorization.
func (i *ProjectInfo) RequireAuth(method types.Method) bool {
if len(i.AuthWebhookURL) == 0 {
return false
}

if len(i.AuthWebhookMethods) == 0 {
return true
}

for _, m := range i.AuthWebhookMethods {
if types.Method(m) == method {
return true
}
}

return false
}

// ToProject converts the ProjectInfo to the Project.
func (i *ProjectInfo) ToProject() *types.Project {
return &types.Project{
Expand Down
Loading

0 comments on commit a53bf0d

Please sign in to comment.