use bytes::BufMut;
use crate::primitives::fixed::{get_i32, get_i64, get_i8, put_i32, put_i64, put_i8};
use crate::primitives::string_bytes::{
compact_nullable_string_len, compact_string_len, nullable_string_len,
put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
string_len,
};
use crate::primitives::string_bytes_borrowed::{
get_compact_nullable_string_borrowed, get_compact_string_borrowed,
get_nullable_string_borrowed, get_string_borrowed,
};
use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
use crate::{Decode, DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
pub const API_KEY: i16 = 1;
pub const MIN_VERSION: i16 = 4;
pub const MAX_VERSION: i16 = 18;
pub const FLEXIBLE_MIN: i16 = 12;
#[inline]
fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchRequest<'a> {
pub replica_id: i32,
pub max_wait_ms: i32,
pub min_bytes: i32,
pub max_bytes: i32,
pub isolation_level: i8,
pub session_id: i32,
pub session_epoch: i32,
pub topics: Vec<FetchTopic<'a>>,
pub forgotten_topics_data: Vec<ForgottenTopic<'a>>,
pub rack_id: &'a str,
pub cluster_id: Option<String>,
pub replica_state: ReplicaState,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for FetchRequest<'a> {
fn default() -> Self {
Self {
replica_id: -1i32,
max_wait_ms: 0i32,
min_bytes: 0i32,
max_bytes: 2_147_483_647i32,
isolation_level: 0i8,
session_id: 0i32,
session_epoch: -1i32,
topics: Vec::new(),
forgotten_topics_data: Vec::new(),
rack_id: "",
cluster_id: None,
replica_state: Default::default(),
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> FetchRequest<'a> {
pub fn to_owned(&self) -> crate::owned::fetch_request::FetchRequest {
crate::owned::fetch_request::FetchRequest {
replica_id: (self.replica_id),
max_wait_ms: (self.max_wait_ms),
min_bytes: (self.min_bytes),
max_bytes: (self.max_bytes),
isolation_level: (self.isolation_level),
session_id: (self.session_id),
session_epoch: (self.session_epoch),
topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
forgotten_topics_data: (self.forgotten_topics_data).iter().map(|it| it.to_owned()).collect(),
rack_id: (self.rack_id).to_string(),
cluster_id: self.cluster_id.clone(),
replica_state: (self.replica_state).to_owned(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for FetchRequest<'a> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
}
let flex = is_flexible(version);
if version >= 0 && version <= 14 { put_i32(buf, self.replica_id) }
if version >= 0 { put_i32(buf, self.max_wait_ms) }
if version >= 0 { put_i32(buf, self.min_bytes) }
if version >= 3 { put_i32(buf, self.max_bytes) }
if version >= 4 { put_i8(buf, self.isolation_level) }
if version >= 7 { put_i32(buf, self.session_id) }
if version >= 7 { put_i32(buf, self.session_epoch) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
if version >= 7 { { crate::primitives::array::put_array_len(buf, (self.forgotten_topics_data).len(), flex); for it in &self.forgotten_topics_data { it.encode(buf, version)?; } } }
if version >= 11 { if flex { put_compact_string(buf, self.rack_id) } else { put_string(buf, self.rack_id) } }
if flex {
let mut tagged = WriteTaggedFields::new();
if !(self.cluster_id.is_none()) {
let payload = encode_to_bytes(if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }, |b| { if flex { put_compact_nullable_string(b, self.cluster_id.as_deref()) } else { put_nullable_string(b, self.cluster_id.as_deref()) }; Ok(()) });
tagged.add(0, payload);
}
if !(crate::codegen_helpers::is_default(&self.replica_state)) {
let payload = encode_to_bytes(self.replica_state.encoded_len(version), |b| { self.replica_state.encode(b, version)?; Ok(()) });
tagged.add(1, payload);
}
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = is_flexible(version);
let mut n: usize = 0;
if version >= 0 && version <= 14 { n += 4; }
if version >= 0 { n += 4; }
if version >= 0 { n += 4; }
if version >= 3 { n += 4; }
if version >= 4 { n += 1; }
if version >= 7 { n += 4; }
if version >= 7 { n += 4; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.forgotten_topics_data).len(), flex); let body: usize = (self.forgotten_topics_data).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if version >= 11 { n += if flex { compact_string_len(self.rack_id) } else { string_len(self.rack_id) }; }
if flex {
let mut known_pairs: Vec<(u32, usize)> = Vec::new();
if !(self.cluster_id.is_none()) {
known_pairs.push((0, if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }));
}
if !(crate::codegen_helpers::is_default(&self.replica_state)) {
known_pairs.push((1, self.replica_state.encoded_len(version)));
}
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for FetchRequest<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
}
let flex = is_flexible(version);
let mut out = Self::default();
if version >= 0 && version <= 14 { out.replica_id = get_i32(buf)?; }
if version >= 0 { out.max_wait_ms = get_i32(buf)?; }
if version >= 0 { out.min_bytes = get_i32(buf)?; }
if version >= 3 { out.max_bytes = get_i32(buf)?; }
if version >= 4 { out.isolation_level = get_i8(buf)?; }
if version >= 7 { out.session_id = get_i32(buf)?; }
if version >= 7 { out.session_epoch = get_i32(buf)?; }
if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(FetchTopic::decode_borrow(buf, version)?); } v }; }
if version >= 7 { out.forgotten_topics_data = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(ForgottenTopic::decode_borrow(buf, version)?); } v }; }
if version >= 11 { out.rack_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if flex {
let mut tag_cluster_id = None;
let mut tag_replica_state = None;
out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
match tag {
0 => { tag_cluster_id = Some({ let b: &mut &[u8] = payload; if flex { crate::primitives::string_bytes::get_compact_nullable_string_owned(b)? } else { crate::primitives::string_bytes::get_nullable_string_owned(b)? } }); Ok(true) }
1 => { tag_replica_state = Some({ let b: &mut &[u8] = payload; ReplicaState::decode_borrow(b, version)? }); Ok(true) }
_ => Ok(false),
}
})?;
if let Some(v) = tag_cluster_id { out.cluster_id = v; }
if let Some(v) = tag_replica_state { out.replica_state = v; }
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicaState {
pub replica_id: i32,
pub replica_epoch: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for ReplicaState {
fn default() -> Self {
Self {
replica_id: -1i32,
replica_epoch: -1i64,
unknown_tagged_fields: Default::default(),
}
}
}
impl ReplicaState {
pub fn to_owned(&self) -> crate::owned::fetch_request::ReplicaState {
crate::owned::fetch_request::ReplicaState {
replica_id: (self.replica_id),
replica_epoch: (self.replica_epoch),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for ReplicaState {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 15 { put_i32(buf, self.replica_id) }
if version >= 15 { put_i64(buf, self.replica_epoch) }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 15 { n += 4; }
if version >= 15 { n += 8; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for ReplicaState {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 15 { out.replica_id = get_i32(buf)?; }
if version >= 15 { out.replica_epoch = get_i64(buf)?; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchTopic<'a> {
pub topic: &'a str,
pub topic_id: crate::primitives::uuid::Uuid,
pub partitions: Vec<FetchPartition>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for FetchTopic<'a> {
fn default() -> Self {
Self {
topic: "",
topic_id: Default::default(),
partitions: Vec::new(),
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> FetchTopic<'a> {
pub fn to_owned(&self) -> crate::owned::fetch_request::FetchTopic {
crate::owned::fetch_request::FetchTopic {
topic: (self.topic).to_string(),
topic_id: (self.topic_id),
partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for FetchTopic<'a> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 0 && version <= 12 { if flex { put_compact_string(buf, self.topic) } else { put_string(buf, self.topic) } }
if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 0 && version <= 12 { n += if flex { compact_string_len(self.topic) } else { string_len(self.topic) }; }
if version >= 13 { n += 16; }
if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for FetchTopic<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 0 && version <= 12 { out.topic = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(FetchPartition::decode_borrow(buf, version)?); } v }; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchPartition {
pub partition: i32,
pub current_leader_epoch: i32,
pub fetch_offset: i64,
pub last_fetched_epoch: i32,
pub log_start_offset: i64,
pub partition_max_bytes: i32,
pub replica_directory_id: crate::primitives::uuid::Uuid,
pub high_watermark: i64,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl Default for FetchPartition {
fn default() -> Self {
Self {
partition: 0i32,
current_leader_epoch: -1i32,
fetch_offset: 0i64,
last_fetched_epoch: -1i32,
log_start_offset: -1i64,
partition_max_bytes: 0i32,
replica_directory_id: Default::default(),
high_watermark: 9_223_372_036_854_775_807i64,
unknown_tagged_fields: Default::default(),
}
}
}
impl FetchPartition {
pub fn to_owned(&self) -> crate::owned::fetch_request::FetchPartition {
crate::owned::fetch_request::FetchPartition {
partition: (self.partition),
current_leader_epoch: (self.current_leader_epoch),
fetch_offset: (self.fetch_offset),
last_fetched_epoch: (self.last_fetched_epoch),
log_start_offset: (self.log_start_offset),
partition_max_bytes: (self.partition_max_bytes),
replica_directory_id: (self.replica_directory_id),
high_watermark: (self.high_watermark),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl Encode for FetchPartition {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 0 { put_i32(buf, self.partition) }
if version >= 9 { put_i32(buf, self.current_leader_epoch) }
if version >= 0 { put_i64(buf, self.fetch_offset) }
if version >= 12 { put_i32(buf, self.last_fetched_epoch) }
if version >= 5 { put_i64(buf, self.log_start_offset) }
if version >= 0 { put_i32(buf, self.partition_max_bytes) }
if flex {
let mut tagged = WriteTaggedFields::new();
if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
let payload = encode_to_bytes(16, |b| { crate::primitives::uuid::put_uuid(b, self.replica_directory_id); Ok(()) });
tagged.add(0, payload);
}
if !(self.high_watermark == 9_223_372_036_854_775_807i64) {
let payload = encode_to_bytes(8, |b| { put_i64(b, self.high_watermark); Ok(()) });
tagged.add(1, payload);
}
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 0 { n += 4; }
if version >= 9 { n += 4; }
if version >= 0 { n += 8; }
if version >= 12 { n += 4; }
if version >= 5 { n += 8; }
if version >= 0 { n += 4; }
if flex {
let mut known_pairs: Vec<(u32, usize)> = Vec::new();
if !(crate::codegen_helpers::is_default(&self.replica_directory_id)) {
known_pairs.push((0, 16));
}
if !(self.high_watermark == 9_223_372_036_854_775_807i64) {
known_pairs.push((1, 8));
}
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for FetchPartition {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 0 { out.partition = get_i32(buf)?; }
if version >= 9 { out.current_leader_epoch = get_i32(buf)?; }
if version >= 0 { out.fetch_offset = get_i64(buf)?; }
if version >= 12 { out.last_fetched_epoch = get_i32(buf)?; }
if version >= 5 { out.log_start_offset = get_i64(buf)?; }
if version >= 0 { out.partition_max_bytes = get_i32(buf)?; }
if flex {
let mut tag_replica_directory_id = None;
let mut tag_high_watermark = None;
out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
match tag {
0 => { tag_replica_directory_id = Some({ let b: &mut &[u8] = payload; crate::primitives::uuid::get_uuid(b)? }); Ok(true) }
1 => { tag_high_watermark = Some({ let b: &mut &[u8] = payload; get_i64(b)? }); Ok(true) }
_ => Ok(false),
}
})?;
if let Some(v) = tag_replica_directory_id { out.replica_directory_id = v; }
if let Some(v) = tag_high_watermark { out.high_watermark = v; }
}
Ok(out)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForgottenTopic<'a> {
pub topic: &'a str,
pub topic_id: crate::primitives::uuid::Uuid,
pub partitions: Vec<i32>,
pub unknown_tagged_fields: UnknownTaggedFields,
}
impl<'a> Default for ForgottenTopic<'a> {
fn default() -> Self {
Self {
topic: "",
topic_id: Default::default(),
partitions: Vec::new(),
unknown_tagged_fields: Default::default(),
}
}
}
impl<'a> ForgottenTopic<'a> {
pub fn to_owned(&self) -> crate::owned::fetch_request::ForgottenTopic {
crate::owned::fetch_request::ForgottenTopic {
topic: (self.topic).to_string(),
topic_id: (self.topic_id),
partitions: (self.partitions).clone(),
unknown_tagged_fields: self.unknown_tagged_fields.clone(),
}
}
}
impl<'a> Encode for ForgottenTopic<'a> {
fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
let flex = version >= 12;
if version >= 7 && version <= 12 { if flex { put_compact_string(buf, self.topic) } else { put_string(buf, self.topic) } }
if version >= 13 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
if version >= 7 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
if flex {
let tagged = WriteTaggedFields::new();
tagged.write(buf, &self.unknown_tagged_fields);
}
Ok(())
}
fn encoded_len(&self, version: i16) -> usize {
let flex = version >= 12;
let mut n: usize = 0;
if version >= 7 && version <= 12 { n += if flex { compact_string_len(self.topic) } else { string_len(self.topic) }; }
if version >= 13 { n += 16; }
if version >= 7 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|_| 4).sum(); prefix + body }; }
if flex {
let known_pairs: Vec<(u32, usize)> = Vec::new();
n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
}
n
}
}
impl<'de> DecodeBorrow<'de> for ForgottenTopic<'de> {
fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
let flex = version >= 12;
let mut out = Self::default();
if version >= 7 && version <= 12 { out.topic = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
if version >= 13 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
if version >= 7 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
if flex {
out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
Ok(false)
})?;
}
Ok(out)
}
}