packet_encoder.go 2.6 KB

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