/
main.go
60 lines (49 loc) · 1.25 KB
/
main.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
package main
import (
"container/list"
"fmt"
"os"
"strings"
"github.com/mateuszdyminski/mr/mapreduce"
)
func Map(value string) *list.List {
list := list.New()
lines := strings.Split(value, "\n")
for _, l := range lines {
// ommit empty lines
if l == "" {
continue
}
numbers := strings.Split(strings.TrimSpace(l), ",")
for _, n := range numbers {
list.PushFront(mapreduce.KeyValue{Key: n, Value: ""})
}
}
return list
}
func Reduce(key string, values *list.List) string {
counter := 0
for e := values.Front(); e != nil; e = e.Next() {
counter++
}
return fmt.Sprintf("%d", counter)
}
// Can be run in 3 ways:
// 1) Sequential (e.g., go run wc.go master x.txt sequential)
// 2) Master (e.g., go run wc.go master x.txt localhost:7777)
// 3) Worker (e.g., go run wc.go worker localhost:7777 localhost:7778 &)
func main() {
if len(os.Args) != 4 {
fmt.Printf("%s: see usage comments in file\n", os.Args[0])
} else if os.Args[1] == "master" {
if os.Args[3] == "sequential" {
mapreduce.RunSingle(5, 1, os.Args[2], Map, Reduce)
} else {
mr := mapreduce.MakeMapReduce(5, 1, os.Args[2], os.Args[3])
// Wait until MR is done
<-mr.DoneChannel
}
} else {
mapreduce.RunWorker(os.Args[2], os.Args[3], Map, Reduce, 100)
}
}