William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // request message format is: |
| 9 | // [topic] timeout(int32) |
| 10 | // where topic is: |
| 11 | // name(string) [partition] |
| 12 | // where partition is: |
| 13 | // id(int32) offset(int64) |
| 14 | |
| 15 | type DeleteRecordsRequest struct { |
| 16 | Topics map[string]*DeleteRecordsRequestTopic |
| 17 | Timeout time.Duration |
| 18 | } |
| 19 | |
| 20 | func (d *DeleteRecordsRequest) encode(pe packetEncoder) error { |
| 21 | if err := pe.putArrayLength(len(d.Topics)); err != nil { |
| 22 | return err |
| 23 | } |
| 24 | keys := make([]string, 0, len(d.Topics)) |
| 25 | for topic := range d.Topics { |
| 26 | keys = append(keys, topic) |
| 27 | } |
| 28 | sort.Strings(keys) |
| 29 | for _, topic := range keys { |
| 30 | if err := pe.putString(topic); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | if err := d.Topics[topic].encode(pe); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | } |
| 37 | pe.putInt32(int32(d.Timeout / time.Millisecond)) |
| 38 | |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | func (d *DeleteRecordsRequest) decode(pd packetDecoder, version int16) error { |
| 43 | n, err := pd.getArrayLength() |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | if n > 0 { |
| 49 | d.Topics = make(map[string]*DeleteRecordsRequestTopic, n) |
| 50 | for i := 0; i < n; i++ { |
| 51 | topic, err := pd.getString() |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | details := new(DeleteRecordsRequestTopic) |
| 56 | if err = details.decode(pd, version); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | d.Topics[topic] = details |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | timeout, err := pd.getInt32() |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | d.Timeout = time.Duration(timeout) * time.Millisecond |
| 68 | |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | func (d *DeleteRecordsRequest) key() int16 { |
| 73 | return 21 |
| 74 | } |
| 75 | |
| 76 | func (d *DeleteRecordsRequest) version() int16 { |
| 77 | return 0 |
| 78 | } |
| 79 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 80 | func (d *DeleteRecordsRequest) headerVersion() int16 { |
| 81 | return 1 |
| 82 | } |
| 83 | |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 84 | func (d *DeleteRecordsRequest) requiredVersion() KafkaVersion { |
| 85 | return V0_11_0_0 |
| 86 | } |
| 87 | |
| 88 | type DeleteRecordsRequestTopic struct { |
| 89 | PartitionOffsets map[int32]int64 // partition => offset |
| 90 | } |
| 91 | |
| 92 | func (t *DeleteRecordsRequestTopic) encode(pe packetEncoder) error { |
| 93 | if err := pe.putArrayLength(len(t.PartitionOffsets)); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | keys := make([]int32, 0, len(t.PartitionOffsets)) |
| 97 | for partition := range t.PartitionOffsets { |
| 98 | keys = append(keys, partition) |
| 99 | } |
| 100 | sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) |
| 101 | for _, partition := range keys { |
| 102 | pe.putInt32(partition) |
| 103 | pe.putInt64(t.PartitionOffsets[partition]) |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | func (t *DeleteRecordsRequestTopic) decode(pd packetDecoder, version int16) error { |
| 109 | n, err := pd.getArrayLength() |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | |
| 114 | if n > 0 { |
| 115 | t.PartitionOffsets = make(map[int32]int64, n) |
| 116 | for i := 0; i < n; i++ { |
| 117 | partition, err := pd.getInt32() |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | offset, err := pd.getInt64() |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | t.PartitionOffsets[partition] = offset |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return nil |
| 130 | } |