-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatachan.go
261 lines (229 loc) · 6.56 KB
/
datachan.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Package Datachan provides a Map Reduce like programming environment. It
// takes care of type castings, chaining stages, launching workers,
// spilling data to disk and other repetitive and bored tasks.
//
// Unlike other Hadoop or Spark, Datachan works locally. Its main use case
// is local data processing, without using a lot of RAM and starting
// computations as soon as possible, without orchestration overhead.
//
// Partially based on https://gist.github.com/icecrime/67399480c9a10b48fadc .
package datachan
import (
"encoding/gob"
"fmt"
"reflect"
"sync"
)
func init() {
gob.Register(reflect.Value{})
}
type Stage struct {
output reflect.Value
}
func makeChannel(t reflect.Type, chanDir reflect.ChanDir, buffer int) reflect.Value {
ctype := reflect.ChanOf(chanDir, t)
return reflect.MakeChan(ctype, buffer)
}
func makeMap(key, elem reflect.Type) reflect.Value {
var mapType reflect.Type
mapType = reflect.MapOf(key, elem)
return reflect.MakeMap(mapType)
}
type T interface{}
// Source takes a channel of any kind and transmits it to the next stage
func Source(input T) *Stage {
value := reflect.ValueOf(input)
if value.Kind() != reflect.Chan {
panic("Source argument must be a channel")
}
etype := value.Type().Elem()
output := makeChannel(etype, reflect.BothDir, 0)
go func() {
for v, ok := value.Recv(); ok; v, ok = value.Recv() {
output.Send(v)
}
output.Close()
}()
return &Stage{output}
}
// Map takes a number of workers and a func(input T1, output chan<- T2), and applies that
// function to each element that comes from the previous stage.
// The function must emit the values through the output channel
func (s *Stage) Map(workers int, f T) *Stage {
value := reflect.ValueOf(f)
if value.Kind() != reflect.Func {
panic("Map argument must be a function")
}
// Checks that the map function accepts two arguments
ftype := value.Type()
if ftype.NumIn() != 2 {
panic("Map argument must be a function that receives two values")
}
// Checks constrains of output/emit channel
otype := ftype.In(1)
if otype.Kind() != reflect.Chan {
panic("Second argument of Map function must be a channel")
}
if otype.ChanDir() != reflect.SendDir {
panic("Second argument of Map function must be a send channel")
}
// Checks that the value received by Map function is the same as
// the value generated in previous stage
itype := ftype.In(0)
if itype != s.output.Type().Elem() {
panic(fmt.Sprintf("Input of Map stage function missmatch. Expected %v, found %v", s.output.Type().Elem(), itype))
}
output := makeChannel(otype.Elem(), reflect.BothDir, workers)
wg := sync.WaitGroup{}
var executor func()
executor = func() {
defer wg.Done()
i := 0
for e, ok := s.output.Recv(); ok; e, ok = s.output.Recv() {
value.Call([]reflect.Value{e, output})
i++
if i > 100 {
wg.Add(1)
go executor()
return
}
}
}
go func() {
for i := 0; i < workers; i++ {
wg.Add(1)
go executor()
}
wg.Wait()
output.Close()
}()
return &Stage{output}
}
// Sink returns a <-chan interface{} that can be used to retrieve the
// computation result, one record at a time.
func (s *Stage) Sink(bufferSize int) <-chan T {
output := make(chan T, bufferSize)
go func() {
for e, ok := s.output.Recv(); ok; e, ok = s.output.Recv() {
output <- e.Interface()
}
close(output)
}()
return output
}
// Tee duplicates the output of one stage into two stages.
func (s *Stage) Tee() (*Stage, *Stage) {
o1 := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
o2 := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
go func() {
for e, ok := s.output.Recv(); ok; e, ok = s.output.Recv() {
o1.Send(e)
o2.Send(e)
}
o1.Close()
o2.Close()
}()
return &Stage{o1}, &Stage{o2}
}
// Tee3 triplicates the output of one stage into two stages.
func (s *Stage) Tee3() (*Stage, *Stage, *Stage) {
o1 := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
o2 := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
o3 := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
go func() {
for e, ok := s.output.Recv(); ok; e, ok = s.output.Recv() {
o1.Send(e)
o2.Send(e)
o3.Send(e)
}
o1.Close()
o2.Close()
o3.Close()
}()
return &Stage{o1}, &Stage{o2}, &Stage{o3}
}
// Merge sends the output of several stages into a single one. The input stages
// must have the same output type as the previous one, that is the one that determines
// the stage output type.
func (s *Stage) Merge(stages ...*Stage) *Stage {
output := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
go func() {
cases := make([]reflect.SelectCase, 0, len(stages)+1)
for _, stage := range stages {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: stage.output,
})
}
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: s.output,
})
for len(cases) > 0 {
i, v, ok := reflect.Select(cases)
if ok {
output.Send(v)
} else {
cases = append(cases[:i], cases[i+1:]...)
}
}
output.Close()
}()
return &Stage{output}
}
// Filter takes a number of workers and a func(input T1) bool, and applies that
// function to each element that comes from the previous stage.
// If the function returns false the value is dropped
func (s *Stage) Filter(workers int, f T) *Stage {
value := reflect.ValueOf(f)
if value.Kind() != reflect.Func {
panic("Filter argument must be a function")
}
// Checks that the filter function accepts one arguments
ftype := value.Type()
if ftype.NumIn() != 1 {
panic("Filter argument must be a function that receives one values")
}
// Checks that the value received by Map function is the same as
// the value generated in previous stage
itype := ftype.In(0)
if itype != s.output.Type().Elem() {
panic(fmt.Sprintf("Input of Filter stage function missmatch. Expected %v, found %v", s.output.Type().Elem(), itype))
}
// Checks that return value is boolean
if ftype.NumOut() != 1 {
panic("Filter function must return exactly one boolean value")
}
otype := ftype.Out(0)
if otype.Kind() != reflect.Bool {
panic("Filter function output must be boolean")
}
output := reflect.MakeChan(s.output.Type(), ChanBufferDefault)
wg := sync.WaitGroup{}
var executor func()
executor = func() {
defer wg.Done()
i := 0
for e, ok := s.output.Recv(); ok; e, ok = s.output.Recv() {
preserve := value.Call([]reflect.Value{e})
if preserve[0].Bool() {
output.Send(e)
}
i++
if i > 10000 {
wg.Add(1)
go executor()
return
}
}
}
go func() {
for i := 0; i < workers; i++ {
wg.Add(1)
go executor()
}
wg.Wait()
output.Close()
}()
return &Stage{output}
}