packet_encoder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package sarama
  2. import "github.com/rcrowley/go-metrics"
  3. // PacketEncoder is the interface providing helpers for writing with Kafka's encoding rules.
  4. // Types implementing Encoder only need to worry about calling methods like PutString,
  5. // not about how a string is represented in Kafka.
  6. type packetEncoder interface {
  7. // Primitives
  8. putInt8(in int8)
  9. putInt16(in int16)
  10. putInt32(in int32)
  11. putInt64(in int64)
  12. putVarint(in int64)
  13. putUVarint(in uint64)
  14. putCompactArrayLength(in int)
  15. putArrayLength(in int) error
  16. putBool(in bool)
  17. // Collections
  18. putBytes(in []byte) error
  19. putVarintBytes(in []byte) error
  20. putRawBytes(in []byte) error
  21. putCompactString(in string) error
  22. putNullableCompactString(in *string) error
  23. putString(in string) error
  24. putNullableString(in *string) error
  25. putStringArray(in []string) error
  26. putCompactInt32Array(in []int32) error
  27. putNullableCompactInt32Array(in []int32) error
  28. putInt32Array(in []int32) error
  29. putInt64Array(in []int64) error
  30. putEmptyTaggedFieldArray()
  31. // Provide the current offset to record the batch size metric
  32. offset() int
  33. // Stacks, see PushEncoder
  34. push(in pushEncoder)
  35. pop() error
  36. // To record metrics when provided
  37. metricRegistry() metrics.Registry
  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. // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
  47. reserveLength() int
  48. // Indicates that all required data is now available to calculate and write the field.
  49. // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes
  50. // of data to the saved offset, based on the data between the saved offset and curOffset.
  51. run(curOffset int, buf []byte) error
  52. }
  53. // dynamicPushEncoder extends the interface of pushEncoder for uses cases where the length of the
  54. // fields itself is unknown until its value was computed (for instance varint encoded length
  55. // fields).
  56. type dynamicPushEncoder interface {
  57. pushEncoder
  58. // Called during pop() to adjust the length of the field.
  59. // It should return the difference in bytes between the last computed length and current length.
  60. adjustLength(currOffset int) int
  61. }