Skip to main content

kafka_client_engine/consumer/assigned_host/delivery/
record.rs

1//! Borrowed flattened views over normalized application record descriptors.
2
3use crate::protocol::fetch::{FetchBatch, FetchHeader, FetchRecord};
4
5use super::AssignedConsumerDelivery;
6
7/// Ordered borrowed application records from one direct-consumer delivery.
8#[derive(Debug)]
9pub struct AssignedConsumerRecords<'batch> {
10    topic: &'batch str,
11    partition: i32,
12    batches: &'batch [FetchBatch],
13    batch_index: usize,
14    record_index: usize,
15}
16
17impl<'batch> AssignedConsumerRecords<'batch> {
18    pub(super) fn new(delivery: &'batch AssignedConsumerDelivery) -> Self {
19        let batches = delivery
20            .lease()
21            .outcome()
22            .outcome()
23            .data_batches()
24            .unwrap_or_default();
25        Self {
26            topic: delivery.topic(),
27            partition: delivery.partition(),
28            batches,
29            batch_index: 0,
30            record_index: 0,
31        }
32    }
33}
34
35impl<'batch> Iterator for AssignedConsumerRecords<'batch> {
36    type Item = AssignedConsumerRecord<'batch>;
37
38    fn next(&mut self) -> Option<Self::Item> {
39        loop {
40            let batch = self.batches.get(self.batch_index)?;
41            if batch.is_control {
42                self.batch_index += 1;
43                self.record_index = 0;
44                continue;
45            }
46            if let Some(record) = batch.records.get(self.record_index) {
47                self.record_index += 1;
48                return Some(AssignedConsumerRecord {
49                    topic: self.topic,
50                    partition: self.partition,
51                    record,
52                });
53            }
54            self.batch_index += 1;
55            self.record_index = 0;
56        }
57    }
58}
59
60/// One record borrowing bytes from its owning assigned-consumer batch.
61#[derive(Debug)]
62pub struct AssignedConsumerRecord<'batch> {
63    topic: &'batch str,
64    partition: i32,
65    record: &'batch FetchRecord,
66}
67
68impl AssignedConsumerRecord<'_> {
69    /// Returns the retained Kafka topic name.
70    pub const fn topic(&self) -> &str {
71        self.topic
72    }
73
74    /// Returns the zero-based Kafka partition.
75    pub const fn partition(&self) -> i32 {
76        self.partition
77    }
78
79    /// Returns the absolute Kafka log offset.
80    pub const fn offset(&self) -> i64 {
81        self.record.offset
82    }
83
84    /// Returns the Kafka timestamp in milliseconds when present.
85    pub const fn timestamp_millis(&self) -> Option<i64> {
86        self.record.timestamp
87    }
88
89    /// Returns the nullable key without conflating null and empty bytes.
90    pub fn key(&self) -> Option<&[u8]> {
91        self.record.key.as_deref()
92    }
93
94    /// Returns the nullable value without conflating tombstones and empty bytes.
95    pub fn value(&self) -> Option<&[u8]> {
96        self.record.value.as_deref()
97    }
98
99    /// Returns duplicate-preserving headers in broker order.
100    pub fn headers(&self) -> impl ExactSizeIterator<Item = AssignedConsumerHeader<'_>> {
101        self.record
102            .headers
103            .iter()
104            .map(|header| AssignedConsumerHeader { header })
105    }
106}
107
108/// One borrowed Kafka header retaining null-versus-empty value semantics.
109#[derive(Debug)]
110pub struct AssignedConsumerHeader<'record> {
111    header: &'record FetchHeader,
112}
113
114impl AssignedConsumerHeader<'_> {
115    /// Returns the header key bytes.
116    pub fn key(&self) -> &[u8] {
117        &self.header.key
118    }
119
120    /// Returns the nullable header value bytes.
121    pub fn value(&self) -> Option<&[u8]> {
122        self.header.value.as_deref()
123    }
124}