William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 1 | package sarama |
| 2 | |
| 3 | import "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. |
| 8 | type packetEncoder interface { |
| 9 | // Primitives |
| 10 | putInt8(in int8) |
| 11 | putInt16(in int16) |
| 12 | putInt32(in int32) |
| 13 | putInt64(in int64) |
| 14 | putVarint(in int64) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 15 | putUVarint(in uint64) |
| 16 | putCompactArrayLength(in int) |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 17 | putArrayLength(in int) error |
| 18 | putBool(in bool) |
| 19 | |
| 20 | // Collections |
| 21 | putBytes(in []byte) error |
| 22 | putVarintBytes(in []byte) error |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 23 | putCompactBytes(in []byte) error |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 24 | putRawBytes(in []byte) error |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 25 | putCompactString(in string) error |
| 26 | putNullableCompactString(in *string) error |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 27 | putString(in string) error |
| 28 | putNullableString(in *string) error |
| 29 | putStringArray(in []string) error |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 30 | putCompactInt32Array(in []int32) error |
| 31 | putNullableCompactInt32Array(in []int32) error |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 32 | putInt32Array(in []int32) error |
| 33 | putInt64Array(in []int64) error |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 34 | putEmptyTaggedFieldArray() |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 35 | |
| 36 | // Provide the current offset to record the batch size metric |
| 37 | offset() int |
| 38 | |
| 39 | // Stacks, see PushEncoder |
| 40 | push(in pushEncoder) |
| 41 | pop() error |
| 42 | |
| 43 | // To record metrics when provided |
| 44 | metricRegistry() metrics.Registry |
| 45 | } |
| 46 | |
| 47 | // PushEncoder is the interface for encoding fields like CRCs and lengths where the value |
| 48 | // of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where |
| 49 | // the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they |
| 50 | // depend upon have been written. |
| 51 | type pushEncoder interface { |
| 52 | // Saves the offset into the input buffer as the location to actually write the calculated value when able. |
| 53 | saveOffset(in int) |
| 54 | |
| 55 | // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32). |
| 56 | reserveLength() int |
| 57 | |
| 58 | // Indicates that all required data is now available to calculate and write the field. |
| 59 | // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes |
| 60 | // of data to the saved offset, based on the data between the saved offset and curOffset. |
| 61 | run(curOffset int, buf []byte) error |
| 62 | } |
| 63 | |
| 64 | // dynamicPushEncoder extends the interface of pushEncoder for uses cases where the length of the |
| 65 | // fields itself is unknown until its value was computed (for instance varint encoded length |
| 66 | // fields). |
| 67 | type dynamicPushEncoder interface { |
| 68 | pushEncoder |
| 69 | |
| 70 | // Called during pop() to adjust the length of the field. |
| 71 | // It should return the difference in bytes between the last computed length and current length. |
| 72 | adjustLength(currOffset int) int |
| 73 | } |