fetch_response.go 3.4 KB

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