-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
varint.go
132 lines (123 loc) · 3.77 KB
/
varint.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
// 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 coderx
import (
"encoding/binary"
"fmt"
"reflect"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/coder"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
)
// NewVarIntZ returns a varint coder for the given integer type. It uses a zig-zag scheme,
// which is _different_ from the Beam standard coding scheme.
func NewVarIntZ(t reflect.Type) (*coder.CustomCoder, error) {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return coder.NewCustomCoder("varintz", t, encVarIntZ, decVarIntZ)
default:
return nil, errors.Errorf("not a signed integer type: %v", t)
}
}
// NewVarUintZ returns a uvarint coder for the given integer type. It uses a zig-zag scheme,
// which is _different_ from the Beam standard coding scheme.
func NewVarUintZ(t reflect.Type) (*coder.CustomCoder, error) {
switch t.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return coder.NewCustomCoder("varuintz", t, encVarUintZ, decVarUintZ)
default:
return nil, errors.Errorf("not a unsigned integer type: %v", t)
}
}
func encVarIntZ(v typex.T) []byte {
var val int64
switch n := v.(type) {
case int:
val = int64(n)
case int8:
val = int64(n)
case int16:
val = int64(n)
case int32:
val = int64(n)
case int64:
val = n
default:
panic(fmt.Sprintf("received unknown value type: want a signed integer:, got %T", n))
}
ret := make([]byte, binary.MaxVarintLen64)
size := binary.PutVarint(ret, val)
return ret[:size]
}
func decVarIntZ(t reflect.Type, data []byte) (typex.T, error) {
n, size := binary.Varint(data)
if size <= 0 {
return nil, errors.Errorf("invalid varintz encoding for: %v", data)
}
switch t.Kind() {
case reflect.Int:
return int(n), nil
case reflect.Int8:
return int8(n), nil
case reflect.Int16:
return int16(n), nil
case reflect.Int32:
return int32(n), nil
case reflect.Int64:
return n, nil
default:
panic(fmt.Sprintf("unreachable statement: expected a signed integer, got %v", t))
}
}
func encVarUintZ(v typex.T) []byte {
var val uint64
switch n := v.(type) {
case uint:
val = uint64(n)
case uint8:
val = uint64(n)
case uint16:
val = uint64(n)
case uint32:
val = uint64(n)
case uint64:
val = n
default:
panic(fmt.Sprintf("received unknown value type: want an unsigned integer:, got %T", n))
}
ret := make([]byte, binary.MaxVarintLen64)
size := binary.PutUvarint(ret, val)
return ret[:size]
}
func decVarUintZ(t reflect.Type, data []byte) (typex.T, error) {
n, size := binary.Uvarint(data)
if size <= 0 {
return nil, errors.Errorf("invalid varuintz encoding for: %v", data)
}
switch t.Kind() {
case reflect.Uint:
return uint(n), nil
case reflect.Uint8:
return uint8(n), nil
case reflect.Uint16:
return uint16(n), nil
case reflect.Uint32:
return uint32(n), nil
case reflect.Uint64:
return n, nil
default:
panic(fmt.Sprintf("unreachable statement: expected an unsigned integer, got %v", t))
}
}