fetch_response.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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) (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 (fr *FetchResponse) GetBlock(topic string, partition int32) *FetchResponseBlock {
  97. if fr.Blocks == nil {
  98. return nil
  99. }
  100. if fr.Blocks[topic] == nil {
  101. return nil
  102. }
  103. return fr.Blocks[topic][partition]
  104. }
  105. func (fr *FetchResponse) AddError(topic string, partition int32, err KError) {
  106. if fr.Blocks == nil {
  107. fr.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  108. }
  109. partitions, ok := fr.Blocks[topic]
  110. if !ok {
  111. partitions = make(map[int32]*FetchResponseBlock)
  112. fr.Blocks[topic] = partitions
  113. }
  114. frb, ok := partitions[partition]
  115. if !ok {
  116. frb = new(FetchResponseBlock)
  117. partitions[partition] = frb
  118. }
  119. frb.Err = err
  120. }
  121. func (fr *FetchResponse) AddMessage(topic string, partition int32, key, value Encoder, offset int64) {
  122. if fr.Blocks == nil {
  123. fr.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  124. }
  125. partitions, ok := fr.Blocks[topic]
  126. if !ok {
  127. partitions = make(map[int32]*FetchResponseBlock)
  128. fr.Blocks[topic] = partitions
  129. }
  130. frb, ok := partitions[partition]
  131. if !ok {
  132. frb = new(FetchResponseBlock)
  133. partitions[partition] = frb
  134. }
  135. var kb []byte
  136. var vb []byte
  137. if key != nil {
  138. kb, _ = key.Encode()
  139. }
  140. if value != nil {
  141. vb, _ = value.Encode()
  142. }
  143. msg := &Message{Key: kb, Value: vb}
  144. msgBlock := &MessageBlock{Msg: msg, Offset: offset}
  145. frb.MsgSet.Messages = append(frb.MsgSet.Messages, msgBlock)
  146. }