/
panes.go
120 lines (105 loc) · 3.24 KB
/
panes.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
// 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 coder
import (
"fmt"
"io"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/ioutilx"
)
// EncodePane encodes a typex.PaneInfo.
func EncodePane(v typex.PaneInfo, w io.Writer) error {
// Encoding: typex.PaneInfo
pane := byte(0)
if v.IsFirst {
pane |= 0x02
}
if v.IsLast {
pane |= 0x01
}
pane |= byte(v.Timing << 2)
switch {
case (v.Index == 0 && v.NonSpeculativeIndex == 0) || v.Timing == typex.PaneUnknown:
// The entire pane info is encoded as a single byte
paneByte := []byte{pane}
w.Write(paneByte)
case v.Index == v.NonSpeculativeIndex || v.Timing == typex.PaneEarly:
// The pane info is encoded as this byte plus a single VarInt encoded integer
if v.Timing == typex.PaneEarly && v.NonSpeculativeIndex != -1 {
return fmt.Errorf("error encoding pane %v: non-speculative index value must be equal to -1 if the pane timing is early", v)
}
paneByte := []byte{pane | 1<<4}
w.Write(paneByte)
EncodeVarInt(v.Index, w)
default:
// The pane info is encoded as this byte plus two VarInt encoded integer
paneByte := []byte{pane | 2<<4}
w.Write(paneByte)
EncodeVarInt(v.Index, w)
EncodeVarInt(v.NonSpeculativeIndex, w)
}
return nil
}
// NewPane initializes the PaneInfo from a given byte.
// By default, PaneInfo is assigned to NoFiringPane.
func NewPane(b byte) typex.PaneInfo {
pn := typex.NoFiringPane()
if !(b&0x02 == 2) {
pn.IsFirst = false
}
if !(b&0x01 == 1) {
pn.IsLast = false
}
pn.Timing = typex.PaneTiming((b >> 2) & 0x03)
return pn
}
// DecodePane decodes a single byte.
func DecodePane(r io.Reader) (typex.PaneInfo, error) {
// Decoding: typex.PaneInfo
var data [1]byte
if err := ioutilx.ReadNBufUnsafe(r, data[:]); err != nil {
return typex.PaneInfo{}, err
}
pn := NewPane(data[0] & 0x0f)
switch data[0] >> 4 {
case 0:
// Result encoded in only one pane.
return pn, nil
case 1:
// Result encoded in one pane plus a VarInt encoded integer.
data, err := DecodeVarInt(r)
if err != nil {
return typex.PaneInfo{}, err
}
pn.Index = data
if pn.Timing == typex.PaneEarly {
pn.NonSpeculativeIndex = -1
} else {
pn.NonSpeculativeIndex = pn.Index
}
case 2:
// Result encoded in one pane plus two VarInt encoded integer.
data, err := DecodeVarInt(r)
if err != nil {
return typex.PaneInfo{}, err
}
pn.Index = data
pn.NonSpeculativeIndex, err = DecodeVarInt(r)
if err != nil {
return typex.PaneInfo{}, err
}
}
return pn, nil
}