-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.go
62 lines (54 loc) · 1.04 KB
/
reducer.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
package mapreduce
import (
"log"
"os"
"sync"
)
type reducer struct {
mu sync.Mutex
mappers int
work chan interface{}
result chan interface{}
}
func newReducer(rFunc Reducer) *reducer {
r := &reducer{
work: make(chan interface{}),
result: make(chan interface{}),
}
go r.run(rFunc)
return r
}
func (r *reducer) run(rFunc Reducer) {
err := rFunc(r.work, r.result)
if err != nil {
log.Fatal("Reducer error: ", err)
}
log.Print("Reducer exited cleanly; shutting down")
os.Exit(0)
}
func (r *reducer) GetWork(args *struct{}, work *interface{}) os.Error {
if w, ok := <-r.work; ok {
*work = w
return nil
}
return ErrDone
}
func (r *reducer) SendResult(result *interface{}, reply *struct{}) os.Error {
r.result <- *result
return nil
}
func (r *reducer) Hello(args *struct{}, reply *struct{}) os.Error {
r.mu.Lock()
r.mappers++
r.mu.Unlock()
return nil
}
func (r *reducer) Goodbye(args *struct{}, reply *struct{}) os.Error {
r.mu.Lock()
r.mappers--
if r.mappers <= 0 {
close(r.result)
}
r.mu.Unlock()
return nil
}