-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer.go
91 lines (69 loc) · 1.63 KB
/
balancer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package balancer
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/ilario-pierbattista/parallel-phpunit-executor/internal/common"
)
type chunkWithCounter struct {
common.Chunk
size int
}
// TODO a lot can be done to improve error handling. Must learn how.
func MakeChunks(rootPath string, batchSize int) (common.Chunks, error) {
var files []string
if batchSize < 1 {
return nil, fmt.Errorf("batchSize must be non-zero positive integer, %d given", batchSize)
}
info, err := os.Stat(rootPath)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, fmt.Errorf("%s is not a folder", rootPath)
}
err = filepath.WalkDir(
rootPath,
func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
info, err = os.Stat(filePath)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
files = append(files, filePath)
return nil
},
)
if err != nil {
return nil, err
}
return divide(files, batchSize), nil
}
func divide(files []string, batchSize int) common.Chunks {
chunks := common.Chunks{}
lastChunk := initChunkWithCounter()
for _, f := range files {
if lastChunk.size >= batchSize {
swapChunks(&chunks, &lastChunk)
}
lastChunk.Files = append(lastChunk.Files, f)
lastChunk.size++
}
if lastChunk.size > 0 {
swapChunks(&chunks, &lastChunk)
}
return chunks
}
func swapChunks(chunks *common.Chunks, currentChunk *chunkWithCounter) {
*chunks = append(*chunks, common.MakeChunk(¤tChunk.Files))
*currentChunk = initChunkWithCounter()
}
func initChunkWithCounter() chunkWithCounter {
return chunkWithCounter{size: 0}
}