pub struct Segment { /* private fields */ }Expand description
A single log segment: the .log data file paired with its sparse
.index (offset → byte position) and .timeindex (timestamp →
relative offset) sidecars.
A segment is identified by its base_offset: the absolute offset of
its first record, encoded into the segment’s 20-digit zero-padded
filename. Segments are created via Segment::create (new active
segment) or opened via Segment::open (read-only sealed segment)
or Segment::open_active (active segment with tail recovery).
Implementations§
Source§impl Segment
impl Segment
Sourcepub fn create(dir: &Path, base_offset: i64) -> Result<Self, LogError>
pub fn create(dir: &Path, base_offset: i64) -> Result<Self, LogError>
Create a fresh active segment at the given base offset. Fails if
the .log file already exists.
Sourcepub fn open_active(
dir: &Path,
base_offset: i64,
validate: bool,
) -> Result<Self, LogError>
pub fn open_active( dir: &Path, base_offset: i64, validate: bool, ) -> Result<Self, LogError>
Open as the active segment, scanning from the last-indexed position
to EOF when validate is true. A partial trailing batch (or one
that fails to decode) is truncated; cleanly decoded batches update
last_offset and max_timestamp.
Sourcepub fn open(dir: &Path, base_offset: i64) -> Result<Self, LogError>
pub fn open(dir: &Path, base_offset: i64) -> Result<Self, LogError>
Open an existing segment for reading. Lightweight — no full scan.
Open an existing segment for reading. The log and index files
must already exist on disk; the segment is initialized with
last_offset = base_offset - 1 and max_timestamp = i64::MIN
until tail recovery (via Segment::open_active) populates them.
Sourcepub fn base_offset(&self) -> i64
pub fn base_offset(&self) -> i64
Absolute offset of the first record this segment can hold.
Sourcepub fn txn_index_path(&self) -> PathBuf
pub fn txn_index_path(&self) -> PathBuf
Path to this segment’s .txnindex file (may not exist yet).
Sourcepub fn leader_epoch_checkpoint_path(&self) -> PathBuf
pub fn leader_epoch_checkpoint_path(&self) -> PathBuf
Path to the per-partition .leader-epoch-checkpoint file in this
segment’s directory. The checkpoint is shared across all segments
in a partition — epoch history accumulates over the log’s lifetime.
Sourcepub fn last_offset(&self) -> i64
pub fn last_offset(&self) -> i64
Highest absolute offset (inclusive) of any batch appended to this
segment. Returns base_offset - 1 for an empty segment.
Sourcepub fn size_bytes(&self) -> u64
pub fn size_bytes(&self) -> u64
Current .log file size in bytes.
Sourcepub fn max_timestamp(&self) -> i64
pub fn max_timestamp(&self) -> i64
Highest timestamp observed across all batches in this segment.
Returns i64::MIN for an empty segment.
Sourcepub fn offset_for_timestamp(&self, target_ts: i64) -> Option<(i64, i64)>
pub fn offset_for_timestamp(&self, target_ts: i64) -> Option<(i64, i64)>
Absolute offset and record timestamp of the first record in this
segment whose timestamp is >= target_ts. Uses the sparse time
index for a floor position, then scans .log batches forward
(the index is sparse, so an exact answer needs the post-index
scan — matching Kafka’s LogSegment.findOffsetByTimestamp).
Returns None when no record in this segment qualifies.
Sourcepub fn offset_of_max_timestamp(&self) -> Option<(i64, i64)>
pub fn offset_of_max_timestamp(&self) -> Option<(i64, i64)>
Absolute offset and timestamp of the record carrying this
segment’s max_timestamp. Ties resolve to the earliest offset
(Kafka). Returns None for an empty segment. Uses the time
index’s floor for the max to start the scan, then scans forward
for the first record whose timestamp equals the segment max.
Sourcepub fn is_sealed(&self) -> bool
pub fn is_sealed(&self) -> bool
true once the segment has been sealed via Segment::seal;
sealed segments reject appends.
Sourcepub fn read(
&self,
offset: i64,
max_bytes: usize,
) -> Result<Vec<RecordBatch>, LogError>
pub fn read( &self, offset: i64, max_bytes: usize, ) -> Result<Vec<RecordBatch>, LogError>
Read batches starting at or just before offset, up to roughly
max_bytes of .log data. Returns an empty Vec when offset
is past last_offset.
Sourcepub fn read_raw(
&self,
fetch_offset: i64,
limit_offset: i64,
max_bytes: usize,
) -> Result<RawSegmentRead, LogError>
pub fn read_raw( &self, fetch_offset: i64, limit_offset: i64, max_bytes: usize, ) -> Result<RawSegmentRead, LogError>
Read a contiguous run of complete, verbatim record-batch bytes
beginning at the batch containing fetch_offset, including only
batches whose base_offset < limit_offset, up to roughly max_bytes
(always at least one batch — Kafka’s anti-stall rule). No record
decoding: only fixed batch headers are read to find boundaries.
Sourcepub fn append(
&mut self,
batch: &RecordBatch,
index_interval_bytes: u32,
) -> Result<u64, LogError>
pub fn append( &mut self, batch: &RecordBatch, index_interval_bytes: u32, ) -> Result<u64, LogError>
Append a record batch. Returns the byte position where the batch starts.
Side effects:
- Updates
log_size,max_timestamp,last_offset. - Adds sparse index entries when bytes-since-last-entry exceeds
index_interval_bytes(or for the first batch).