real_encoder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package sarama
  2. import (
  3. "encoding/binary"
  4. "github.com/rcrowley/go-metrics"
  5. )
  6. type realEncoder struct {
  7. raw []byte
  8. off int
  9. stack []pushEncoder
  10. registry metrics.Registry
  11. }
  12. // primitives
  13. func (re *realEncoder) putInt8(in int8) {
  14. re.raw[re.off] = byte(in)
  15. re.off++
  16. }
  17. func (re *realEncoder) putInt16(in int16) {
  18. binary.BigEndian.PutUint16(re.raw[re.off:], uint16(in))
  19. re.off += 2
  20. }
  21. func (re *realEncoder) putInt32(in int32) {
  22. binary.BigEndian.PutUint32(re.raw[re.off:], uint32(in))
  23. re.off += 4
  24. }
  25. func (re *realEncoder) putInt64(in int64) {
  26. binary.BigEndian.PutUint64(re.raw[re.off:], uint64(in))
  27. re.off += 8
  28. }
  29. func (re *realEncoder) putVarint(in int64) {
  30. re.off += binary.PutVarint(re.raw[re.off:], in)
  31. }
  32. func (re *realEncoder) putArrayLength(in int) error {
  33. re.putInt32(int32(in))
  34. return nil
  35. }
  36. // collection
  37. func (re *realEncoder) putRawBytes(in []byte) error {
  38. copy(re.raw[re.off:], in)
  39. re.off += len(in)
  40. return nil
  41. }
  42. func (re *realEncoder) putBytes(in []byte) error {
  43. if in == nil {
  44. re.putInt32(-1)
  45. return nil
  46. }
  47. re.putInt32(int32(len(in)))
  48. return re.putRawBytes(in)
  49. }
  50. func (re *realEncoder) putVarintBytes(in []byte) error {
  51. if in == nil {
  52. re.putVarint(-1)
  53. return nil
  54. }
  55. re.putVarint(int64(len(in)))
  56. return re.putRawBytes(in)
  57. }
  58. func (re *realEncoder) putString(in string) error {
  59. re.putInt16(int16(len(in)))
  60. copy(re.raw[re.off:], in)
  61. re.off += len(in)
  62. return nil
  63. }
  64. func (re *realEncoder) putStringArray(in []string) error {
  65. err := re.putArrayLength(len(in))
  66. if err != nil {
  67. return err
  68. }
  69. for _, val := range in {
  70. if err := re.putString(val); err != nil {
  71. return err
  72. }
  73. }
  74. return nil
  75. }
  76. func (re *realEncoder) putInt32Array(in []int32) error {
  77. err := re.putArrayLength(len(in))
  78. if err != nil {
  79. return err
  80. }
  81. for _, val := range in {
  82. re.putInt32(val)
  83. }
  84. return nil
  85. }
  86. func (re *realEncoder) putInt64Array(in []int64) error {
  87. err := re.putArrayLength(len(in))
  88. if err != nil {
  89. return err
  90. }
  91. for _, val := range in {
  92. re.putInt64(val)
  93. }
  94. return nil
  95. }
  96. func (re *realEncoder) offset() int {
  97. return re.off
  98. }
  99. // stacks
  100. func (re *realEncoder) push(in pushEncoder) {
  101. in.saveOffset(re.off)
  102. re.off += in.reserveLength()
  103. re.stack = append(re.stack, in)
  104. }
  105. func (re *realEncoder) pop() error {
  106. // this is go's ugly pop pattern (the inverse of append)
  107. in := re.stack[len(re.stack)-1]
  108. re.stack = re.stack[:len(re.stack)-1]
  109. return in.run(re.off, re.raw)
  110. }
  111. // we do record metrics during the real encoder pass
  112. func (re *realEncoder) metricRegistry() metrics.Registry {
  113. return re.registry
  114. }