create_partitions_request.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package sarama
  2. import "time"
  3. type CreatePartitionsRequest struct {
  4. Version int16
  5. TopicPartitions map[string]*TopicPartition
  6. Timeout time.Duration
  7. ValidateOnly bool
  8. }
  9. func (c *CreatePartitionsRequest) encode(pe packetEncoder) error {
  10. if err := pe.putArrayLength(len(c.TopicPartitions)); err != nil {
  11. return err
  12. }
  13. for topic, partition := range c.TopicPartitions {
  14. if err := pe.putString(topic); err != nil {
  15. return err
  16. }
  17. if err := partition.encode(pe); err != nil {
  18. return err
  19. }
  20. }
  21. pe.putInt32(int32(c.Timeout / time.Millisecond))
  22. pe.putBool(c.ValidateOnly)
  23. return nil
  24. }
  25. func (c *CreatePartitionsRequest) decode(pd packetDecoder, version int16) (err error) {
  26. n, err := pd.getArrayLength()
  27. if err != nil {
  28. return err
  29. }
  30. c.TopicPartitions = make(map[string]*TopicPartition, n)
  31. for i := 0; i < n; i++ {
  32. topic, err := pd.getString()
  33. if err != nil {
  34. return err
  35. }
  36. c.TopicPartitions[topic] = new(TopicPartition)
  37. if err := c.TopicPartitions[topic].decode(pd, version); err != nil {
  38. return err
  39. }
  40. }
  41. timeout, err := pd.getInt32()
  42. if err != nil {
  43. return err
  44. }
  45. c.Timeout = time.Duration(timeout) * time.Millisecond
  46. if c.ValidateOnly, err = pd.getBool(); err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (r *CreatePartitionsRequest) key() int16 {
  52. return 37
  53. }
  54. func (r *CreatePartitionsRequest) version() int16 {
  55. return r.Version
  56. }
  57. func (r *CreatePartitionsRequest) headerVersion() int16 {
  58. return 1
  59. }
  60. func (r *CreatePartitionsRequest) isValidVersion() bool {
  61. return r.Version >= 0 && r.Version <= 1
  62. }
  63. func (r *CreatePartitionsRequest) requiredVersion() KafkaVersion {
  64. switch r.Version {
  65. case 1:
  66. return V2_0_0_0
  67. case 0:
  68. return V1_0_0_0
  69. default:
  70. return V2_0_0_0
  71. }
  72. }
  73. type TopicPartition struct {
  74. Count int32
  75. Assignment [][]int32
  76. }
  77. func (t *TopicPartition) encode(pe packetEncoder) error {
  78. pe.putInt32(t.Count)
  79. if len(t.Assignment) == 0 {
  80. pe.putInt32(-1)
  81. return nil
  82. }
  83. if err := pe.putArrayLength(len(t.Assignment)); err != nil {
  84. return err
  85. }
  86. for _, assign := range t.Assignment {
  87. if err := pe.putInt32Array(assign); err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. func (t *TopicPartition) decode(pd packetDecoder, version int16) (err error) {
  94. if t.Count, err = pd.getInt32(); err != nil {
  95. return err
  96. }
  97. n, err := pd.getInt32()
  98. if err != nil {
  99. return err
  100. }
  101. if n <= 0 {
  102. return nil
  103. }
  104. t.Assignment = make([][]int32, n)
  105. for i := 0; i < int(n); i++ {
  106. if t.Assignment[i], err = pd.getInt32Array(); err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. }