-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-find-md5-sum-of-file-then-rename-it-concurrently.go
148 lines (118 loc) · 2.93 KB
/
3-find-md5-sum-of-file-then-rename-it-concurrently.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"crypto/md5"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"time"
)
var tempPath = filepath.Join(os.Getenv("TEMP"), "temp-concurrency-pipeline")
type FileInfo struct {
FilePath string // file location
Content []byte // file content
Sum string // md5 sum of content
IsRenamed bool // indicator whether the particular file is renamed already or not
}
func main() {
log.Println("start")
start := time.Now()
// pipeline 1: loop all files and read it
chanFileContent := readFiles()
// pipeline 2: calculate md5sum
chanFileSum1 := getSum(chanFileContent)
chanFileSum2 := getSum(chanFileContent)
chanFileSum3 := getSum(chanFileContent)
chanFileSum := mergeChanFileInfo(chanFileSum1, chanFileSum2, chanFileSum3)
// pipeline 3: rename files
chanRename1 := rename(chanFileSum)
chanRename2 := rename(chanFileSum)
chanRename3 := rename(chanFileSum)
chanRename4 := rename(chanFileSum)
chanRename := mergeChanFileInfo(chanRename1, chanRename2, chanRename3, chanRename4)
// print output
counterRenamed := 0
counterTotal := 0
for fileInfo := range chanRename {
if fileInfo.IsRenamed {
counterRenamed++
}
counterTotal++
}
log.Printf("%d/%d files renamed", counterRenamed, counterTotal)
duration := time.Since(start)
log.Println("done in", duration.Seconds(), "seconds")
}
func readFiles() <-chan FileInfo {
chanOut := make(chan FileInfo)
go func() {
err := filepath.Walk(tempPath, func(path string, info os.FileInfo, err error) error {
// if there is an error, return immediatelly
if err != nil {
return err
}
// if it is a sub directory, return immediatelly
if info.IsDir() {
return nil
}
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
chanOut <- FileInfo{
FilePath: path,
Content: buf,
}
return nil
})
if err != nil {
log.Println("ERROR:", err.Error())
}
close(chanOut)
}()
return chanOut
}
func getSum(chanIn <-chan FileInfo) <-chan FileInfo {
chanOut := make(chan FileInfo)
go func() {
for fileInfo := range chanIn {
fileInfo.Sum = fmt.Sprintf("%x", md5.Sum(fileInfo.Content))
chanOut <- fileInfo
}
close(chanOut)
}()
return chanOut
}
func rename(chanIn <-chan FileInfo) <-chan FileInfo {
chanOut := make(chan FileInfo)
go func() {
for fileInfo := range chanIn {
newPath := filepath.Join(tempPath, fmt.Sprintf("file-%s.txt", fileInfo.Sum))
err := os.Rename(fileInfo.FilePath, newPath)
fileInfo.IsRenamed = err == nil
chanOut <- fileInfo
}
close(chanOut)
}()
return chanOut
}
func mergeChanFileInfo(chanInMany ...<-chan FileInfo) <-chan FileInfo {
wg := new(sync.WaitGroup)
chanOut := make(chan FileInfo)
wg.Add(len(chanInMany))
for _, eachChan := range chanInMany {
go func(eachChan <-chan FileInfo) {
for eachChanData := range eachChan {
chanOut <- eachChanData
}
wg.Done()
}(eachChan)
}
go func() {
wg.Wait()
close(chanOut)
}()
return chanOut
}