fetch_response.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package sarama
  2. type FetchResponseBlock struct {
  3. Err KError
  4. HighWaterMarkOffset int64
  5. MsgSet MessageSet
  6. }
  7. func (pr *FetchResponseBlock) decode(pd packetDecoder) (err error) {
  8. tmp, err := pd.getInt16()
  9. if err != nil {
  10. return err
  11. }
  12. pr.Err = KError(tmp)
  13. pr.HighWaterMarkOffset, err = pd.getInt64()
  14. if err != nil {
  15. return err
  16. }
  17. msgSetSize, err := pd.getInt32()
  18. if err != nil {
  19. return err
  20. }
  21. msgSetDecoder, err := pd.getSubset(int(msgSetSize))
  22. if err != nil {
  23. return err
  24. }
  25. err = (&pr.MsgSet).decode(msgSetDecoder)
  26. return err
  27. }
  28. type FetchResponse struct {
  29. Blocks map[string]map[int32]*FetchResponseBlock
  30. }
  31. func (pr *FetchResponseBlock) encode(pe packetEncoder) (err error) {
  32. pe.putInt16(int16(pr.Err))
  33. pe.putInt64(pr.HighWaterMarkOffset)
  34. pe.push(&lengthField{})
  35. err = pr.MsgSet.encode(pe)
  36. if err != nil {
  37. return err
  38. }
  39. return pe.pop()
  40. }
  41. func (fr *FetchResponse) decode(pd packetDecoder, version int16) (err error) {
  42. numTopics, err := pd.getArrayLength()
  43. if err != nil {
  44. return err
  45. }
  46. fr.Blocks = make(map[string]map[int32]*FetchResponseBlock, numTopics)
  47. for i := 0; i < numTopics; i++ {
  48. name, err := pd.getString()
  49. if err != nil {
  50. return err
  51. }
  52. numBlocks, err := pd.getArrayLength()
  53. if err != nil {
  54. return err
  55. }
  56. fr.Blocks[name] = make(map[int32]*FetchResponseBlock, numBlocks)
  57. for j := 0; j < numBlocks; j++ {
  58. id, err := pd.getInt32()
  59. if err != nil {
  60. return err
  61. }
  62. block := new(FetchResponseBlock)
  63. err = block.decode(pd)
  64. if err != nil {
  65. return err
  66. }
  67. fr.Blocks[name][id] = block
  68. }
  69. }
  70. return nil
  71. }
  72. func (fr *FetchResponse) encode(pe packetEncoder) (err error) {
  73. err = pe.putArrayLength(len(fr.Blocks))
  74. if err != nil {
  75. return err
  76. }
  77. for topic, partitions := range fr.Blocks {
  78. err = pe.putString(topic)
  79. if err != nil {
  80. return err
  81. }
  82. err = pe.putArrayLength(len(partitions))
  83. if err != nil {
  84. return err
  85. }
  86. for id, block := range partitions {
  87. pe.putInt32(id)
  88. err = block.encode(pe)
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. }
  94. return nil
  95. }
  96. func (r *FetchResponse) key() int16 {
  97. return 1
  98. }
  99. func (r *FetchResponse) version() int16 {
  100. return 0
  101. }
  102. func (r *FetchResponse) requiredVersion() KafkaVersion {
  103. return minVersion
  104. }
  105. func (fr *FetchResponse) GetBlock(topic string, partition int32) *FetchResponseBlock {
  106. if fr.Blocks == nil {
  107. return nil
  108. }
  109. if fr.Blocks[topic] == nil {
  110. return nil
  111. }
  112. return fr.Blocks[topic][partition]
  113. }
  114. func (fr *FetchResponse) AddError(topic string, partition int32, err KError) {
  115. if fr.Blocks == nil {
  116. fr.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  117. }
  118. partitions, ok := fr.Blocks[topic]
  119. if !ok {
  120. partitions = make(map[int32]*FetchResponseBlock)
  121. fr.Blocks[topic] = partitions
  122. }
  123. frb, ok := partitions[partition]
  124. if !ok {
  125. frb = new(FetchResponseBlock)
  126. partitions[partition] = frb
  127. }
  128. frb.Err = err
  129. }
  130. func (fr *FetchResponse) AddMessage(topic string, partition int32, key, value Encoder, offset int64) {
  131. if fr.Blocks == nil {
  132. fr.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  133. }
  134. partitions, ok := fr.Blocks[topic]
  135. if !ok {
  136. partitions = make(map[int32]*FetchResponseBlock)
  137. fr.Blocks[topic] = partitions
  138. }
  139. frb, ok := partitions[partition]
  140. if !ok {
  141. frb = new(FetchResponseBlock)
  142. partitions[partition] = frb
  143. }
  144. var kb []byte
  145. var vb []byte
  146. if key != nil {
  147. kb, _ = key.Encode()
  148. }
  149. if value != nil {
  150. vb, _ = value.Encode()
  151. }
  152. msg := &Message{Key: kb, Value: vb}
  153. msgBlock := &MessageBlock{Msg: msg, Offset: offset}
  154. frb.MsgSet.Messages = append(frb.MsgSet.Messages, msgBlock)
  155. }