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) |
| 15 | putArrayLength(in int) error |
| 16 | putBool(in bool) |
| 17 | |
| 18 | // Collections |
| 19 | putBytes(in []byte) error |
| 20 | putVarintBytes(in []byte) error |
| 21 | putRawBytes(in []byte) error |
| 22 | putString(in string) error |
| 23 | putNullableString(in *string) error |
| 24 | putStringArray(in []string) error |
| 25 | putInt32Array(in []int32) error |
| 26 | putInt64Array(in []int64) error |
| 27 | |
| 28 | // Provide the current offset to record the batch size metric |
| 29 | offset() int |
| 30 | |
| 31 | // Stacks, see PushEncoder |
| 32 | push(in pushEncoder) |
| 33 | pop() error |
| 34 | |
| 35 | // To record metrics when provided |
| 36 | metricRegistry() metrics.Registry |
| 37 | } |
| 38 | |
| 39 | // PushEncoder is the interface for encoding fields like CRCs and lengths where the value |
| 40 | // of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where |
| 41 | // the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they |
| 42 | // depend upon have been written. |
| 43 | type pushEncoder interface { |
| 44 | // Saves the offset into the input buffer as the location to actually write the calculated value when able. |
| 45 | saveOffset(in int) |
| 46 | |
| 47 | // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32). |
| 48 | reserveLength() int |
| 49 | |
| 50 | // Indicates that all required data is now available to calculate and write the field. |
| 51 | // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes |
| 52 | // of data to the saved offset, based on the data between the saved offset and curOffset. |
| 53 | run(curOffset int, buf []byte) error |
| 54 | } |
| 55 | |
| 56 | // dynamicPushEncoder extends the interface of pushEncoder for uses cases where the length of the |
| 57 | // fields itself is unknown until its value was computed (for instance varint encoded length |
| 58 | // fields). |
| 59 | type dynamicPushEncoder interface { |
| 60 | pushEncoder |
| 61 | |
| 62 | // Called during pop() to adjust the length of the field. |
| 63 | // It should return the difference in bytes between the last computed length and current length. |
| 64 | adjustLength(currOffset int) int |
| 65 | } |