offset_fetch_response.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package sarama
  2. type OffsetFetchResponseBlock struct {
  3. Offset int64
  4. Metadata string
  5. Err KError
  6. }
  7. func (b *OffsetFetchResponseBlock) decode(pd packetDecoder) (err error) {
  8. b.Offset, err = pd.getInt64()
  9. if err != nil {
  10. return err
  11. }
  12. b.Metadata, err = pd.getString()
  13. if err != nil {
  14. return err
  15. }
  16. tmp, err := pd.getInt16()
  17. if err != nil {
  18. return err
  19. }
  20. b.Err = KError(tmp)
  21. return nil
  22. }
  23. func (b *OffsetFetchResponseBlock) encode(pe packetEncoder) (err error) {
  24. pe.putInt64(b.Offset)
  25. err = pe.putString(b.Metadata)
  26. if err != nil {
  27. return err
  28. }
  29. pe.putInt16(int16(b.Err))
  30. return nil
  31. }
  32. type OffsetFetchResponse struct {
  33. Blocks map[string]map[int32]*OffsetFetchResponseBlock
  34. }
  35. func (r *OffsetFetchResponse) encode(pe packetEncoder) error {
  36. if err := pe.putArrayLength(len(r.Blocks)); err != nil {
  37. return err
  38. }
  39. for topic, partitions := range r.Blocks {
  40. if err := pe.putString(topic); err != nil {
  41. return err
  42. }
  43. if err := pe.putArrayLength(len(partitions)); err != nil {
  44. return err
  45. }
  46. for partition, block := range partitions {
  47. pe.putInt32(partition)
  48. if err := block.encode(pe); err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. return nil
  54. }
  55. func (r *OffsetFetchResponse) decode(pd packetDecoder, version int16) (err error) {
  56. numTopics, err := pd.getArrayLength()
  57. if err != nil || numTopics == 0 {
  58. return err
  59. }
  60. r.Blocks = make(map[string]map[int32]*OffsetFetchResponseBlock, numTopics)
  61. for i := 0; i < numTopics; i++ {
  62. name, err := pd.getString()
  63. if err != nil {
  64. return err
  65. }
  66. numBlocks, err := pd.getArrayLength()
  67. if err != nil {
  68. return err
  69. }
  70. if numBlocks == 0 {
  71. r.Blocks[name] = nil
  72. continue
  73. }
  74. r.Blocks[name] = make(map[int32]*OffsetFetchResponseBlock, numBlocks)
  75. for j := 0; j < numBlocks; j++ {
  76. id, err := pd.getInt32()
  77. if err != nil {
  78. return err
  79. }
  80. block := new(OffsetFetchResponseBlock)
  81. err = block.decode(pd)
  82. if err != nil {
  83. return err
  84. }
  85. r.Blocks[name][id] = block
  86. }
  87. }
  88. return nil
  89. }
  90. func (r *OffsetFetchResponse) key() int16 {
  91. return 9
  92. }
  93. func (r *OffsetFetchResponse) version() int16 {
  94. return 0
  95. }
  96. func (r *OffsetFetchResponse) requiredVersion() KafkaVersion {
  97. return MinVersion
  98. }
  99. func (r *OffsetFetchResponse) GetBlock(topic string, partition int32) *OffsetFetchResponseBlock {
  100. if r.Blocks == nil {
  101. return nil
  102. }
  103. if r.Blocks[topic] == nil {
  104. return nil
  105. }
  106. return r.Blocks[topic][partition]
  107. }
  108. func (r *OffsetFetchResponse) AddBlock(topic string, partition int32, block *OffsetFetchResponseBlock) {
  109. if r.Blocks == nil {
  110. r.Blocks = make(map[string]map[int32]*OffsetFetchResponseBlock)
  111. }
  112. partitions := r.Blocks[topic]
  113. if partitions == nil {
  114. partitions = make(map[int32]*OffsetFetchResponseBlock)
  115. r.Blocks[topic] = partitions
  116. }
  117. partitions[partition] = block
  118. }