This repository was archived by the owner on Dec 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +84
-127
lines changed Open diff view settings
Expand file tree Collapse file tree 7 files changed +84
-127
lines changed Open diff view settings
Original file line number Diff line number Diff 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 ... )
Original file line number Diff line number Diff 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 ... )
Original file line number Diff line number Diff 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}
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1616
1717package 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.
Original file line number Diff line number Diff line change 1717package algorithm
1818
1919import (
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+ }
You can’t perform that action at this time.
0 commit comments