cubefs
69 строк · 2.7 Кб
1package sarama
2
3// PacketDecoder is the interface providing helpers for reading with Kafka's encoding rules.
4// Types implementing Decoder only need to worry about calling methods like GetString,
5// not about how a string is represented in Kafka.
6type packetDecoder interface {
7// Primitives
8getInt8() (int8, error)
9getInt16() (int16, error)
10getInt32() (int32, error)
11getInt64() (int64, error)
12getVarint() (int64, error)
13getUVarint() (uint64, error)
14getFloat64() (float64, error)
15getArrayLength() (int, error)
16getCompactArrayLength() (int, error)
17getBool() (bool, error)
18getEmptyTaggedFieldArray() (int, error)
19
20// Collections
21getBytes() ([]byte, error)
22getVarintBytes() ([]byte, error)
23getCompactBytes() ([]byte, error)
24getRawBytes(length int) ([]byte, error)
25getString() (string, error)
26getNullableString() (*string, error)
27getCompactString() (string, error)
28getCompactNullableString() (*string, error)
29getCompactInt32Array() ([]int32, error)
30getInt32Array() ([]int32, error)
31getInt64Array() ([]int64, error)
32getStringArray() ([]string, error)
33
34// Subsets
35remaining() int
36getSubset(length int) (packetDecoder, error)
37peek(offset, length int) (packetDecoder, error) // similar to getSubset, but it doesn't advance the offset
38peekInt8(offset int) (int8, error) // similar to peek, but just one byte
39
40// Stacks, see PushDecoder
41push(in pushDecoder) error
42pop() error
43}
44
45// PushDecoder is the interface for decoding fields like CRCs and lengths where the validity
46// of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where
47// the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they
48// depend upon have been decoded.
49type pushDecoder interface {
50// Saves the offset into the input buffer as the location to actually read the calculated value when able.
51saveOffset(in int)
52
53// Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32).
54reserveLength() int
55
56// Indicates that all required data is now available to calculate and check the field.
57// SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes
58// of data from the saved offset, and verify it based on the data between the saved offset and curOffset.
59check(curOffset int, buf []byte) error
60}
61
62// dynamicPushDecoder extends the interface of pushDecoder for uses cases where the length of the
63// fields itself is unknown until its value was decoded (for instance varint encoded length
64// fields).
65// During push, dynamicPushDecoder.decode() method will be called instead of reserveLength()
66type dynamicPushDecoder interface {
67pushDecoder
68decoder
69}
70