-
Notifications
You must be signed in to change notification settings - Fork 0
/
nocrash.go
47 lines (39 loc) · 948 Bytes
/
nocrash.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
package main
//
// same as crash.go but doesn't actually crash.
//
// go build -buildmode=plugin nocrash.go
//
import "../mr"
import crand "crypto/rand"
import "math/big"
import "strings"
import "os"
import "sort"
import "strconv"
func maybeCrash() {
max := big.NewInt(1000)
rr, _ := crand.Int(crand.Reader, max)
if false && rr.Int64() < 500 {
// crash!
os.Exit(1)
}
}
func Map(filename string, contents string) []mr.KeyValue {
maybeCrash()
kva := []mr.KeyValue{}
kva = append(kva, mr.KeyValue{"a", filename})
kva = append(kva, mr.KeyValue{"b", strconv.Itoa(len(filename))})
kva = append(kva, mr.KeyValue{"c", strconv.Itoa(len(contents))})
kva = append(kva, mr.KeyValue{"d", "xyzzy"})
return kva
}
func Reduce(key string, values []string) string {
maybeCrash()
// sort values to ensure deterministic output.
vv := make([]string, len(values))
copy(vv, values)
sort.Strings(vv)
val := strings.Join(vv, " ")
return val
}