-
Notifications
You must be signed in to change notification settings - Fork 18
/
stream.go
504 lines (449 loc) · 15.9 KB
/
stream.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package molecule
import (
"bytes"
"io"
"math"
"github.com/richardartoul/molecule/src/protowire"
)
const (
// The defaultBufferSize is the default size for buffers used for embedded
// values, which must first be written to a buffer to determine their
// length. This is not used if BufferFactory is set.
defaultBufferSize int = 1024 * 8
)
// A ProtoStream supports writing protobuf data in a streaming fashion. Its methods
// will write their output to the wrapped `io.Writer`. Zero values are not included.
//
// ProtoStream instances are *not* threadsafe and *not* re-entrant.
type ProtoStream struct {
// The outputWriter is the writer to which the protobuf-encoded bytes are
// written.
outputWriter io.Writer
// The scratchBuffer is a buffer used and re-used for generating output.
// Each method should begin by resetting this buffer.
scratchBuffer []byte
// The scratchArray is a second, very small array used for packed
// encodings. It is large enough to fit two max-size varints (10 bytes
// each) without reallocation
scratchArray [20]byte
// The childStream is a ProtoStream used to implement `Embedded`, and
// reused for multiple calls.
childStream *ProtoStream
// The childBuffer is the buffer to which `childStream` writes.
childBuffer *bytes.Buffer
// The BufferFactory creates new, empty buffers as needed. Users may
// override this function to provide pre-initialized buffers of a larger
// size, or from a buffer pool, for example.
BufferFactory func() []byte
}
// NewProtoStream creates a new ProtoStream writing to the given Writer. If the
// writer is nil, the stream cannot be used until it has been set with `Reset`.
func NewProtoStream(outputWriter io.Writer) *ProtoStream {
return &ProtoStream{
scratchBuffer: make([]byte, 0, defaultBufferSize),
childStream: nil,
childBuffer: nil,
outputWriter: outputWriter,
BufferFactory: func() []byte { return make([]byte, 0, defaultBufferSize) },
}
}
// Reset sets the Writer to which this ProtoStream streams. If the writer is nil,
// then the protostream cannot be used until Reset is called with a non-nil value.
func (ps *ProtoStream) Reset(outputWriter io.Writer) {
ps.outputWriter = outputWriter
ps.scratchBuffer = ps.scratchBuffer[:0]
}
// Double writes a value of proto type double to the stream.
func (ps *ProtoStream) Double(fieldNumber int, value float64) error {
if value == 0.0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.Fixed64Type)
ps.scratchBuffer = protowire.AppendFixed64(ps.scratchBuffer, math.Float64bits(value))
return ps.writeScratch()
}
// DoublePacked writes a slice of values of proto type double to the stream,
// in packed form.
func (ps *ProtoStream) DoublePacked(fieldNumber int, values []float64) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendFixed64(ps.scratchBuffer, math.Float64bits(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Float writes a value of proto type double to the stream.
func (ps *ProtoStream) Float(fieldNumber int, value float32) error {
if value == 0.0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.Fixed32Type)
ps.scratchBuffer = protowire.AppendFixed32(ps.scratchBuffer, math.Float32bits(value))
return ps.writeScratch()
}
// FloatPacked writes a slice of values of proto type float to the stream,
// in packed form.
func (ps *ProtoStream) FloatPacked(fieldNumber int, values []float32) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendFixed32(ps.scratchBuffer, math.Float32bits(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Int32 writes a value of proto type int32 to the stream.
func (ps *ProtoStream) Int32(fieldNumber int, value int32) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(value))
return ps.writeScratch()
}
// Int32Packed writes a slice of values of proto type int32 to the stream,
// in packed form.
func (ps *ProtoStream) Int32Packed(fieldNumber int, values []int32) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Int64 writes a value of proto type int64 to the stream.
func (ps *ProtoStream) Int64(fieldNumber int, value int64) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(value))
return ps.writeScratch()
}
// Int64Packed writes a slice of values of proto type int64 to the stream,
// in packed form.
func (ps *ProtoStream) Int64Packed(fieldNumber int, values []int64) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Uint32 writes a value of proto type uint32 to the stream.
func (ps *ProtoStream) Uint32(fieldNumber int, value uint32) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(value))
return ps.writeScratch()
}
// Uint32Packed writes a slice of values of proto type uint32 to the stream,
// in packed form.
func (ps *ProtoStream) Uint32Packed(fieldNumber int, values []uint32) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Uint64 writes a value of proto type uint64 to the stream.
func (ps *ProtoStream) Uint64(fieldNumber int, value uint64) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, value)
return ps.writeScratch()
}
// Uint64Packed writes a slice of values of proto type uint64 to the stream,
// in packed form.
func (ps *ProtoStream) Uint64Packed(fieldNumber int, values []uint64) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, value)
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Sint32 writes a value of proto type sint32 to the stream.
func (ps *ProtoStream) Sint32(fieldNumber int, value int32) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, protowire.EncodeZigZag(int64(value)))
return ps.writeScratch()
}
// Sint32Packed writes a slice of values of proto type sint32 to the stream,
// in packed form.
func (ps *ProtoStream) Sint32Packed(fieldNumber int, values []int32) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, protowire.EncodeZigZag(int64(value)))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Sint64 writes a value of proto type sint64 to the stream.
func (ps *ProtoStream) Sint64(fieldNumber int, value int64) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, protowire.EncodeZigZag(value))
return ps.writeScratch()
}
// Sint64Packed writes a slice of values of proto type sint64 to the stream,
// in packed form.
func (ps *ProtoStream) Sint64Packed(fieldNumber int, values []int64) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, protowire.EncodeZigZag(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Fixed32 writes a value of proto type fixed32 to the stream.
func (ps *ProtoStream) Fixed32(fieldNumber int, value uint32) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.Fixed32Type)
ps.scratchBuffer = protowire.AppendFixed32(ps.scratchBuffer, value)
return ps.writeScratch()
}
// Fixed32Packed writes a slice of values of proto type fixed32 to the stream,
// in packed form.
func (ps *ProtoStream) Fixed32Packed(fieldNumber int, values []uint32) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendFixed32(ps.scratchBuffer, value)
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Fixed64 writes a value of proto type fixed64 to the stream.
func (ps *ProtoStream) Fixed64(fieldNumber int, value uint64) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.Fixed64Type)
ps.scratchBuffer = protowire.AppendFixed64(ps.scratchBuffer, value)
return ps.writeScratch()
}
// Fixed64Packed writes a slice of values of proto type fixed64 to the stream,
// in packed form.
func (ps *ProtoStream) Fixed64Packed(fieldNumber int, values []uint64) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendFixed64(ps.scratchBuffer, value)
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Sfixed32 writes a value of proto type sfixed32 to the stream.
func (ps *ProtoStream) Sfixed32(fieldNumber int, value int32) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.Fixed32Type)
ps.scratchBuffer = protowire.AppendFixed32(ps.scratchBuffer, uint32(value))
return ps.writeScratch()
}
// Sfixed32Packed writes a slice of values of proto type sfixed32 to the stream,
// in packed form.
func (ps *ProtoStream) Sfixed32Packed(fieldNumber int, values []int32) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendFixed32(ps.scratchBuffer, uint32(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Sfixed64 writes a value of proto type sfixed64 to the stream.
func (ps *ProtoStream) Sfixed64(fieldNumber int, value int64) error {
if value == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.Fixed64Type)
ps.scratchBuffer = protowire.AppendFixed64(ps.scratchBuffer, uint64(value))
return ps.writeScratch()
}
// Sfixed64Packed writes a slice of values of proto type sfixed64 to the stream,
// in packed form.
func (ps *ProtoStream) Sfixed64Packed(fieldNumber int, values []int64) error {
if len(values) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
for _, value := range values {
ps.scratchBuffer = protowire.AppendFixed64(ps.scratchBuffer, uint64(value))
}
return ps.writeScratchAsPacked(fieldNumber)
}
// Bool writes a value of proto type bool to the stream.
func (ps *ProtoStream) Bool(fieldNumber int, value bool) error {
if value == false {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.VarintType)
var bit uint64
if value {
bit = 1
}
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, bit)
return ps.writeScratch()
}
// String writes a string to the stream.
func (ps *ProtoStream) String(fieldNumber int, value string) error {
if len(value) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.BytesType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(len(value)))
err := ps.writeScratch()
if err != nil {
return err
}
return ps.writeAllString(value)
}
// Bytes writes the given bytes to the stream.
func (ps *ProtoStream) Bytes(fieldNumber int, value []byte) error {
if len(value) == 0 {
return nil
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.BytesType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(len(value)))
err := ps.writeScratch()
if err != nil {
return err
}
return ps.writeAll(value)
}
// Embedded is used for constructing embedded messages. It calls the given
// function with a new ProtoStream, then embeds the result in the current
// stream.
//
// NOTE: if the inner function creates an empty message (such as for a struct
// at its zero value), that empty message will still be added to the stream.
func (ps *ProtoStream) Embedded(fieldNumber int, inner func(*ProtoStream) error) error {
// Create a new child, writing to a buffer, if one does not already exist.
if ps.childStream == nil {
ps.childBuffer = bytes.NewBuffer(ps.BufferFactory())
ps.childStream = NewProtoStream(ps.childBuffer)
}
// Write the embedded value using the child, leaving the result in ps.childBuffer.
ps.childBuffer.Reset()
err := inner(ps.childStream)
if err != nil {
return err
}
ps.scratchBuffer = ps.scratchBuffer[:0]
ps.encodeKeyToScratch(fieldNumber, protowire.BytesType)
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(ps.childBuffer.Len()))
// Write the key and length prefix.
err = ps.writeScratch()
if err != nil {
return err
}
// Write out the embedded message.
return ps.writeAll(ps.childBuffer.Bytes())
}
// Write writes raw []byte to the underlying writer. It is the callers
// responsibility to make sure this wont yield a corrupt protobuf stream.
func (ps *ProtoStream) Write(raw []byte) (int, error) {
return ps.outputWriter.Write(raw)
}
// writeScratch flushes the scratch buffer to output.
func (ps *ProtoStream) writeScratch() error {
return ps.writeAll(ps.scratchBuffer)
}
// writeScratchAsPacked writes the scratch buffer to outputWriter, prefixed with
// the given key and the length of the scratch buffer. This is used for packed
// encodings.
func (ps *ProtoStream) writeScratchAsPacked(fieldNumber int) error {
// The scratch buffer is full of the packed data, but we need to write
// the key and size, so we use scratchArray. We could use a stack allocation
// here, but as of writing the go compiler is not smart enough to figure out
// that the value does not escape.
keysize := ps.scratchArray[:0]
keysize = protowire.AppendVarint(keysize, uint64((fieldNumber<<3)|int(protowire.BytesType)))
keysize = protowire.AppendVarint(keysize, uint64(len(ps.scratchBuffer)))
// Write the key and length prefix.
err := ps.writeAll(keysize)
if err != nil {
return err
}
// Write out the embedded message.
err = ps.writeScratch()
if err != nil {
return err
}
return nil
}
// writeAll writes an entire buffer to output.
func (ps *ProtoStream) writeAll(buf []byte) error {
for len(buf) > 0 {
n, err := ps.outputWriter.Write(buf)
if err != nil {
return err
}
buf = buf[n:]
}
return nil
}
// writeAllString writes an entire string to output, using io.WriteString
// to avoid allocation.
func (ps *ProtoStream) writeAllString(value string) error {
for len(value) > 0 {
n, err := io.WriteString(ps.outputWriter, value)
if err != nil {
return err
}
value = value[n:]
}
return nil
}
// encodeKeyToScratch encodes a protobuf key into ps.scratch.
func (ps *ProtoStream) encodeKeyToScratch(fieldNumber int, wireType protowire.Type) {
ps.scratchBuffer = protowire.AppendVarint(ps.scratchBuffer, uint64(fieldNumber)<<3+uint64(wireType))
}