-
Notifications
You must be signed in to change notification settings - Fork 0
/
dupdetect.go
71 lines (55 loc) · 1.89 KB
/
dupdetect.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
package main
import (
"fmt"
"os"
"strings"
)
// scan a set of files to make sure none of the codes are duplicated, either within the file
// or across the set of files
func main() {
fmt.Println("dupdetect")
files := os.Args[1:] // assume that every parameter is a filename
res := make(chan results) // either a count of codes processed or failure code
var workers []*worker
// build an array of workers, one per file
// each worker needs to know about all the workers created ahead of ti
for i, file := range files {
w := new(worker)
w.fname = file
w.outp = res
w.inp = make(chan request)
w.friends = workers[:i]
workers = append(workers, w)
}
// launch each of the workers
totalThreads := len(workers)
for i, w := range workers {
w.listenCount = len(workers) - (i + 1)
totalThreads += w.listenCount
go processFile(w)
}
// now wait for the result and print the results
// each file processed will result in two result packets
// the first is sent when the file has fully loaded and reports the number of codes loaded without a duplicate
// the second is number of codes checked for his "friend" workers
// if at anytime, the resutt comes back -1, a duplicate has been found and we should just stop
totalCodesSeen := 0
totalCodesCross := 0
for i := 0; i < totalThreads; i++ {
result := <-res
if result.codesSeen == -1 {
// time to go, print result and get out
fmt.Println(result.resultsDescription)
return
}
if strings.Compare(result.resultsDescription, loadOp) == 0 {
totalCodesSeen += result.codesSeen
fmt.Printf("Saw %d loaded\n", result.codesSeen)
}
if strings.Compare(result.resultsDescription, checkOp) == 0 {
totalCodesCross += result.codesSeen
fmt.Printf("Saw %d codes checked\n", result.codesSeen)
}
}
fmt.Printf("A total of %d codes checked, %d codes cross checked, no duplicates found.\n", totalCodesSeen, totalCodesCross)
}