1use bytes::{Buf, BufMut, Bytes, BytesMut};
4use zerocopy::FromBytes as _;
5
6use crate::primitives::varint::{
7 get_varint, get_varlong, put_varint, put_varlong, varint_len, varlong_len,
8};
9use crate::records::RecordsError;
10use crate::records::crc::{crc32c, crc32c_append};
11use crate::records::header::{Attributes, HEADER_LEN};
12
13#[derive(Debug, Clone, PartialEq, Eq, Default)]
14pub struct RecordHeader {
15 pub key: String,
16 pub value: Option<Bytes>,
17}
18
19#[derive(Debug, Clone, PartialEq, Eq, Default)]
20pub struct Record {
21 pub attributes: i8,
22 pub timestamp_delta: i64,
23 pub offset_delta: i32,
24 pub key: Option<Bytes>,
25 pub value: Option<Bytes>,
26 pub headers: Vec<RecordHeader>,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct RecordBatch {
31 pub base_offset: i64,
32 pub partition_leader_epoch: i32,
33 pub attributes: Attributes,
34 pub last_offset_delta: i32,
35 pub base_timestamp: i64,
36 pub max_timestamp: i64,
37 pub producer_id: i64,
38 pub producer_epoch: i16,
39 pub base_sequence: i32,
40 pub records: Vec<Record>,
41}
42
43impl Default for RecordBatch {
44 fn default() -> Self {
45 Self {
46 base_offset: 0,
47 partition_leader_epoch: 0,
48 attributes: Attributes::default(),
49 last_offset_delta: 0,
50 base_timestamp: 0,
51 max_timestamp: 0,
52 producer_id: -1, producer_epoch: -1,
54 base_sequence: -1,
55 records: Vec::new(),
56 }
57 }
58}
59
60impl Record {
61 pub fn encode<B: BufMut>(&self, buf: &mut B) -> Result<(), RecordsError> {
63 let body_len = self.body_len();
64 put_varlong(
65 buf,
66 i64::try_from(body_len)
67 .map_err(|_| RecordsError::RecordParse("record body length overflow".into()))?,
68 );
69 self.encode_body(buf)
70 }
71
72 pub fn encoded_len(&self) -> usize {
74 let body = self.body_len();
75 #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
76 let body_i64 = body as i64;
77 varlong_len(body_i64) + body
78 }
79
80 fn body_len(&self) -> usize {
81 let mut n = 1; n += varlong_len(self.timestamp_delta);
83 n += varint_len(self.offset_delta);
84 n += match &self.key {
85 None => varint_len(-1),
86 Some(k) => varint_len(i32::try_from(k.len()).unwrap_or(i32::MAX)) + k.len(),
87 };
88 n += match &self.value {
89 None => varint_len(-1),
90 Some(v) => varint_len(i32::try_from(v.len()).unwrap_or(i32::MAX)) + v.len(),
91 };
92 n += varint_len(i32::try_from(self.headers.len()).unwrap_or(i32::MAX));
93 for h in &self.headers {
94 let key_bytes = h.key.as_bytes();
95 n += varint_len(i32::try_from(key_bytes.len()).unwrap_or(i32::MAX)) + key_bytes.len();
96 n += match &h.value {
97 None => varint_len(-1),
98 Some(v) => varint_len(i32::try_from(v.len()).unwrap_or(i32::MAX)) + v.len(),
99 };
100 }
101 n
102 }
103
104 fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<(), RecordsError> {
105 buf.put_i8(self.attributes);
106 put_varlong(buf, self.timestamp_delta);
107 put_varint(buf, self.offset_delta);
108 match &self.key {
109 None => put_varint(buf, -1),
110 Some(k) => {
111 put_varint(
112 buf,
113 i32::try_from(k.len()).map_err(|_| {
114 RecordsError::RecordParse("record key length overflow".into())
115 })?,
116 );
117 buf.put_slice(k);
118 }
119 }
120 match &self.value {
121 None => put_varint(buf, -1),
122 Some(v) => {
123 put_varint(
124 buf,
125 i32::try_from(v.len()).map_err(|_| {
126 RecordsError::RecordParse("record value length overflow".into())
127 })?,
128 );
129 buf.put_slice(v);
130 }
131 }
132 put_varint(
133 buf,
134 i32::try_from(self.headers.len())
135 .map_err(|_| RecordsError::RecordParse("record header count overflow".into()))?,
136 );
137 for h in &self.headers {
138 let key_bytes = h.key.as_bytes();
139 put_varint(
140 buf,
141 i32::try_from(key_bytes.len())
142 .map_err(|_| RecordsError::RecordParse("header key length overflow".into()))?,
143 );
144 buf.put_slice(key_bytes);
145 match &h.value {
146 None => put_varint(buf, -1),
147 Some(v) => {
148 put_varint(
149 buf,
150 i32::try_from(v.len()).map_err(|_| {
151 RecordsError::RecordParse("header value length overflow".into())
152 })?,
153 );
154 buf.put_slice(v);
155 }
156 }
157 }
158 Ok(())
159 }
160
161 pub fn decode<B: Buf>(buf: &mut B) -> Result<Self, RecordsError> {
164 let body_len = get_varlong(buf)
165 .map_err(|e| RecordsError::RecordParse(format!("record length: {e}")))?;
166 let body_len = usize::try_from(body_len).map_err(|_| {
167 RecordsError::RecordParse(format!("record length negative or too large: {body_len}"))
168 })?;
169 if buf.remaining() < body_len {
170 return Err(RecordsError::BodyTooShort {
171 needed: body_len - buf.remaining(),
172 });
173 }
174 let mut body = buf.take(body_len);
177 let r = Self::decode_body(&mut body)?;
178 if body.has_remaining() {
180 return Err(RecordsError::RecordParse(format!(
181 "trailing bytes inside record (left={})",
182 body.remaining()
183 )));
184 }
185 Ok(r)
186 }
187
188 fn decode_body<B: Buf>(buf: &mut B) -> Result<Self, RecordsError> {
189 if buf.remaining() == 0 {
190 return Err(RecordsError::RecordParse("record body empty".into()));
191 }
192 let attributes = buf.get_i8();
193 let timestamp_delta = get_varlong(buf)
194 .map_err(|e| RecordsError::RecordParse(format!("timestamp_delta: {e}")))?;
195 let offset_delta =
196 get_varint(buf).map_err(|e| RecordsError::RecordParse(format!("offset_delta: {e}")))?;
197
198 let key = decode_nullable_bytes(buf, "key")?;
199 let value = decode_nullable_bytes(buf, "value")?;
200
201 let header_count =
202 get_varint(buf).map_err(|e| RecordsError::RecordParse(format!("header_count: {e}")))?;
203 if header_count < 0 {
204 return Err(RecordsError::RecordParse(format!(
205 "negative header count {header_count}"
206 )));
207 }
208 #[allow(clippy::cast_sign_loss)] let header_count_usize = header_count as usize;
210 let mut headers = Vec::with_capacity(header_count_usize);
211 for i in 0..header_count {
212 headers.push(
213 decode_record_header(buf)
214 .map_err(|e| RecordsError::RecordParse(format!("header[{i}]: {e}")))?,
215 );
216 }
217
218 Ok(Self {
219 attributes,
220 timestamp_delta,
221 offset_delta,
222 key,
223 value,
224 headers,
225 })
226 }
227}
228
229fn decode_nullable_bytes<B: Buf>(buf: &mut B, label: &str) -> Result<Option<Bytes>, RecordsError> {
230 let len =
231 get_varint(buf).map_err(|e| RecordsError::RecordParse(format!("{label} length: {e}")))?;
232 if len < 0 {
233 Ok(None)
234 } else {
235 #[allow(clippy::cast_sign_loss)] let n = len as usize;
237 if buf.remaining() < n {
238 return Err(RecordsError::BodyTooShort {
239 needed: n - buf.remaining(),
240 });
241 }
242 let mut v = vec![0u8; n];
243 buf.copy_to_slice(&mut v);
244 Ok(Some(Bytes::from(v)))
245 }
246}
247
248fn decode_record_header<B: Buf>(buf: &mut B) -> Result<RecordHeader, String> {
249 let key_len = get_varint(buf).map_err(|e| format!("key length: {e}"))?;
250 if key_len < 0 {
251 return Err(format!("non-nullable key has negative length {key_len}"));
252 }
253 #[allow(clippy::cast_sign_loss)] let n = key_len as usize;
255 if buf.remaining() < n {
256 return Err(format!("key truncated (need {} more)", n - buf.remaining()));
257 }
258 let mut kv = vec![0u8; n];
259 buf.copy_to_slice(&mut kv);
260 let key = String::from_utf8(kv).map_err(|e| format!("key utf-8: {e}"))?;
261
262 let value_len = get_varint(buf).map_err(|e| format!("value length: {e}"))?;
263 let value = if value_len < 0 {
264 None
265 } else {
266 #[allow(clippy::cast_sign_loss)] let n = value_len as usize;
268 if buf.remaining() < n {
269 return Err(format!(
270 "value truncated (need {} more)",
271 n - buf.remaining()
272 ));
273 }
274 let mut vv = vec![0u8; n];
275 buf.copy_to_slice(&mut vv);
276 Some(Bytes::from(vv))
277 };
278
279 Ok(RecordHeader { key, value })
280}
281
282#[cfg(test)]
283mod record_tests {
284 use super::*;
285 use bytes::BytesMut;
286
287 fn fixture_minimal_record() -> Record {
288 Record {
289 attributes: 0,
290 timestamp_delta: 0,
291 offset_delta: 0,
292 key: None,
293 value: None,
294 headers: vec![],
295 }
296 }
297
298 fn fixture_keyed_record() -> Record {
299 Record {
300 attributes: 0,
301 timestamp_delta: 17,
302 offset_delta: 2,
303 key: Some(Bytes::from_static(b"the-key")),
304 value: Some(Bytes::from_static(b"hello kafka")),
305 headers: vec![
306 RecordHeader {
307 key: "trace-id".to_string(),
308 value: Some(Bytes::from_static(b"abc")),
309 },
310 RecordHeader {
311 key: "null-val".to_string(),
312 value: None,
313 },
314 ],
315 }
316 }
317
318 fn fixture_large_payload_record() -> Record {
319 Record {
320 attributes: 0,
321 timestamp_delta: 1_000_000,
322 offset_delta: 999,
323 key: Some(Bytes::from(vec![b'k'; 128])),
324 value: Some(Bytes::from(vec![b'v'; 4096])),
325 headers: vec![],
326 }
327 }
328
329 macro_rules! roundtrip {
330 ($name:ident, $fixture:ident) => {
331 #[test]
332 fn $name() {
333 let r = $fixture();
334 let mut buf = BytesMut::new();
335 r.encode(&mut buf).unwrap();
336 assert_eq!(buf.len(), r.encoded_len(), "predicted len mismatch");
337
338 let mut cur: &[u8] = &buf[..];
339 let decoded = Record::decode(&mut cur).unwrap();
340 assert_eq!(decoded, r);
341 assert!(cur.is_empty(), "trailing bytes after decode");
342 }
343 };
344 }
345
346 roundtrip!(minimal, fixture_minimal_record);
347 roundtrip!(keyed_with_headers, fixture_keyed_record);
348 roundtrip!(large_payload, fixture_large_payload_record);
349
350 #[test]
351 fn decode_rejects_negative_header_count() {
352 let mut buf = BytesMut::new();
353 put_varlong(&mut buf, 6); buf.put_i8(0); put_varlong(&mut buf, 0); put_varint(&mut buf, 0); put_varint(&mut buf, -1); put_varint(&mut buf, -1); put_varint(&mut buf, -1); let mut cur: &[u8] = &buf[..];
364 match Record::decode(&mut cur) {
365 Err(RecordsError::RecordParse(msg)) => {
366 assert!(msg.contains("negative header count"), "got: {msg}");
367 }
368 other => panic!("expected RecordParse, got {other:?}"),
369 }
370 }
371}
372
373impl RecordBatch {
374 pub fn decode<B: Buf>(buf: &mut B) -> Result<Self, RecordsError> {
377 const HEADER_TAIL_LEN: i32 = 49;
383
384 if buf.remaining() < HEADER_LEN {
386 return Err(RecordsError::HeaderTooShort {
387 needed: HEADER_LEN - buf.remaining(),
388 });
389 }
390 let mut hdr_bytes = [0u8; HEADER_LEN];
392 buf.copy_to_slice(&mut hdr_bytes);
393
394 let hdr = crate::records::header::RecordBatchHeader::ref_from_bytes(&hdr_bytes[..])
395 .map_err(|_| RecordsError::ZerocopyFailure)?;
396
397 if hdr.magic != 2 {
398 return Err(RecordsError::UnsupportedMagic { found: hdr.magic });
399 }
400
401 let body_len = i32::checked_sub(hdr.batch_length.get(), HEADER_TAIL_LEN)
403 .and_then(|n| usize::try_from(n).ok())
404 .ok_or_else(|| {
405 RecordsError::RecordParse("negative or oversized batch_length".into())
406 })?;
407
408 if buf.remaining() < body_len {
409 return Err(RecordsError::BodyTooShort {
410 needed: body_len - buf.remaining(),
411 });
412 }
413
414 let mut body = vec![0u8; body_len];
416 buf.copy_to_slice(&mut body);
417
418 let expected_crc = hdr.crc.get();
421 let mut computed = crc32c(&hdr_bytes[21..HEADER_LEN]);
422 computed = crc32c_append(computed, &body);
423 if computed != expected_crc {
424 return Err(RecordsError::CrcMismatch {
425 expected: expected_crc,
426 computed,
427 });
428 }
429
430 let attributes = Attributes(hdr.attributes.get());
431 let codec = attributes.compression();
432
433 let body_for_records: Bytes = if codec == crabka_compression::CompressionType::None {
435 Bytes::from(body)
436 } else {
437 crabka_compression::decompress(codec, &body)?
438 };
439
440 let count = hdr.records_count.get();
442 if count < 0 {
443 return Err(RecordsError::RecordParse(format!(
444 "negative records_count {count}"
445 )));
446 }
447 let mut body_cur: &[u8] = &body_for_records[..];
448 #[allow(clippy::cast_sign_loss)] let mut records = Vec::with_capacity(count as usize);
450 for i in 0..count {
451 records.push(
452 Record::decode(&mut body_cur)
453 .map_err(|e| RecordsError::RecordParse(format!("record[{i}]: {e}")))?,
454 );
455 }
456 if !body_cur.is_empty() {
457 return Err(RecordsError::RecordParse(format!(
458 "trailing bytes after records (left={})",
459 body_cur.len()
460 )));
461 }
462
463 Ok(Self {
464 base_offset: hdr.base_offset.get(),
465 partition_leader_epoch: hdr.partition_leader_epoch.get(),
466 attributes,
467 last_offset_delta: hdr.last_offset_delta.get(),
468 base_timestamp: hdr.base_timestamp.get(),
469 max_timestamp: hdr.max_timestamp.get(),
470 producer_id: hdr.producer_id.get(),
471 producer_epoch: hdr.producer_epoch.get(),
472 base_sequence: hdr.base_sequence.get(),
473 records,
474 })
475 }
476
477 pub fn encode<B: BufMut>(&self, buf: &mut B) -> Result<(), RecordsError> {
479 const HEADER_TAIL_LEN: i32 = 49;
480
481 let mut raw_body =
483 BytesMut::with_capacity(self.records.iter().map(Record::encoded_len).sum());
484 for r in &self.records {
485 r.encode(&mut raw_body)?;
486 }
487 let raw_body = raw_body.freeze();
488
489 let codec = self.attributes.compression();
491 let body: Bytes = if codec == crabka_compression::CompressionType::None {
492 raw_body
493 } else {
494 crabka_compression::compress(codec, &raw_body)?
495 };
496
497 let batch_length = HEADER_TAIL_LEN
499 + i32::try_from(body.len())
500 .map_err(|_| RecordsError::RecordParse("body length exceeds i32".into()))?;
501
502 let mut covered = BytesMut::with_capacity(40);
504 covered.put_i16(self.attributes.0);
505 covered.put_i32(self.last_offset_delta);
506 covered.put_i64(self.base_timestamp);
507 covered.put_i64(self.max_timestamp);
508 covered.put_i64(self.producer_id);
509 covered.put_i16(self.producer_epoch);
510 covered.put_i32(self.base_sequence);
511 covered.put_i32(
512 i32::try_from(self.records.len())
513 .map_err(|_| RecordsError::RecordParse("records_count exceeds i32".into()))?,
514 );
515 let covered_head = covered.freeze();
516
517 let mut crc = crc32c(&covered_head);
519 crc = crc32c_append(crc, &body);
520
521 buf.put_i64(self.base_offset);
523 buf.put_i32(batch_length);
524 buf.put_i32(self.partition_leader_epoch);
525 buf.put_i8(2); buf.put_u32(crc);
527 buf.put_slice(&covered_head);
528 buf.put_slice(&body);
529 Ok(())
530 }
531
532 pub fn encoded_len(&self) -> usize {
535 let body: usize = self.records.iter().map(Record::encoded_len).sum();
536 HEADER_LEN + body
537 }
538}
539
540#[cfg(test)]
541mod batch_tests {
542 use super::*;
543 use crabka_compression::CompressionType;
544
545 fn fixture_empty_batch() -> RecordBatch {
546 RecordBatch::default()
547 }
548
549 fn fixture_single_record_batch() -> RecordBatch {
550 RecordBatch {
551 records: vec![Record {
552 key: Some(Bytes::from_static(b"k1")),
553 value: Some(Bytes::from_static(b"v1")),
554 ..Default::default()
555 }],
556 ..RecordBatch::default()
557 }
558 }
559
560 fn fixture_multi_record_batch() -> RecordBatch {
561 RecordBatch {
562 base_offset: 42,
563 partition_leader_epoch: 5,
564 last_offset_delta: 2,
565 base_timestamp: 1_700_000_000,
566 max_timestamp: 1_700_000_500,
567 producer_id: 100,
568 producer_epoch: 3,
569 base_sequence: 7,
570 records: vec![
571 Record {
572 offset_delta: 0,
573 timestamp_delta: 0,
574 key: Some(Bytes::from_static(b"a")),
575 value: Some(Bytes::from_static(b"1")),
576 ..Default::default()
577 },
578 Record {
579 offset_delta: 1,
580 timestamp_delta: 100,
581 key: Some(Bytes::from_static(b"b")),
582 value: Some(Bytes::from_static(b"2")),
583 ..Default::default()
584 },
585 Record {
586 offset_delta: 2,
587 timestamp_delta: 500,
588 key: None,
589 value: Some(Bytes::from_static(b"3")),
590 headers: vec![RecordHeader {
591 key: "h".to_string(),
592 value: Some(Bytes::from_static(b"hv")),
593 }],
594 ..Default::default()
595 },
596 ],
597 ..RecordBatch::default()
598 }
599 }
600
601 macro_rules! roundtrip_uncompressed {
602 ($name:ident, $fixture:ident) => {
603 #[test]
604 fn $name() {
605 let mut b = $fixture();
606 b.attributes = b.attributes.with_compression(CompressionType::None);
607
608 let mut buf = BytesMut::new();
609 b.encode(&mut buf).unwrap();
610 assert_eq!(buf.len(), b.encoded_len());
611
612 let mut cur: &[u8] = &buf[..];
613 let decoded = RecordBatch::decode(&mut cur).unwrap();
614 assert_eq!(decoded, b);
615 assert!(cur.is_empty());
616 }
617 };
618 }
619
620 roundtrip_uncompressed!(uncompressed_empty, fixture_empty_batch);
621 roundtrip_uncompressed!(uncompressed_single, fixture_single_record_batch);
622 roundtrip_uncompressed!(uncompressed_multi, fixture_multi_record_batch);
623
624 #[test]
625 fn rejects_pre_v2_magic() {
626 let mut buf = BytesMut::new();
627 buf.put_i64(0); buf.put_i32(49); buf.put_i32(0); buf.put_i8(1); buf.put_u32(0); for _ in 21..HEADER_LEN {
633 buf.put_u8(0);
634 }
635 let mut cur: &[u8] = &buf[..];
636 assert!(matches!(
637 RecordBatch::decode(&mut cur),
638 Err(RecordsError::UnsupportedMagic { found: 1 })
639 ));
640 }
641
642 #[test]
643 fn rejects_bad_crc() {
644 let b = fixture_single_record_batch();
645 let mut buf = BytesMut::new();
646 b.encode(&mut buf).unwrap();
647 buf[17] ^= 0xFF;
649 let mut cur: &[u8] = &buf[..];
650 assert!(matches!(
651 RecordBatch::decode(&mut cur),
652 Err(RecordsError::CrcMismatch { .. })
653 ));
654 }
655
656 macro_rules! roundtrip_compressed {
657 ($name:ident, $codec:expr) => {
658 #[test]
659 fn $name() {
660 let mut b = fixture_multi_record_batch();
661 b.attributes = b.attributes.with_compression($codec);
662
663 let mut buf = BytesMut::new();
664 b.encode(&mut buf).unwrap();
665 let mut cur: &[u8] = &buf[..];
666 let decoded = RecordBatch::decode(&mut cur).unwrap();
667 assert_eq!(decoded, b);
668 assert!(cur.is_empty());
669 }
670 };
671 }
672
673 roundtrip_compressed!(compressed_gzip, CompressionType::Gzip);
674 roundtrip_compressed!(compressed_snappy, CompressionType::Snappy);
675 roundtrip_compressed!(compressed_lz4, CompressionType::Lz4);
676 roundtrip_compressed!(compressed_zstd, CompressionType::Zstd);
677}
678
679impl crate::Encode for RecordBatch {
680 fn encode<B: BufMut>(&self, buf: &mut B, _version: i16) -> Result<(), crate::ProtocolError> {
681 RecordBatch::encode(self, buf).map_err(Into::into)
682 }
683
684 fn encoded_len(&self, _version: i16) -> usize {
685 RecordBatch::encoded_len(self)
686 }
687}
688
689impl crate::Decode<'_> for RecordBatch {
690 fn decode<B: Buf>(buf: &mut B, _version: i16) -> Result<Self, crate::ProtocolError> {
691 RecordBatch::decode(buf).map_err(Into::into)
692 }
693}