-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathmaster_splitmerge.go
72 lines (67 loc) · 1.41 KB
/
master_splitmerge.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
package mapreduce
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"sort"
)
// merge combines the results of the many reduce jobs into a single output file
// XXX use merge sort
func (mr *Master) merge() {
debug("Merge phase")
kvs := make(map[string]string)
for i := 0; i < mr.nReduce; i++ {
p := mergeName(mr.jobName, i)
fmt.Printf("Merge: read %s\n", p)
file, err := os.Open(p)
if err != nil {
log.Fatal("Merge: ", err)
}
dec := json.NewDecoder(file)
for {
var kv KeyValue
err = dec.Decode(&kv)
if err != nil {
break
}
kvs[kv.Key] = kv.Value
}
file.Close()
}
var keys []string
for k := range kvs {
keys = append(keys, k)
}
sort.Strings(keys)
file, err := os.Create("mrtmp." + mr.jobName)
if err != nil {
log.Fatal("Merge: create ", err)
}
w := bufio.NewWriter(file)
for _, k := range keys {
fmt.Fprintf(w, "%s: %s\n", k, kvs[k])
}
w.Flush()
file.Close()
}
// removeFile is a simple wrapper around os.Remove that logs errors.
func removeFile(n string) {
err := os.Remove(n)
if err != nil {
log.Fatal("CleanupFiles ", err)
}
}
// CleanupFiles removes all intermediate files produced by running mapreduce.
func (mr *Master) CleanupFiles() {
for i := range mr.files {
for j := 0; j < mr.nReduce; j++ {
removeFile(reduceName(mr.jobName, i, j))
}
}
for i := 0; i < mr.nReduce; i++ {
removeFile(mergeName(mr.jobName, i))
}
removeFile("mrtmp." + mr.jobName)
}