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

Commit

Permalink
Merge pull request #1318 from lowzj/dfget-util
Browse files Browse the repository at this point in the history
refactor: move package dfget/util to pkg/algorithm
  • Loading branch information
starnop authored May 1, 2020
2 parents 6cc9d86 + ce8b258 commit ab71c0d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 127 deletions.
4 changes: 2 additions & 2 deletions cmd/dfdaemon/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/dragonflyoss/Dragonfly/dfdaemon/config"
"github.com/dragonflyoss/Dragonfly/dfdaemon/constant"
dfgetcfg "github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/util"
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
"github.com/dragonflyoss/Dragonfly/pkg/dflog"
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
"github.com/dragonflyoss/Dragonfly/pkg/fileutils"
Expand All @@ -50,7 +50,7 @@ func adjustSupernodeList(nodes []string) []string {
case 1:
return append(nodes, nodes[0])
default:
util.Shuffle(nodesLen, func(i, j int) {
algorithm.Shuffle(nodesLen, func(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
})
return append(nodes, nodes...)
Expand Down
4 changes: 2 additions & 2 deletions dfget/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
p2pDown "github.com/dragonflyoss/Dragonfly/dfget/core/downloader/p2p_downloader"
"github.com/dragonflyoss/Dragonfly/dfget/core/regist"
"github.com/dragonflyoss/Dragonfly/dfget/core/uploader"
"github.com/dragonflyoss/Dragonfly/dfget/util"
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
"github.com/dragonflyoss/Dragonfly/pkg/constants"
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
"github.com/dragonflyoss/Dragonfly/pkg/fileutils"
Expand Down Expand Up @@ -282,7 +282,7 @@ func adjustSupernodeList(nodes []string) []string {
case 1:
return append(nodes, nodes[0])
default:
util.Shuffle(nodesLen, func(i, j int) {
algorithm.Shuffle(nodesLen, func(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
})
return append(nodes, nodes...)
Expand Down
6 changes: 3 additions & 3 deletions dfget/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
. "github.com/dragonflyoss/Dragonfly/dfget/core/helper"
"github.com/dragonflyoss/Dragonfly/dfget/core/regist"
"github.com/dragonflyoss/Dragonfly/dfget/core/uploader"
"github.com/dragonflyoss/Dragonfly/dfget/util"
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"

"github.com/go-check/check"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -114,8 +114,8 @@ func (s *CoreTestSuite) TestAdjustSupernodeList(c *check.C) {
for _, v := range cases {
nodes := adjustSupernodeList(v)
for _, n := range v {
c.Assert(util.ContainsString(nodes[:len(v)], n), check.Equals, true)
c.Assert(util.ContainsString(nodes[len(v):], n), check.Equals, true)
c.Assert(algorithm.ContainsString(nodes[:len(v)], n), check.Equals, true)
c.Assert(algorithm.ContainsString(nodes[len(v):], n), check.Equals, true)
}
}
}
Expand Down
65 changes: 0 additions & 65 deletions dfget/util/algorithm.go

This file was deleted.

55 changes: 0 additions & 55 deletions dfget/util/algorithm_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions pkg/algorithm/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,59 @@

package algorithm

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// ContainsString returns whether the value is in arr.
func ContainsString(arr []string, value string) bool {
for _, v := range arr {
if v == value {
return true
}
}
return false
}

// Shuffle pseudo-randomizes the order of elements.
// n is the number of elements.
// swap swaps the elements with indexes i and j.
// copy from rand.Shuffle of go1.10.
func Shuffle(n int, swap func(int, int)) {
if n < 2 {
return
}
i := n - 1
for ; i > 1<<31-1-1; i-- {
j := int(rand.Int63n(int64(i + 1)))
swap(i, j)
}
for ; i > 0; i-- {
j := int(int31n(int32(i + 1)))
swap(i, j)
}
}

func int31n(n int32) int32 {
v := rand.Uint32()
prod := uint64(v) * uint64(n)
low := uint32(prod)
if low < uint32(n) {
thresh := uint32(-n) % uint32(n)
for low < thresh {
v = rand.Uint32()
prod = uint64(v) * uint64(n)
low = uint32(prod)
}
}
return int32(prod >> 32)
}

// GCDSlice returns the greatest common divisor of a slice.
// It returns 1 when s is empty because that any number divided by 1 is still
// itself.
Expand Down
24 changes: 24 additions & 0 deletions pkg/algorithm/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package algorithm

import (
"math/rand"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -66,3 +67,26 @@ func (suit *AlgorithmSuite) TestGCDSlice() {
suit.Equal(v.result, result)
}
}

func (suit *AlgorithmSuite) TestContainsString() {
suit.Equal(ContainsString(nil, "x"), false)
suit.Equal(ContainsString([]string{"x", "y"}, "x"), true)
suit.Equal(ContainsString([]string{"x", "y"}, "xx"), false)
}

func (suit *AlgorithmSuite) TestShuffle() {
// Check that Shuffle allows n=0 and n=1, but that swap is never called for them.
rand.Seed(1)
for n := 0; n <= 1; n++ {
Shuffle(n, func(i, j int) {
suit.Failf("swap called", "n=%d i=%d j=%d", n, i, j)
})
}

// Check that Shuffle calls swap n-1 times when n >= 2.
for n := 2; n <= 100; n++ {
isRun := 0
Shuffle(n, func(i, j int) { isRun++ })
suit.Equal(isRun, n-1)
}
}

0 comments on commit ab71c0d

Please sign in to comment.