Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
feature: implement the peer mgr
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Mar 25, 2019
1 parent 0ae9f70 commit 781c956
Show file tree
Hide file tree
Showing 16 changed files with 1,140 additions and 16 deletions.
5 changes: 4 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ definitions:
version:
type: "string"
description: "version number of dfget binary"

created:
type : "string"
format : "date-time"
description: "the time to join the P2P network"
TaskCreateRequest:
type: "object"
description: ""
Expand Down
21 changes: 21 additions & 0 deletions apis/types/peer_info.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion common/util/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"reflect"
"strconv"
"unicode"
)

// Max returns the larger of x or y.
Expand All @@ -42,7 +43,12 @@ func Min(x, y int32) int32 {

// IsEmptyStr returns whether the string s is empty.
func IsEmptyStr(s string) bool {
return s == ""
for _, v := range s {
if !unicode.IsSpace(v) {
return false
}
}
return true
}

// IsEmptySlice returns whether the slice values is empty.
Expand Down Expand Up @@ -73,6 +79,14 @@ func IsPositive(value int64) bool {
return value > 0
}

// IsNatural returns whether the value>=0.
func IsNatural(value string) bool {
if v, err := strconv.Atoi(value); err == nil {
return v >= 0
}
return false
}

// IsNumeric returns whether the value is a numeric.
// If the bitSize of value below 0 or above 64 an error is returned.
func IsNumeric(value string) bool {
Expand Down
8 changes: 8 additions & 0 deletions common/util/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func (suite *AssertSuite) TestMin(c *check.C) {

func (suite *AssertSuite) TestIsEmptyStr(c *check.C) {
c.Assert(IsEmptyStr(""), check.Equals, true)
c.Assert(IsEmptyStr(" "), check.Equals, true)
c.Assert(IsEmptyStr("\n "), check.Equals, true)
c.Assert(IsEmptyStr("x"), check.Equals, false)
}

Expand Down Expand Up @@ -77,6 +79,12 @@ func (suite *AssertSuite) TestIsPositive(c *check.C) {
c.Assert(IsPositive(-1), check.Equals, false)
}

func (suite *AssertSuite) TestIsNatural(c *check.C) {
c.Assert(IsNatural("0"), check.Equals, true)
c.Assert(IsNatural("1"), check.Equals, true)
c.Assert(IsNatural("-1"), check.Equals, false)
}

func (suite *AssertSuite) TestIsNumeric(c *check.C) {
c.Assert(IsNumeric("0"), check.Equals, true)
c.Assert(IsNumeric("1"), check.Equals, true)
Expand Down
1 change: 1 addition & 0 deletions supernode/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"github.com/dragonflyoss/Dragonfly/supernode/config"
"github.com/dragonflyoss/Dragonfly/supernode/server"

"github.com/sirupsen/logrus"
)

Expand Down
183 changes: 183 additions & 0 deletions supernode/daemon/mgr/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package mgr

import (
"net/http"
"sort"
"strconv"
"strings"

"github.com/dragonflyoss/Dragonfly/common/util"
errorType "github.com/dragonflyoss/Dragonfly/supernode/errors"

"github.com/pkg/errors"
)

const (
// PAGENUM identity the page number of the data.
// The default value is 0.
PAGENUM = "pageNum"

// PAGESIZE identity the page size of the data.
// If this value equals 0, return all values.
PAGESIZE = "pageSize"

// SORTKEY identity the sort key of the data.
// Each mgr needs to define acceptable values based on its own implementation.
SORTKEY = "sortKey"

// SORTDIRECT identity the sort direct of the data.
// The value can only be ASC or DESC.
SORTDIRECT = "sortDirect"

// ASCDIRECT means to sort the records in ascending order.
ASCDIRECT = "ASC"

// DESCDIRECT means to sort the records in descending order.
DESCDIRECT = "DESC"
)

var sortDirectMap = map[string]bool{
ASCDIRECT: true,
DESCDIRECT: true,
}

// PageFilter is a struct.
type PageFilter struct {
pageNum int
pageSize int
sortKey []string
sortDirect string
}

// ParseFilter gets filter params from request and returns a map[string][]string.
func ParseFilter(req *http.Request, sortKeyMap map[string]bool) (pageFilter *PageFilter, err error) {
v := req.URL.Query()
pageFilter = &PageFilter{}

// pageNum
pageNum, err := stoi(v.Get(PAGENUM))
if err != nil {
return nil, errors.Wrapf(errorType.ErrInvalidValue, "pageNum %s is not a number: %v", pageNum, err)
}
pageFilter.pageNum = pageNum

// pageSize
pageSize, err := stoi(v.Get(PAGESIZE))
if err != nil {
return nil, errors.Wrapf(errorType.ErrInvalidValue, "pageSize %s is not a number: %v", pageSize, err)
}
pageFilter.pageSize = pageSize

// sortDirect
sortDirect := v.Get(SORTDIRECT)
if sortDirect == "" {
sortDirect = ASCDIRECT
}
pageFilter.sortDirect = sortDirect

// sortKey
if sortKey, ok := v[SORTKEY]; ok {
pageFilter.sortKey = sortKey
}

err = validateFilter(pageFilter, sortKeyMap)
if err != nil {
return nil, err
}

return
}

func stoi(str string) (int, error) {
if util.IsEmptyStr(str) {
return 0, nil
}

result, err := strconv.Atoi(str)
if err != nil || result < 0 {
return -1, err
}
return result, nil
}

func validateFilter(pageFilter *PageFilter, sortKeyMap map[string]bool) error {
// pageNum
if pageFilter.pageNum < 0 {
return errors.Wrapf(errorType.ErrInvalidValue, "pageNum %s is not a natural number: %v", pageFilter.pageNum)
}

// pageSize
if pageFilter.pageSize < 0 {
return errors.Wrapf(errorType.ErrInvalidValue, "pageSize %s is not a natural number: %v", pageFilter.pageSize)
}

// sortDirect
if _, ok := sortDirectMap[strings.ToUpper(pageFilter.sortDirect)]; !ok {
return errors.Wrapf(errorType.ErrInvalidValue, "unexpected sortDirect %s", pageFilter.sortDirect)
}

// sortKey
if len(pageFilter.sortKey) == 0 || sortKeyMap == nil {
return nil
}
for _, value := range pageFilter.sortKey {
if v, ok := sortKeyMap[value]; !ok || !v {
return errors.Wrapf(errorType.ErrInvalidValue, "unexpected sortKey %s", value)
}
}

return nil
}

// getPageValues get some pages of metaSlice after ordering it.
// The less is a function that reports whether the element with
// index i should sort before the element with index j.
//
// Eg:
// people := []struct {
// Name string
// Age int
// }{
// {"Gopher", 7},
// {"Alice", 55},
// {"Vera", 24},
// {"Bob", 75},
// }
//
// If you want to sort it by age, and the less function should be defined as follows:
//
// less := func(i, j int) bool { return people[i].Age < people[j].Age }
func getPageValues(metaSlice []interface{}, pageNum, pageSize int,
less func(i, j int) bool) []interface{} {

if metaSlice == nil {
return nil
}
if less == nil {
return metaSlice
}

// sort the data slice
sort.Slice(metaSlice, less)

if pageSize == 0 {
return metaSlice
}

sliceLength := len(metaSlice)
start := pageNum * pageSize
end := (pageNum + 1) * pageSize

if sliceLength < start {
return nil
}
if sliceLength < end {
return metaSlice[start:sliceLength]
}
return metaSlice[start:end]
}

// isDESC returns whether the sortDirect is desc.
func isDESC(str string) bool {
return strings.ToUpper(str) == DESCDIRECT
}
Loading

0 comments on commit 781c956

Please sign in to comment.