Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit ab71c0d

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

File tree

Expand file treeCollapse file tree

7 files changed

+84
-127
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+84
-127
lines changed
Open diff view settings
Collapse file

‎cmd/dfdaemon/app/init.go‎

Copy file name to clipboardExpand all lines: cmd/dfdaemon/app/init.go
+2-2Lines 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...)
Collapse file

‎dfget/core/core.go‎

Copy file name to clipboardExpand all lines: dfget/core/core.go
+2-2Lines 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...)
Collapse file

‎dfget/core/core_test.go‎

Copy file name to clipboardExpand all lines: dfget/core/core_test.go
+3-3Lines 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
}
Collapse file

‎dfget/util/algorithm.go‎

Copy file name to clipboardExpand all lines: dfget/util/algorithm.go
-65Lines changed: 0 additions & 65 deletions
This file was deleted.
Collapse file

‎dfget/util/algorithm_test.go‎

Copy file name to clipboardExpand all lines: dfget/util/algorithm_test.go
-55Lines changed: 0 additions & 55 deletions
This file was deleted.
Collapse file

‎pkg/algorithm/algorithm.go‎

Copy file name to clipboardExpand all lines: pkg/algorithm/algorithm.go
+53Lines 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.
Collapse file

‎pkg/algorithm/algorithm_test.go‎

Copy file name to clipboardExpand all lines: pkg/algorithm/algorithm_test.go
+24Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.