blob: 5016e09a6cebc022b98afc6d736276d61e05db3d [file] [log] [blame]
kesavand2cde6582020-06-22 04:56:23 -04001package 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
10 putInt8(in int8)
11 putInt16(in int16)
12 putInt32(in int32)
13 putInt64(in int64)
14 putVarint(in int64)
kesavandc71914f2022-03-25 11:19:03 +053015 putUVarint(in uint64)
16 putFloat64(in float64)
17 putCompactArrayLength(in int)
kesavand2cde6582020-06-22 04:56:23 -040018 putArrayLength(in int) error
19 putBool(in bool)
20
21 // Collections
22 putBytes(in []byte) error
23 putVarintBytes(in []byte) error
kesavandc71914f2022-03-25 11:19:03 +053024 putCompactBytes(in []byte) error
kesavand2cde6582020-06-22 04:56:23 -040025 putRawBytes(in []byte) error
kesavandc71914f2022-03-25 11:19:03 +053026 putCompactString(in string) error
27 putNullableCompactString(in *string) error
kesavand2cde6582020-06-22 04:56:23 -040028 putString(in string) error
29 putNullableString(in *string) error
30 putStringArray(in []string) error
kesavandc71914f2022-03-25 11:19:03 +053031 putCompactInt32Array(in []int32) error
32 putNullableCompactInt32Array(in []int32) error
kesavand2cde6582020-06-22 04:56:23 -040033 putInt32Array(in []int32) error
34 putInt64Array(in []int64) error
kesavandc71914f2022-03-25 11:19:03 +053035 putEmptyTaggedFieldArray()
kesavand2cde6582020-06-22 04:56:23 -040036
37 // Provide the current offset to record the batch size metric
38 offset() int
39
40 // Stacks, see PushEncoder
41 push(in pushEncoder)
42 pop() error
43
44 // To record metrics when provided
45 metricRegistry() 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.
54 saveOffset(in int)
55
56 // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
57 reserveLength() 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.
62 run(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 {
69 pushEncoder
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.
73 adjustLength(currOffset int) int
74}