cubefs
74 строки · 2.6 Кб
1package sarama
2
3import "github.com/rcrowley/go-metrics"
4
5// PacketEncoder is the interface providing helpers for writing with Kafka's encoding rules.
6// Types implementing Encoder only need to worry about calling methods like PutString,
7// not about how a string is represented in Kafka.
8type packetEncoder interface {
9// Primitives
10putInt8(in int8)
11putInt16(in int16)
12putInt32(in int32)
13putInt64(in int64)
14putVarint(in int64)
15putUVarint(in uint64)
16putFloat64(in float64)
17putCompactArrayLength(in int)
18putArrayLength(in int) error
19putBool(in bool)
20
21// Collections
22putBytes(in []byte) error
23putVarintBytes(in []byte) error
24putCompactBytes(in []byte) error
25putRawBytes(in []byte) error
26putCompactString(in string) error
27putNullableCompactString(in *string) error
28putString(in string) error
29putNullableString(in *string) error
30putStringArray(in []string) error
31putCompactInt32Array(in []int32) error
32putNullableCompactInt32Array(in []int32) error
33putInt32Array(in []int32) error
34putInt64Array(in []int64) error
35putEmptyTaggedFieldArray()
36
37// Provide the current offset to record the batch size metric
38offset() int
39
40// Stacks, see PushEncoder
41push(in pushEncoder)
42pop() error
43
44// To record metrics when provided
45metricRegistry() metrics.Registry
46}
47
48// PushEncoder is the interface for encoding fields like CRCs and lengths where the value
49// of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where
50// the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they
51// depend upon have been written.
52type pushEncoder interface {
53// Saves the offset into the input buffer as the location to actually write the calculated value when able.
54saveOffset(in int)
55
56// Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
57reserveLength() int
58
59// Indicates that all required data is now available to calculate and write the field.
60// SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes
61// of data to the saved offset, based on the data between the saved offset and curOffset.
62run(curOffset int, buf []byte) error
63}
64
65// dynamicPushEncoder extends the interface of pushEncoder for uses cases where the length of the
66// fields itself is unknown until its value was computed (for instance varint encoded length
67// fields).
68type dynamicPushEncoder interface {
69pushEncoder
70
71// Called during pop() to adjust the length of the field.
72// It should return the difference in bytes between the last computed length and current length.
73adjustLength(currOffset int) int
74}
75