prep_encoder.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package sarama
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math"
  6. "github.com/rcrowley/go-metrics"
  7. )
  8. type prepEncoder struct {
  9. stack []pushEncoder
  10. length int
  11. }
  12. // primitives
  13. func (pe *prepEncoder) putInt8(in int8) {
  14. pe.length++
  15. }
  16. func (pe *prepEncoder) putInt16(in int16) {
  17. pe.length += 2
  18. }
  19. func (pe *prepEncoder) putInt32(in int32) {
  20. pe.length += 4
  21. }
  22. func (pe *prepEncoder) putInt64(in int64) {
  23. pe.length += 8
  24. }
  25. func (pe *prepEncoder) putVarint(in int64) {
  26. var buf [binary.MaxVarintLen64]byte
  27. pe.length += binary.PutVarint(buf[:], in)
  28. }
  29. func (pe *prepEncoder) putArrayLength(in int) error {
  30. if in > math.MaxInt32 {
  31. return PacketEncodingError{fmt.Sprintf("array too long (%d)", in)}
  32. }
  33. pe.length += 4
  34. return nil
  35. }
  36. func (pe *prepEncoder) putBool(in bool) {
  37. pe.length++
  38. }
  39. // arrays
  40. func (pe *prepEncoder) putBytes(in []byte) error {
  41. pe.length += 4
  42. if in == nil {
  43. return nil
  44. }
  45. return pe.putRawBytes(in)
  46. }
  47. func (pe *prepEncoder) putVarintBytes(in []byte) error {
  48. if in == nil {
  49. pe.putVarint(-1)
  50. return nil
  51. }
  52. pe.putVarint(int64(len(in)))
  53. return pe.putRawBytes(in)
  54. }
  55. func (pe *prepEncoder) putRawBytes(in []byte) error {
  56. if len(in) > math.MaxInt32 {
  57. return PacketEncodingError{fmt.Sprintf("byteslice too long (%d)", len(in))}
  58. }
  59. pe.length += len(in)
  60. return nil
  61. }
  62. func (pe *prepEncoder) putNullableString(in *string) error {
  63. if in == nil {
  64. pe.length += 2
  65. return nil
  66. }
  67. return pe.putString(*in)
  68. }
  69. func (pe *prepEncoder) putString(in string) error {
  70. pe.length += 2
  71. if len(in) > math.MaxInt16 {
  72. return PacketEncodingError{fmt.Sprintf("string too long (%d)", len(in))}
  73. }
  74. pe.length += len(in)
  75. return nil
  76. }
  77. func (pe *prepEncoder) putStringArray(in []string) error {
  78. err := pe.putArrayLength(len(in))
  79. if err != nil {
  80. return err
  81. }
  82. for _, str := range in {
  83. if err := pe.putString(str); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. func (pe *prepEncoder) putInt32Array(in []int32) error {
  90. err := pe.putArrayLength(len(in))
  91. if err != nil {
  92. return err
  93. }
  94. pe.length += 4 * len(in)
  95. return nil
  96. }
  97. func (pe *prepEncoder) putInt64Array(in []int64) error {
  98. err := pe.putArrayLength(len(in))
  99. if err != nil {
  100. return err
  101. }
  102. pe.length += 8 * len(in)
  103. return nil
  104. }
  105. func (pe *prepEncoder) offset() int {
  106. return pe.length
  107. }
  108. // stackable
  109. func (pe *prepEncoder) push(in pushEncoder) {
  110. in.saveOffset(pe.length)
  111. pe.length += in.reserveLength()
  112. pe.stack = append(pe.stack, in)
  113. }
  114. func (pe *prepEncoder) pop() error {
  115. in := pe.stack[len(pe.stack)-1]
  116. pe.stack = pe.stack[:len(pe.stack)-1]
  117. if dpe, ok := in.(dynamicPushEncoder); ok {
  118. pe.length += dpe.adjustLength(pe.length)
  119. }
  120. return nil
  121. }
  122. // we do not record metrics during the prep encoder pass
  123. func (pe *prepEncoder) metricRegistry() metrics.Registry {
  124. return nil
  125. }