forked from apache/beam
/
gbk.go
145 lines (121 loc) · 4.16 KB
/
gbk.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package direct
import (
"bytes"
"context"
"fmt"
"github.com/apache/beam/sdks/go/pkg/beam/core/graph"
"github.com/apache/beam/sdks/go/pkg/beam/core/runtime/exec"
"github.com/apache/beam/sdks/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
)
type group struct {
key exec.FullValue
values [][]exec.FullValue
}
// CoGBK buffers all input and continues on FinishBundle. Use with small single-bundle data only.
type CoGBK struct {
UID exec.UnitID
Edge *graph.MultiEdge
Out exec.Node
enc exec.ElementEncoder // key encoder for coder-equality
wEnc exec.WindowEncoder // window encoder for windowing
m map[string]*group
}
func (n *CoGBK) ID() exec.UnitID {
return n.UID
}
func (n *CoGBK) Up(ctx context.Context) error {
n.enc = exec.MakeElementEncoder(n.Edge.Input[0].From.Coder.Components[0])
n.wEnc = exec.MakeWindowEncoder(n.Edge.Input[0].From.WindowingStrategy().Fn.Coder())
n.m = make(map[string]*group)
return nil
}
func (n *CoGBK) StartBundle(ctx context.Context, id string, data exec.DataContext) error {
return n.Out.StartBundle(ctx, id, data)
}
func (n *CoGBK) ProcessElement(ctx context.Context, elm *exec.FullValue, _ ...exec.ReStream) error {
index := elm.Elm.(int)
value := elm.Elm2.(*exec.FullValue)
for _, w := range elm.Windows {
ws := []typex.Window{w}
var buf bytes.Buffer
if err := n.enc.Encode(&exec.FullValue{Elm: value.Elm}, &buf); err != nil {
return errors.WithContextf(err, "encoding key %v for CoGBK", elm)
}
if err := n.wEnc.Encode(ws, &buf); err != nil {
return errors.WithContextf(err, "encoding window %v for CoGBK", w)
}
key := buf.String()
g, ok := n.m[key]
if !ok {
g = &group{
key: exec.FullValue{Elm: value.Elm, Timestamp: value.Timestamp, Windows: ws},
values: make([][]exec.FullValue, len(n.Edge.Input)),
}
n.m[key] = g
}
g.values[index] = append(g.values[index], exec.FullValue{Elm: value.Elm2, Timestamp: value.Timestamp})
}
return nil
}
func (n *CoGBK) FinishBundle(ctx context.Context) error {
for key, g := range n.m {
values := make([]exec.ReStream, len(g.values))
for i, list := range g.values {
values[i] = &exec.FixedReStream{Buf: list}
}
if err := n.Out.ProcessElement(ctx, &g.key, values...); err != nil {
return err
}
delete(n.m, key)
}
return n.Out.FinishBundle(ctx)
}
func (n *CoGBK) Down(ctx context.Context) error {
return nil
}
func (n *CoGBK) String() string {
return fmt.Sprintf("CoGBK. Out:%v", n.Out.ID())
}
// Inject injects the predecessor index into each FullValue, effectively
// converting KV<X,Y> into KV<X,KV<int,Y>>. Used to prime CoGBK.
type Inject struct {
UID exec.UnitID
N int
Out exec.Node
}
func (n *Inject) ID() exec.UnitID {
return n.UID
}
func (n *Inject) Up(ctx context.Context) error {
return nil
}
func (n *Inject) StartBundle(ctx context.Context, id string, data exec.DataContext) error {
return n.Out.StartBundle(ctx, id, data)
}
func (n *Inject) ProcessElement(ctx context.Context, elm *exec.FullValue, values ...exec.ReStream) error {
return n.Out.ProcessElement(ctx, &exec.FullValue{Elm: n.N, Elm2: elm, Timestamp: elm.Timestamp, Windows: elm.Windows}, values...)
}
func (n *Inject) FinishBundle(ctx context.Context) error {
return n.Out.FinishBundle(ctx)
}
func (n *Inject) Down(ctx context.Context) error {
return nil
}
func (n *Inject) String() string {
return fmt.Sprintf("Inject[%v]. Out:%v", n.N, n.Out.ID())
}