forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_skip.go
225 lines (168 loc) · 3.84 KB
/
codec_skip.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
package avro
import (
"fmt"
"unsafe"
)
func createSkipDecoder(schema Schema) ValDecoder {
switch schema.Type() {
case Boolean:
return &boolSkipDecoder{}
case Int:
return &intSkipDecoder{}
case Long:
return &longSkipDecoder{}
case Float:
return &floatSkipDecoder{}
case Double:
return &doubleSkipDecoder{}
case String:
return &stringSkipDecoder{}
case Bytes:
return &bytesSkipDecoder{}
case Record:
return skipDecoderOfRecord(schema)
case Ref:
return createSkipDecoder(schema.(*RefSchema).Schema())
case Enum:
return &enumSkipDecoder{symbols: schema.(*EnumSchema).Symbols()}
case Array:
return skipDecoderOfArray(schema)
case Map:
return skipDecoderOfMap(schema)
case Union:
return skipDecoderOfUnion(schema)
case Fixed:
return &fixedSkipDecoder{size: schema.(*FixedSchema).Size()}
default:
return &errorDecoder{err: fmt.Errorf("avro: schema type %s is unsupported", schema.Type())}
}
}
type boolSkipDecoder struct{}
func (*boolSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipBool()
}
type intSkipDecoder struct{}
func (*intSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipInt()
}
type longSkipDecoder struct{}
func (*longSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipLong()
}
type floatSkipDecoder struct{}
func (*floatSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipFloat()
}
type doubleSkipDecoder struct{}
func (*doubleSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipDouble()
}
type stringSkipDecoder struct{}
func (*stringSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipString()
}
type bytesSkipDecoder struct{}
func (c *bytesSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipBytes()
}
func skipDecoderOfRecord(schema Schema) ValDecoder {
rec := schema.(*RecordSchema)
decoders := make([]ValDecoder, len(rec.Fields()))
for i, field := range rec.Fields() {
decoders[i] = createSkipDecoder(field.Type())
}
return &recordSkipDecoder{
decoders: decoders,
}
}
type recordSkipDecoder struct {
decoders []ValDecoder
}
func (d *recordSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
for _, decoder := range d.decoders {
decoder.Decode(nil, r)
}
}
type enumSkipDecoder struct {
symbols []string
}
func (c *enumSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipInt()
}
func skipDecoderOfArray(schema Schema) ValDecoder {
arr := schema.(*ArraySchema)
decoder := createSkipDecoder(arr.Items())
return &sliceSkipDecoder{
decoder: decoder,
}
}
type sliceSkipDecoder struct {
decoder ValDecoder
}
func (d *sliceSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
for {
l, size := r.ReadBlockHeader()
if l == 0 {
break
}
if size > 0 {
r.SkipNBytes(int(size))
continue
}
for i := 0; i < int(l); i++ {
d.decoder.Decode(nil, r)
}
}
}
func skipDecoderOfMap(schema Schema) ValDecoder {
m := schema.(*MapSchema)
decoder := createSkipDecoder(m.Values())
return &mapSkipDecoder{
decoder: decoder,
}
}
type mapSkipDecoder struct {
decoder ValDecoder
}
func (d *mapSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
for {
l, size := r.ReadBlockHeader()
if l == 0 {
break
}
if size > 0 {
r.SkipNBytes(int(size))
continue
}
for i := 0; i < int(l); i++ {
r.SkipString()
d.decoder.Decode(nil, r)
}
}
}
func skipDecoderOfUnion(schema Schema) ValDecoder {
union := schema.(*UnionSchema)
return &unionSkipDecoder{
schema: union,
}
}
type unionSkipDecoder struct {
schema *UnionSchema
}
func (d *unionSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
_, resSchema := getUnionSchema(d.schema, r)
if resSchema == nil {
return
}
// In a null case, just return
if resSchema.Type() == Null {
return
}
createSkipDecoder(resSchema).Decode(nil, r)
}
type fixedSkipDecoder struct {
size int
}
func (d *fixedSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
r.SkipNBytes(d.size)
}