Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame^] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // response message format is: |
| 9 | // throttleMs(int32) [topic] |
| 10 | // where topic is: |
| 11 | // name(string) [partition] |
| 12 | // where partition is: |
| 13 | // id(int32) low_watermark(int64) error_code(int16) |
| 14 | |
| 15 | type DeleteRecordsResponse struct { |
| 16 | Version int16 |
| 17 | ThrottleTime time.Duration |
| 18 | Topics map[string]*DeleteRecordsResponseTopic |
| 19 | } |
| 20 | |
| 21 | func (d *DeleteRecordsResponse) encode(pe packetEncoder) error { |
| 22 | pe.putInt32(int32(d.ThrottleTime / time.Millisecond)) |
| 23 | |
| 24 | if err := pe.putArrayLength(len(d.Topics)); err != nil { |
| 25 | return err |
| 26 | } |
| 27 | keys := make([]string, 0, len(d.Topics)) |
| 28 | for topic := range d.Topics { |
| 29 | keys = append(keys, topic) |
| 30 | } |
| 31 | sort.Strings(keys) |
| 32 | for _, topic := range keys { |
| 33 | if err := pe.putString(topic); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if err := d.Topics[topic].encode(pe); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | } |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | func (d *DeleteRecordsResponse) decode(pd packetDecoder, version int16) error { |
| 44 | d.Version = version |
| 45 | |
| 46 | throttleTime, err := pd.getInt32() |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | d.ThrottleTime = time.Duration(throttleTime) * time.Millisecond |
| 51 | |
| 52 | n, err := pd.getArrayLength() |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | if n > 0 { |
| 58 | d.Topics = make(map[string]*DeleteRecordsResponseTopic, n) |
| 59 | for i := 0; i < n; i++ { |
| 60 | topic, err := pd.getString() |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | details := new(DeleteRecordsResponseTopic) |
| 65 | if err = details.decode(pd, version); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | d.Topics[topic] = details |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | func (d *DeleteRecordsResponse) key() int16 { |
| 76 | return 21 |
| 77 | } |
| 78 | |
| 79 | func (d *DeleteRecordsResponse) version() int16 { |
| 80 | return 0 |
| 81 | } |
| 82 | |
| 83 | func (d *DeleteRecordsResponse) requiredVersion() KafkaVersion { |
| 84 | return V0_11_0_0 |
| 85 | } |
| 86 | |
| 87 | type DeleteRecordsResponseTopic struct { |
| 88 | Partitions map[int32]*DeleteRecordsResponsePartition |
| 89 | } |
| 90 | |
| 91 | func (t *DeleteRecordsResponseTopic) encode(pe packetEncoder) error { |
| 92 | if err := pe.putArrayLength(len(t.Partitions)); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | keys := make([]int32, 0, len(t.Partitions)) |
| 96 | for partition := range t.Partitions { |
| 97 | keys = append(keys, partition) |
| 98 | } |
| 99 | sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) |
| 100 | for _, partition := range keys { |
| 101 | pe.putInt32(partition) |
| 102 | if err := t.Partitions[partition].encode(pe); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | func (t *DeleteRecordsResponseTopic) decode(pd packetDecoder, version int16) error { |
| 110 | n, err := pd.getArrayLength() |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | if n > 0 { |
| 116 | t.Partitions = make(map[int32]*DeleteRecordsResponsePartition, n) |
| 117 | for i := 0; i < n; i++ { |
| 118 | partition, err := pd.getInt32() |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | details := new(DeleteRecordsResponsePartition) |
| 123 | if err = details.decode(pd, version); err != nil { |
| 124 | return err |
| 125 | } |
| 126 | t.Partitions[partition] = details |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | type DeleteRecordsResponsePartition struct { |
| 134 | LowWatermark int64 |
| 135 | Err KError |
| 136 | } |
| 137 | |
| 138 | func (t *DeleteRecordsResponsePartition) encode(pe packetEncoder) error { |
| 139 | pe.putInt64(t.LowWatermark) |
| 140 | pe.putInt16(int16(t.Err)) |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | func (t *DeleteRecordsResponsePartition) decode(pd packetDecoder, version int16) error { |
| 145 | lowWatermark, err := pd.getInt64() |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | t.LowWatermark = lowWatermark |
| 150 | |
| 151 | kErr, err := pd.getInt16() |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | t.Err = KError(kErr) |
| 156 | |
| 157 | return nil |
| 158 | } |