-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.go
More file actions
185 lines (171 loc) · 4.64 KB
/
Copy pathpipeline.go
File metadata and controls
185 lines (171 loc) · 4.64 KB
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
// Package pipeline is a staged streaming dataflow runtime: composable
// push-based operators (Map / Filter / FlatMap / Window / MapReduce) over typed
// channels, with bounded backpressure, plus a flow-based-programming (FBP)
// Component/Port/Network layer for declaratively-wired pipelines.
//
// It is the dataflow-operator-graph that Go channels + a worker pool do not
// provide ergonomically. Module path dev.helix.pipeline.
package pipeline
import (
"context"
"sync"
)
// Stream is a push-based source of T. Subscribe drives every element to sink
// until the source is exhausted, the context is cancelled, or sink returns an
// error. Subscribe blocks until the stream completes.
type Stream[T any] interface {
Subscribe(ctx context.Context, sink func(T) error) error
}
// Operator transforms a Stream[T] into a Stream[U] — a composable stage.
type Operator[T any, U any] func(in Stream[T]) Stream[U]
// streamFunc adapts a function to a Stream.
type streamFunc[T any] func(ctx context.Context, sink func(T) error) error
func (f streamFunc[T]) Subscribe(ctx context.Context, sink func(T) error) error {
return f(ctx, sink)
}
// FromSlice builds a bounded Stream from a slice.
func FromSlice[T any](items []T) Stream[T] {
return streamFunc[T](func(ctx context.Context, sink func(T) error) error {
for _, it := range items {
if err := ctx.Err(); err != nil {
return err
}
if err := sink(it); err != nil {
return err
}
}
return nil
})
}
// FromChannel builds a Stream from a receive channel.
func FromChannel[T any](ch <-chan T) Stream[T] {
return streamFunc[T](func(ctx context.Context, sink func(T) error) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case v, ok := <-ch:
if !ok {
return nil
}
if err := sink(v); err != nil {
return err
}
}
}
})
}
// Map applies fn to each element. An fn error aborts the stream.
func Map[T any, U any](fn func(T) (U, error)) Operator[T, U] {
return func(in Stream[T]) Stream[U] {
return streamFunc[U](func(ctx context.Context, sink func(U) error) error {
return in.Subscribe(ctx, func(t T) error {
u, err := fn(t)
if err != nil {
return err
}
return sink(u)
})
})
}
}
// Filter keeps elements for which pred is true.
func Filter[T any](pred func(T) bool) Operator[T, T] {
return func(in Stream[T]) Stream[T] {
return streamFunc[T](func(ctx context.Context, sink func(T) error) error {
return in.Subscribe(ctx, func(t T) error {
if pred(t) {
return sink(t)
}
return nil
})
})
}
}
// FlatMap expands each element into zero or more outputs.
func FlatMap[T any, U any](fn func(T) []U) Operator[T, U] {
return func(in Stream[T]) Stream[U] {
return streamFunc[U](func(ctx context.Context, sink func(U) error) error {
return in.Subscribe(ctx, func(t T) error {
for _, u := range fn(t) {
if err := sink(u); err != nil {
return err
}
}
return nil
})
})
}
}
// Window batches elements into fixed-size slices; a trailing partial window is
// emitted on completion.
func Window[T any](size int) Operator[T, []T] {
if size < 1 {
size = 1
}
return func(in Stream[T]) Stream[[]T] {
return streamFunc[[]T](func(ctx context.Context, sink func([]T) error) error {
buf := make([]T, 0, size)
err := in.Subscribe(ctx, func(t T) error {
buf = append(buf, t)
if len(buf) == size {
out := buf
buf = make([]T, 0, size)
return sink(out)
}
return nil
})
if err != nil {
return err
}
if len(buf) > 0 {
return sink(buf)
}
return nil
})
}
}
// Pipe2 composes two operators into one stage.
func Pipe2[A, B, C any](op1 Operator[A, B], op2 Operator[B, C]) Operator[A, C] {
return func(in Stream[A]) Stream[C] {
return op2(op1(in))
}
}
// Collect drains a Stream into a slice (terminal). Useful for tests + small
// terminal sinks.
func Collect[T any](ctx context.Context, s Stream[T]) ([]T, error) {
var out []T
var mu sync.Mutex
err := s.Subscribe(ctx, func(t T) error {
mu.Lock()
out = append(out, t)
mu.Unlock()
return nil
})
return out, err
}
// MapReduce partitions the stream by key, reduces each partition, and returns
// the per-key reduced values. It is terminal (drains the input stream).
func MapReduce[T any, K comparable, R any](
ctx context.Context,
in Stream[T],
mapper func(T) (K, R),
reducer func(acc R, next R) R,
) (map[K]R, error) {
acc := make(map[K]R)
seen := make(map[K]bool)
err := in.Subscribe(ctx, func(t T) error {
k, r := mapper(t)
if !seen[k] {
acc[k] = r
seen[k] = true
} else {
acc[k] = reducer(acc[k], r)
}
return nil
})
if err != nil {
return nil, err
}
return acc, nil
}