offset_fetch_response.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package sarama
  2. type OffsetFetchResponseBlock struct {
  3. Offset int64
  4. Metadata string
  5. Err KError
  6. }
  7. func (r *OffsetFetchResponseBlock) decode(pd packetDecoder) (err error) {
  8. r.Offset, err = pd.getInt64()
  9. if err != nil {
  10. return err
  11. }
  12. r.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. r.Err = KError(tmp)
  21. return nil
  22. }
  23. func (r *OffsetFetchResponseBlock) encode(pe packetEncoder) (err error) {
  24. pe.putInt64(r.Offset)
  25. err = pe.putString(r.Metadata)
  26. if err != nil {
  27. return err
  28. }
  29. pe.putInt16(int16(r.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) (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) GetBlock(topic string, partition int32) *OffsetFetchResponseBlock {
  91. if r.Blocks == nil {
  92. return nil
  93. }
  94. if r.Blocks[topic] == nil {
  95. return nil
  96. }
  97. return r.Blocks[topic][partition]
  98. }
  99. func (r *OffsetFetchResponse) AddBlock(topic string, partition int32, block *OffsetFetchResponseBlock) {
  100. if r.Blocks == nil {
  101. r.Blocks = make(map[string]map[int32]*OffsetFetchResponseBlock)
  102. }
  103. partitions := r.Blocks[topic]
  104. if partitions == nil {
  105. partitions = make(map[int32]*OffsetFetchResponseBlock)
  106. r.Blocks[topic] = partitions
  107. }
  108. partitions[partition] = block
  109. }