blob: aea53ca83b4d0e3284d0aea3ac923f059ee174f5 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001package 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)
khenaidood948f772021-08-11 17:49:24 -040015 putUVarint(in uint64)
16 putCompactArrayLength(in int)
khenaidooac637102019-01-14 15:44:34 -050017 putArrayLength(in int) error
18 putBool(in bool)
19
20 // Collections
21 putBytes(in []byte) error
22 putVarintBytes(in []byte) error
khenaidood948f772021-08-11 17:49:24 -040023 putCompactBytes(in []byte) error
khenaidooac637102019-01-14 15:44:34 -050024 putRawBytes(in []byte) error
khenaidood948f772021-08-11 17:49:24 -040025 putCompactString(in string) error
26 putNullableCompactString(in *string) error
khenaidooac637102019-01-14 15:44:34 -050027 putString(in string) error
28 putNullableString(in *string) error
29 putStringArray(in []string) error
khenaidood948f772021-08-11 17:49:24 -040030 putCompactInt32Array(in []int32) error
31 putNullableCompactInt32Array(in []int32) error
khenaidooac637102019-01-14 15:44:34 -050032 putInt32Array(in []int32) error
33 putInt64Array(in []int64) error
khenaidood948f772021-08-11 17:49:24 -040034 putEmptyTaggedFieldArray()
khenaidooac637102019-01-14 15:44:34 -050035
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.
51type 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).
67type 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}