forked from jwang/mapreduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapreduce.go
43 lines (35 loc) · 844 Bytes
/
mapreduce.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
package mapreduce
import (
"flag"
"http"
"log"
"os"
"rpc"
)
var ErrDone = os.NewError("End of stream")
type Mapper func(work interface{}) (result interface{}, err os.Error)
type Reducer func(work chan<- interface{}, result <-chan interface{}) os.Error
var (
httpAddr = flag.String("http", ":0", "HTTP server listen address")
reducerAddr = flag.String("reducer", "", "reducer server URL")
isMapper = flag.Bool("map", false, "act as a mapper")
)
func Run(r Reducer, m Mapper) {
flag.Parse()
if *isMapper {
if *reducerAddr == "" {
flag.Usage()
os.Exit(2)
}
log.Println("Acting as a Mapper")
runMapper(*reducerAddr, m)
return
}
log.Println("Acting as Reducer")
rpc.RegisterName("Reducer", newReducer(r))
rpc.HandleHTTP()
err := http.ListenAndServe(*httpAddr, nil)
if err != nil {
log.Fatal(err)
}
}