use crate::protocol::fetch::{FetchBatch, FetchHeader, FetchRecord};
use super::AssignedConsumerDelivery;
#[derive(Debug)]
pub struct AssignedConsumerRecords<'batch> {
topic: &'batch str,
partition: i32,
batches: &'batch [FetchBatch],
batch_index: usize,
record_index: usize,
}
impl<'batch> AssignedConsumerRecords<'batch> {
pub(super) fn new(delivery: &'batch AssignedConsumerDelivery) -> Self {
let batches = delivery
.lease()
.outcome()
.outcome()
.data_batches()
.unwrap_or_default();
Self {
topic: delivery.topic(),
partition: delivery.partition(),
batches,
batch_index: 0,
record_index: 0,
}
}
}
impl<'batch> Iterator for AssignedConsumerRecords<'batch> {
type Item = AssignedConsumerRecord<'batch>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let batch = self.batches.get(self.batch_index)?;
if batch.is_control {
self.batch_index += 1;
self.record_index = 0;
continue;
}
if let Some(record) = batch.records.get(self.record_index) {
self.record_index += 1;
return Some(AssignedConsumerRecord {
topic: self.topic,
partition: self.partition,
record,
});
}
self.batch_index += 1;
self.record_index = 0;
}
}
}
#[derive(Debug)]
pub struct AssignedConsumerRecord<'batch> {
topic: &'batch str,
partition: i32,
record: &'batch FetchRecord,
}
impl AssignedConsumerRecord<'_> {
pub const fn topic(&self) -> &str {
self.topic
}
pub const fn partition(&self) -> i32 {
self.partition
}
pub const fn offset(&self) -> i64 {
self.record.offset
}
pub const fn timestamp_millis(&self) -> Option<i64> {
self.record.timestamp
}
pub fn key(&self) -> Option<&[u8]> {
self.record.key.as_deref()
}
pub fn value(&self) -> Option<&[u8]> {
self.record.value.as_deref()
}
pub fn headers(&self) -> impl ExactSizeIterator<Item = AssignedConsumerHeader<'_>> {
self.record
.headers
.iter()
.map(|header| AssignedConsumerHeader { header })
}
}
#[derive(Debug)]
pub struct AssignedConsumerHeader<'record> {
header: &'record FetchHeader,
}
impl AssignedConsumerHeader<'_> {
pub fn key(&self) -> &[u8] {
&self.header.key
}
pub fn value(&self) -> Option<&[u8]> {
self.header.value.as_deref()
}
}