Expand description
Apache Kafka legacy (v0/v1) MessageSet codec, with bridges to and from
the v2 RecordBatch types in crabka_protocol.
See the Kafka protocol docs for the wire layout this crate
implements. v0 carries no per-message timestamp; v1 adds an i64
timestamp per message (KIP-32). Compression in both is signalled in
the low 3 bits of the per-message attributes byte, with the
compressed payload appearing as a single outer message whose value
is a nested (uncompressed) MessageSet.
§Quick tour
Message/Magic: per-message wire format (CRC + fields).ParsedRecord: cross-format view of a single offset-tagged record.decode_message_set/encode_flat_message_set/encode_compressed_message_set: top-level codec.v2_to_legacy/legacy_to_v2: bridge to/fromcrabka_protocol::records::RecordBatch. Use these from the Fetch (down-conversion) and Produce (up-conversion) handlers.
§Encode and decode a v1 MessageSet
use bytes::{Bytes, BytesMut};
use crabka_records_legacy::{Magic, ParsedRecord, decode_message_set, encode_flat_message_set};
let records = vec![ParsedRecord {
offset: 42,
timestamp: Some(1_713_000_000_000),
key: Some(Bytes::from_static(b"order-42")),
value: Some(Bytes::from_static(b"created")),
}];
let mut buf = BytesMut::new();
encode_flat_message_set(records, Magic::V1, &mut buf);
let decoded = decode_message_set(&mut &buf[..], buf.len())?;
assert_eq!(decoded[0].offset, 42);Modules§
- attrs
- Bit layout of the legacy
attributesbyte.
Structs§
- Message
- Owned, decoded legacy message (post-frame parse).
- Parsed
Record - A single decoded MessageSet entry: the offset-tagged payload of one logical record after compression unwrapping.
Enums§
- Legacy
Records Error - Magic
- Magic byte (i.e. legacy message format version) — 0 or 1.
Functions§
- attrs_
with_ compression - compression_
from_ attrs - Map a v0/v1 compression-code (low 3 bits of
attributes) to aCompressionType. - decode_
message_ set - Decode a flat (uncompressed) MessageSet from
buf, expecting it to consume exactlyset_size_bytesbytes from the buffer. Compressed wrapper messages encountered at top level are unwrapped recursively once — nested compression (a compressed wrapper inside a compressed wrapper) is rejected. - encode_
compressed_ message_ set - Encode a MessageSet wrapped in a single compressed outer message. The inner set is uncompressed and contains one message per record, laid out per KIP-32 conventions (v1 inner offsets relative 0..N-1).
- encode_
flat_ message_ set - Encode a flat MessageSet (one outer message per record) of magic
magicintobuf. Useful when emitting an uncompressed batch. - legacy_
to_ v2 - Up-convert a v0/v1 MessageSet to a v2 RecordBatch suitable for the log write path.
- parsed_
from_ v2 - Iterate the v2 batch’s records, dropping control batches entirely.
- v2_
to_ legacy - Down-convert a v2 RecordBatch to v0/v1 MessageSet bytes.