Skip to main content

krabka_protocol/records/
file_region.rs

1//! A descriptor for a contiguous run of records bytes that live in a segment
2//! `.log` file. The zero-copy fetch path (Increment D) uses it.
3//!
4//! The fetch read returns the `(file, offset, len)` triple instead of a `pread`
5//! of the records into an owned `Bytes`, which would copy them out of the page
6//! cache into userspace. The broker then `sendfile(2)`s that range straight
7//! from the page cache to a plaintext socket and never brings the bytes into
8//! userspace. The `Arc<File>` pins the inode through the async send, even if
9//! compaction or truncation removes the segment from the log in the meantime.
10//! On Unix the open fd keeps the inode alive.
11//!
12//! `FileRegion` is intentionally pure-`std`: an `Arc<std::fs::File>` and
13//! integers. The protocol crate therefore needs no new dependency, and the same
14//! type can flow from the log crate's `read_raw_desc` through
15//! `RecordsPayload::FileRegions` to the broker's sendfile drainer.
16
17use std::{fs::File, sync::Arc};
18
19/// A `[offset, offset+len)` byte range within a segment `.log` file.
20///
21/// The range holds one or more complete v2 record batches, and it can end with
22/// a clipped trailing batch. Byte for byte, these are the records-field bytes
23/// that a fetch response would otherwise materialize.
24#[derive(Debug, Clone)]
25pub struct FileRegion {
26    /// The segment `.log` file. Shared (`Arc`) so the inode stays open for the
27    /// duration of the async send regardless of concurrent retention.
28    pub file: Arc<File>,
29    /// Start byte position of the records run within `file`.
30    pub offset: u64,
31    /// Number of bytes in the run.
32    pub len: usize,
33}
34
35impl PartialEq for FileRegion {
36    /// Two regions are equal when they describe the same byte range of the
37    /// same underlying file. `Arc<File>` has no `PartialEq`, so this impl
38    /// compares `Arc` pointer identity, which means the same open handle.
39    fn eq(&self, other: &Self) -> bool {
40        Arc::ptr_eq(&self.file, &other.file) && self.offset == other.offset && self.len == other.len
41    }
42}
43
44impl Eq for FileRegion {}