|
| 1 | +use std::io::{Read, Write}; |
| 2 | + |
| 3 | +use crate::codecs::{FromByte, ToByte}; |
| 4 | +use crate::error::{KafkaCode, Result}; |
| 5 | +use crate::utils::TimestampedPartitionOffset; |
| 6 | + |
| 7 | +use super::{HeaderRequest, HeaderResponse, API_KEY_OFFSET}; |
| 8 | + |
| 9 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 10 | +pub enum ListOffsetVersion { |
| 11 | + // currently only support 1 |
| 12 | + V1 = 1, |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +/// https://kafka.apache.org/protocol.html#The_Messages_ListOffsets |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct ListOffsetsRequest<'a> { |
| 19 | + pub header: HeaderRequest<'a>, |
| 20 | + pub replica: i32, |
| 21 | + pub topics: Vec<TopicListOffsetsRequest<'a>>, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug)] |
| 25 | +pub struct TopicListOffsetsRequest<'a> { |
| 26 | + pub topic: &'a str, |
| 27 | + pub partitions: Vec<PartitionListOffsetsRequest>, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Default, Debug)] |
| 31 | +pub struct PartitionListOffsetsRequest { |
| 32 | + pub partition: i32, |
| 33 | + pub time: i64, |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a> ListOffsetsRequest<'a> { |
| 37 | + pub fn new( |
| 38 | + correlation_id: i32, |
| 39 | + version: ListOffsetVersion, |
| 40 | + client_id: &'a str, |
| 41 | + ) -> ListOffsetsRequest<'a> { |
| 42 | + ListOffsetsRequest { |
| 43 | + header: HeaderRequest::new(API_KEY_OFFSET, version as i16, correlation_id, client_id), |
| 44 | + replica: -1, |
| 45 | + topics: vec![], |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn add(&mut self, topic: &'a str, partition: i32, time: i64) { |
| 50 | + for tp in &mut self.topics { |
| 51 | + if tp.topic == topic { |
| 52 | + tp.add(partition, time); |
| 53 | + return; |
| 54 | + } |
| 55 | + } |
| 56 | + let mut tp = TopicListOffsetsRequest::new(topic); |
| 57 | + tp.add(partition, time); |
| 58 | + self.topics.push(tp); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<'a> TopicListOffsetsRequest<'a> { |
| 63 | + fn new(topic: &'a str) -> TopicListOffsetsRequest<'a> { |
| 64 | + TopicListOffsetsRequest { |
| 65 | + topic, |
| 66 | + partitions: vec![], |
| 67 | + } |
| 68 | + } |
| 69 | + fn add(&mut self, partition: i32, time: i64) { |
| 70 | + self.partitions |
| 71 | + .push(PartitionListOffsetsRequest::new(partition, time)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl PartitionListOffsetsRequest { |
| 76 | + fn new(partition: i32, time: i64) -> PartitionListOffsetsRequest { |
| 77 | + PartitionListOffsetsRequest { partition, time } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'a> ToByte for ListOffsetsRequest<'a> { |
| 82 | + fn encode<T: Write>(&self, buffer: &mut T) -> Result<()> { |
| 83 | + try_multi!( |
| 84 | + self.header.encode(buffer), |
| 85 | + self.replica.encode(buffer), |
| 86 | + self.topics.encode(buffer) |
| 87 | + ) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<'a> ToByte for TopicListOffsetsRequest<'a> { |
| 92 | + fn encode<T: Write>(&self, buffer: &mut T) -> Result<()> { |
| 93 | + try_multi!(self.topic.encode(buffer), self.partitions.encode(buffer)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl ToByte for PartitionListOffsetsRequest { |
| 98 | + fn encode<T: Write>(&self, buffer: &mut T) -> Result<()> { |
| 99 | + try_multi!(self.partition.encode(buffer), self.time.encode(buffer)) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// ------------------------------------- |
| 104 | + |
| 105 | +#[derive(Default, Debug)] |
| 106 | +pub struct ListOffsetsResponse { |
| 107 | + pub header: HeaderResponse, |
| 108 | + pub topics: Vec<TopicListOffsetsResponse>, |
| 109 | +} |
| 110 | + |
| 111 | +#[derive(Default, Debug)] |
| 112 | +pub struct TopicListOffsetsResponse { |
| 113 | + pub topic: String, |
| 114 | + pub partitions: Vec<TimestampedPartitionOffsetListOffsetsResponse>, |
| 115 | +} |
| 116 | + |
| 117 | +#[derive(Default, Debug)] |
| 118 | +pub struct TimestampedPartitionOffsetListOffsetsResponse { |
| 119 | + pub partition: i32, |
| 120 | + pub error_code: i16, |
| 121 | + pub timestamp: i64, |
| 122 | + pub offset: i64, |
| 123 | +} |
| 124 | + |
| 125 | +impl TimestampedPartitionOffsetListOffsetsResponse { |
| 126 | + pub fn to_offset(&self) -> std::result::Result<TimestampedPartitionOffset, KafkaCode> { |
| 127 | + match KafkaCode::from_protocol(self.error_code) { |
| 128 | + Some(code) => Err(code), |
| 129 | + None => Ok(TimestampedPartitionOffset { |
| 130 | + partition: self.partition, |
| 131 | + offset: self.offset, |
| 132 | + time: self.timestamp, |
| 133 | + }), |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl FromByte for ListOffsetsResponse { |
| 139 | + type R = ListOffsetsResponse; |
| 140 | + |
| 141 | + #[allow(unused_must_use)] |
| 142 | + fn decode<T: Read>(&mut self, buffer: &mut T) -> Result<()> { |
| 143 | + try_multi!(self.header.decode(buffer), self.topics.decode(buffer)) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl FromByte for TopicListOffsetsResponse { |
| 148 | + type R = TopicListOffsetsResponse; |
| 149 | + |
| 150 | + #[allow(unused_must_use)] |
| 151 | + fn decode<T: Read>(&mut self, buffer: &mut T) -> Result<()> { |
| 152 | + try_multi!(self.topic.decode(buffer), self.partitions.decode(buffer)) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +impl FromByte for TimestampedPartitionOffsetListOffsetsResponse { |
| 157 | + type R = TimestampedPartitionOffsetListOffsetsResponse; |
| 158 | + |
| 159 | + #[allow(unused_must_use)] |
| 160 | + fn decode<T: Read>(&mut self, buffer: &mut T) -> Result<()> { |
| 161 | + try_multi!( |
| 162 | + self.partition.decode(buffer), |
| 163 | + self.error_code.decode(buffer), |
| 164 | + self.timestamp.decode(buffer), |
| 165 | + self.offset.decode(buffer) |
| 166 | + ) |
| 167 | + } |
| 168 | +} |
0 commit comments