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

Commit ab71c0d

Browse files
authored
Merge pull request #1318 from lowzj/dfget-util
refactor: move package dfget/util to pkg/algorithm
2 parents 6cc9d86 + ce8b258 commit ab71c0d

File tree

7 files changed

+84
-127
lines changed

7 files changed

+84
-127
lines changed

cmd/dfdaemon/app/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/dragonflyoss/Dragonfly/dfdaemon/config"
3030
"github.com/dragonflyoss/Dragonfly/dfdaemon/constant"
3131
dfgetcfg "github.com/dragonflyoss/Dragonfly/dfget/config"
32-
"github.com/dragonflyoss/Dragonfly/dfget/util"
32+
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
3333
"github.com/dragonflyoss/Dragonfly/pkg/dflog"
3434
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
3535
"github.com/dragonflyoss/Dragonfly/pkg/fileutils"
@@ -50,7 +50,7 @@ func adjustSupernodeList(nodes []string) []string {
5050
case 1:
5151
return append(nodes, nodes[0])
5252
default:
53-
util.Shuffle(nodesLen, func(i, j int) {
53+
algorithm.Shuffle(nodesLen, func(i, j int) {
5454
nodes[i], nodes[j] = nodes[j], nodes[i]
5555
})
5656
return append(nodes, nodes...)

dfget/core/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
p2pDown "github.com/dragonflyoss/Dragonfly/dfget/core/downloader/p2p_downloader"
3535
"github.com/dragonflyoss/Dragonfly/dfget/core/regist"
3636
"github.com/dragonflyoss/Dragonfly/dfget/core/uploader"
37-
"github.com/dragonflyoss/Dragonfly/dfget/util"
37+
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
3838
"github.com/dragonflyoss/Dragonfly/pkg/constants"
3939
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
4040
"github.com/dragonflyoss/Dragonfly/pkg/fileutils"
@@ -282,7 +282,7 @@ func adjustSupernodeList(nodes []string) []string {
282282
case 1:
283283
return append(nodes, nodes[0])
284284
default:
285-
util.Shuffle(nodesLen, func(i, j int) {
285+
algorithm.Shuffle(nodesLen, func(i, j int) {
286286
nodes[i], nodes[j] = nodes[j], nodes[i]
287287
})
288288
return append(nodes, nodes...)

dfget/core/core_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
. "github.com/dragonflyoss/Dragonfly/dfget/core/helper"
3434
"github.com/dragonflyoss/Dragonfly/dfget/core/regist"
3535
"github.com/dragonflyoss/Dragonfly/dfget/core/uploader"
36-
"github.com/dragonflyoss/Dragonfly/dfget/util"
36+
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
3737

3838
"github.com/go-check/check"
3939
"github.com/valyala/fasthttp"
@@ -114,8 +114,8 @@ func (s *CoreTestSuite) TestAdjustSupernodeList(c *check.C) {
114114
for _, v := range cases {
115115
nodes := adjustSupernodeList(v)
116116
for _, n := range v {
117-
c.Assert(util.ContainsString(nodes[:len(v)], n), check.Equals, true)
118-
c.Assert(util.ContainsString(nodes[len(v):], n), check.Equals, true)
117+
c.Assert(algorithm.ContainsString(nodes[:len(v)], n), check.Equals, true)
118+
c.Assert(algorithm.ContainsString(nodes[len(v):], n), check.Equals, true)
119119
}
120120
}
121121
}

dfget/util/algorithm.go

Lines changed: 0 additions & 65 deletions
This file was deleted.

dfget/util/algorithm_test.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

pkg/algorithm/algorithm.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@
1616

1717
package algorithm
1818

19+
import (
20+
"math/rand"
21+
"time"
22+
)
23+
24+
func init() {
25+
rand.Seed(time.Now().UnixNano())
26+
}
27+
28+
// ContainsString returns whether the value is in arr.
29+
func ContainsString(arr []string, value string) bool {
30+
for _, v := range arr {
31+
if v == value {
32+
return true
33+
}
34+
}
35+
return false
36+
}
37+
38+
// Shuffle pseudo-randomizes the order of elements.
39+
// n is the number of elements.
40+
// swap swaps the elements with indexes i and j.
41+
// copy from rand.Shuffle of go1.10.
42+
func Shuffle(n int, swap func(int, int)) {
43+
if n < 2 {
44+
return
45+
}
46+
i := n - 1
47+
for ; i > 1<<31-1-1; i-- {
48+
j := int(rand.Int63n(int64(i + 1)))
49+
swap(i, j)
50+
}
51+
for ; i > 0; i-- {
52+
j := int(int31n(int32(i + 1)))
53+
swap(i, j)
54+
}
55+
}
56+
57+
func int31n(n int32) int32 {
58+
v := rand.Uint32()
59+
prod := uint64(v) * uint64(n)
60+
low := uint32(prod)
61+
if low < uint32(n) {
62+
thresh := uint32(-n) % uint32(n)
63+
for low < thresh {
64+
v = rand.Uint32()
65+
prod = uint64(v) * uint64(n)
66+
low = uint32(prod)
67+
}
68+
}
69+
return int32(prod >> 32)
70+
}
71+
1972
// GCDSlice returns the greatest common divisor of a slice.
2073
// It returns 1 when s is empty because that any number divided by 1 is still
2174
// itself.

pkg/algorithm/algorithm_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package algorithm
1818

1919
import (
20+
"math/rand"
2021
"testing"
2122

2223
"github.com/stretchr/testify/suite"
@@ -66,3 +67,26 @@ func (suit *AlgorithmSuite) TestGCDSlice() {
6667
suit.Equal(v.result, result)
6768
}
6869
}
70+
71+
func (suit *AlgorithmSuite) TestContainsString() {
72+
suit.Equal(ContainsString(nil, "x"), false)
73+
suit.Equal(ContainsString([]string{"x", "y"}, "x"), true)
74+
suit.Equal(ContainsString([]string{"x", "y"}, "xx"), false)
75+
}
76+
77+
func (suit *AlgorithmSuite) TestShuffle() {
78+
// Check that Shuffle allows n=0 and n=1, but that swap is never called for them.
79+
rand.Seed(1)
80+
for n := 0; n <= 1; n++ {
81+
Shuffle(n, func(i, j int) {
82+
suit.Failf("swap called", "n=%d i=%d j=%d", n, i, j)
83+
})
84+
}
85+
86+
// Check that Shuffle calls swap n-1 times when n >= 2.
87+
for n := 2; n <= 100; n++ {
88+
isRun := 0
89+
Shuffle(n, func(i, j int) { isRun++ })
90+
suit.Equal(isRun, n-1)
91+
}
92+
}

0 commit comments

Comments
 (0)