-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathdataset_map.go
155 lines (140 loc) · 4.53 KB
/
dataset_map.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package flow
import (
"reflect"
)
// map can work with multiple kinds of inputs and outputs
// Input Types:
// 1. single value
// 2. (key, value) : Most common format for key value pair
// 3. (key, values) : GroupByKey() outputs
// 4. (key, values1, values2) : CoGroup() outputs
// 5. (key, value1, value2) : Join() outputs
// Output Types:
// 1. return single value
// 2. return (key, value)
// 3. return no value
// 4. return no value, but last parameter is a output channel
func (d *Dataset) Map(f interface{}) *Dataset {
outType := guessFunctionOutputType(f)
ret, step := add1ShardTo1Step(d, outType)
step.Name = "Map"
step.Function = func(task *Task) {
invokeMapFunc := _buildMapperFunction(f, task)
for input := range task.InputChan() {
invokeMapFunc(input)
}
// println("exiting d:", d.Id, "step:", step.Id, "task:", task.Id)
}
return ret
}
func _buildMapperFunction(f interface{}, task *Task) func(input reflect.Value) {
fn, ft := reflect.ValueOf(f), reflect.TypeOf(f)
var outChan reflect.Value
if ft.In(ft.NumIn()-1).Kind() == reflect.Chan || ft.NumOut() > 0 {
outChan = task.Outputs[0].WriteChan
}
if ft.In(ft.NumIn()-1).Kind() == reflect.Chan {
return _buildMapperFunctionWithChannel(fn, outChan)
}
return _buildMapperFunctionWithoutChannel(fn, outChan)
}
// if last parameter in the function is a channel
// use the channel element type as output type
func _buildMapperFunctionWithChannel(fn, outChan reflect.Value) func(input reflect.Value) {
return func(input reflect.Value) {
switch input.Type() {
case KeyValueType:
kv := input.Interface().(KeyValue)
_functionCallWithChanOutput(fn, outChan, kv.Key, kv.Value)
case KeyValueValueType:
kv := input.Interface().(KeyValueValue)
_functionCallWithChanOutput(fn, outChan, kv.Key, kv.Value1, kv.Value2)
case KeyValuesType:
kvs := input.Interface().(KeyValues)
_functionCallWithChanOutput(fn, outChan, kvs.Key, kvs.Values)
case KeyValuesValuesType:
kvv := input.Interface().(KeyValuesValues)
_functionCallWithChanOutput(fn, outChan, kvv.Key, kvv.Values1, kvv.Values2)
default:
fn.Call([]reflect.Value{input, outChan})
}
}
}
func _buildMapperFunctionWithoutChannel(fn, outChan reflect.Value) func(input reflect.Value) {
return func(input reflect.Value) {
outs := _functionCallBasedOnInputType(fn, input)
sendMapOutputs(outChan, outs)
}
}
func _functionCallWithChanOutput(fn reflect.Value, outChan reflect.Value, inputs ...interface{}) []reflect.Value {
var args []reflect.Value
for _, input := range inputs {
args = append(args, reflect.ValueOf(input))
}
args = append(args, outChan)
return fn.Call(args)
}
func _functionCall(fn reflect.Value, inputs ...interface{}) []reflect.Value {
var args []reflect.Value
for _, input := range inputs {
args = append(args, reflect.ValueOf(input))
}
return fn.Call(args)
}
func _functionCallBasedOnInputType(fn, input reflect.Value) (outs []reflect.Value) {
switch input.Type() {
case KeyValueType:
kv := input.Interface().(KeyValue)
outs = _functionCall(fn, kv.Key, kv.Value)
case KeyValueValueType:
kv := input.Interface().(KeyValueValue)
outs = _functionCall(fn, kv.Key, kv.Value1, kv.Value2)
case KeyValuesType:
kvs := input.Interface().(KeyValues)
outs = _functionCall(fn, kvs.Key, kvs.Values)
case KeyValuesValuesType:
kvv := input.Interface().(KeyValuesValues)
outs = _functionCall(fn, kvv.Key, kvv.Values1, kvv.Values2)
default:
outs = fn.Call([]reflect.Value{input})
}
return
}
// f(A)bool
func (d *Dataset) Filter(f interface{}) *Dataset {
ret, step := add1ShardTo1Step(d, d.Type)
ret.IsKeyPartitioned = d.IsKeyPartitioned
ret.IsKeyLocalSorted = d.IsKeyLocalSorted
step.Name = "Filter"
step.Function = func(task *Task) {
fn := reflect.ValueOf(f)
outChan := task.Outputs[0].WriteChan
for input := range task.InputChan() {
outs := _functionCallBasedOnInputType(fn, input)
if len(outs) > 0 && outs[0].Bool() {
outChan.Send(input)
}
}
}
return ret
}
func add1ShardTo1Step(d *Dataset, nextDataType reflect.Type) (ret *Dataset, step *Step) {
ret = d.context.newNextDataset(len(d.Shards), nextDataType)
step = d.context.AddOneToOneStep(d, ret)
return
}
// the value over the outChan is always reflect.Value
// but the inner values are always actual interface{} object
func sendMapOutputs(outChan reflect.Value, values []reflect.Value) {
if !outChan.IsValid() {
return
}
if len(values) == 2 {
outChan.Send(reflect.ValueOf(KeyValue{values[0].Interface(), values[1].Interface()}))
return
}
if len(values) == 1 {
outChan.Send(values[0])
return
}
}