real_encoder.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) putArrayLength(in int) error {
  30. re.putInt32(int32(in))
  31. return nil
  32. }
  33. // collection
  34. func (re *realEncoder) putRawBytes(in []byte) error {
  35. copy(re.raw[re.off:], in)
  36. re.off += len(in)
  37. return nil
  38. }
  39. func (re *realEncoder) putBytes(in []byte) error {
  40. if in == nil {
  41. re.putInt32(-1)
  42. return nil
  43. }
  44. re.putInt32(int32(len(in)))
  45. copy(re.raw[re.off:], in)
  46. re.off += len(in)
  47. return nil
  48. }
  49. func (re *realEncoder) putString(in string) error {
  50. re.putInt16(int16(len(in)))
  51. copy(re.raw[re.off:], in)
  52. re.off += len(in)
  53. return nil
  54. }
  55. func (re *realEncoder) putStringArray(in []string) error {
  56. err := re.putArrayLength(len(in))
  57. if err != nil {
  58. return err
  59. }
  60. for _, val := range in {
  61. if err := re.putString(val); err != nil {
  62. return err
  63. }
  64. }
  65. return nil
  66. }
  67. func (re *realEncoder) putInt32Array(in []int32) error {
  68. err := re.putArrayLength(len(in))
  69. if err != nil {
  70. return err
  71. }
  72. for _, val := range in {
  73. re.putInt32(val)
  74. }
  75. return nil
  76. }
  77. func (re *realEncoder) putInt64Array(in []int64) error {
  78. err := re.putArrayLength(len(in))
  79. if err != nil {
  80. return err
  81. }
  82. for _, val := range in {
  83. re.putInt64(val)
  84. }
  85. return nil
  86. }
  87. func (re *realEncoder) offset() int {
  88. return re.off
  89. }
  90. // stacks
  91. func (re *realEncoder) push(in pushEncoder) {
  92. in.saveOffset(re.off)
  93. re.off += in.reserveLength()
  94. re.stack = append(re.stack, in)
  95. }
  96. func (re *realEncoder) pop() error {
  97. // this is go's ugly pop pattern (the inverse of append)
  98. in := re.stack[len(re.stack)-1]
  99. re.stack = re.stack[:len(re.stack)-1]
  100. return in.run(re.off, re.raw)
  101. }
  102. // we do record metrics during the real encoder pass
  103. func (re *realEncoder) metricRegistry() metrics.Registry {
  104. return re.registry
  105. }