Skip to main content

Segment

Struct Segment 

Source
pub struct Segment { /* private fields */ }

Implementations§

Source§

impl Segment

Source

pub fn append( &mut self, batch: &RecordBatch, index_interval: ByteSize, ) -> Result<u64, LogError>

Append a record batch and return the byte position where the batch starts.

Side effects:

  • Updates log_size, max_timestamp, and last_offset.
  • Adds sparse index entries when the byte count since the last entry exceeds index_interval, which Kafka’s LogSegment.append does not do for the first batch either.
§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source

pub fn append_verbatim( &mut self, bytes: &[u8], base_offset: Offset, last_offset_delta: i32, max_timestamp: i64, leader_epoch: LeaderEpoch, index_interval: ByteSize, ) -> Result<u64, LogError>

Append a batch verbatim and write the producer’s exact wire bytes.

This method does not decode, re-encode, recompress, or recompute the CRC. bytes is the producer’s verbatim v2 batch, which the caller has already CRC-validated through the borrowed header-only path. This method patches only base_offset, bytes 0..8, and partition_leader_epoch, bytes 12..16, into a writable copy, then writes those bytes. Both fields sit outside the CRC-covered region. The stored CRC stays byte-identical to the producer’s CRC, because no CRC-covered byte changes.

base_offset, last_offset_delta, max_timestamp, and leader_epoch come from the caller’s borrowed header read. This method updates the segment side effects, log_size, last_offset, max_timestamp, and the sparse index, exactly as Segment::append does.

It returns the byte position where the batch starts.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source§

impl Segment

Source

pub fn seal(&mut self) -> Result<(), LogError>

Mark this segment as sealed. No more appends.

Sealing first writes the segment’s final time-index entry, Kafka’s LogSegment.onBecomeInactiveSegment: the sparse index lags the writes, so without it a reopened segment would not learn the timestamp of the batches after its last index point from the index. A segment that wrote through O_DIRECT then goes back to the page cache, which cuts the padding past its last batch, and what is left of its block reservation is given back.

§Errors

Returns an error when the time-index entry cannot be written or its offset overflows the index range, or when the padding cannot be cut. The segment stays open then.

Source

pub fn seal_at(&mut self, last: Offset)

Seal a segment loaded without a scan and set its last_offset to last.

Callers pass next_segment.base_offset - 1, the highest offset this sealed segment can hold. The no-scan loader leaves last_offset = base_offset - 1 because it does not scan the .log. Without this fix, a sealed segment recovered on Log::open reports that stale last_offset. Log::read_raw skips any segment whose last_offset() < fetch_offset, so it would skip the first sealed segment after a restart and serve a later segment’s base offset. That creates an offset gap, and a follower that fetches at 0 then loops on the resulting append mismatch.

Source

pub fn flush(&mut self) -> Result<(), LogError>

Force-sync everything to disk.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source

pub fn truncate_to_relative(&mut self, rel: u32) -> Result<(), LogError>

Truncate the .log file and the indexes so that no batch at relative_offset >= rel remains. Log::truncate_to uses this method. The segment stays unsealed.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source§

impl Segment

Source

pub fn create(dir: &Path, base_offset: Offset) -> Result<Self, LogError>

Create a fresh active segment at the given base offset. This fails if the .log file already exists.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source

pub fn open_active( dir: &Path, base_offset: Offset, validate: bool, ) -> Result<Self, LogError>

Open as the active segment.

When validate is true, this method scans from byte zero to EOF. It truncates a partial or invalid trailing batch, rebuilds both sparse indexes, and updates last_offset and max_timestamp from the same maximal valid prefix.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source

pub fn open(dir: &Path, base_offset: Offset) -> Result<Self, LogError>

Open an existing segment for reading and refuse further appends.

Scans the batches to restore the last offset and maximum timestamp. A corrupt or incomplete batch returns an error without truncating the file. Use Segment::open_active to recover a writable segment.

§Errors

Returns an error when the segment or its indexes cannot be opened, its batches cannot be read, or its offset chain or batch bytes are corrupt.

Source§

impl Segment

Source

pub fn read( &self, offset: Offset, max_size: ByteSize, ) -> Result<Vec<RecordBatch>, LogError>

Read batches from offset or just before it, up to about max_bytes of .log data. The result is an empty Vec when offset is past last_offset.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source§

impl Segment

Source

pub fn read_raw( &self, fetch_offset: Offset, limit_offset: Offset, max_size: ByteSize, ) -> Result<RawSegmentRead, LogError>

Read a contiguous run of complete, verbatim record-batch bytes.

The run starts at the batch that contains fetch_offset. It includes only batches whose base_offset < limit_offset, up to about max_bytes, and always at least one batch. That last rule is Kafka’s anti-stall rule. This method decodes no records. It reads only the fixed batch headers to find the boundaries.

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source§

impl Segment

Source

pub fn read_raw_desc( &self, fetch_offset: Offset, limit_offset: Offset, max_size: ByteSize, ) -> Result<RawSegmentDesc, LogError>

Descriptor variant of Segment::read_raw for the zero-copy sendfile fetch path.

This method runs the same boundary walk and selects the identical [start_pos+range_start, start_pos+range_end) byte range that read_raw would have sliced. It returns a krabka_protocol::records::FileRegion descriptor instead of a pread of the payload into an owned Bytes.

The walk reads bounded windows to amortize header lookups and uses the header’s batch_length to find boundaries. It returns a file region instead of building an owned copy of the selected payload. The region is byte-identical to the bytes of read_raw for the same (fetch_offset, limit_offset, max_bytes).

§Errors

Returns an error when log I/O fails, a record or index is corrupt, or the requested offset violates the segment state.

Source§

impl Segment

Source

pub fn read_view( &self, fetch_offset: Offset, cold_before: Option<u64>, ) -> Result<SegmentReadView, LogError>

A view of this segment for a read from fetch_offset, to run after the log lock is released. cold_before is the log’s verdict on where a fetch of this segment turns cold, taken now because it needs the log.

§Errors

Returns an error when fetch_offset is too far past the segment for its index, which no read of the segment could use either.

Source§

impl Segment

Source

pub fn offset_for_timestamp(&self, target_ts: i64) -> Option<(Offset, i64)>

Absolute offset and record timestamp of the first record in this segment whose timestamp is >= target_ts.

This method takes a floor position from the sparse time index, then scans .log batches forward. The index is sparse, so an exact answer needs that scan after the index lookup. This matches Kafka’s LogSegment.findOffsetByTimestamp. The result is None when no record in this segment qualifies.

The scan applies no max.decompressed.message.bytes: this is the lookup the log’s own bookkeeping uses, to read a segment’s first timestamp. A ListOffsets answer goes through Segment::offset_for_timestamp_with_window, which takes the limit.

Source

pub fn restore_max_timestamp(&mut self) -> Result<(), LogError>

Restore max_timestamp for a segment loaded without a scan.

The no-scan loader leaves the field at its unknown sentinel. Retention compares that sentinel against the age cutoff, so without this call every reopened segment looks older than any window and the first Log::tick after a restart deletes all of them.

Kafka’s LogSegment reads maxTimestampSoFar back from the time index, and this method starts there. The sparse index lags the writes, though: its newest entry holds the running maximum as of the last indexed batch, and every batch appended after it is unaccounted for. The method therefore also walks the batch headers from the newest offset-index entry to the end of the file. That walk is bounded by index_interval plus one batch, and it reads no record body.

§Errors

Returns LogError::Io when the .log file cannot be read.

Source§

impl Segment

Source

pub fn base_offset(&self) -> Offset

Absolute offset of the first record this segment can hold.

Source

pub fn txn_index_path(&self) -> PathBuf

Path to this segment’s .txnindex file (may not exist yet).

Source

pub fn stamp_index_path(&self) -> PathBuf

Path to this segment’s .stampindex sidecar (may not exist yet).

Source

pub fn leader_epoch_checkpoint_path(&self) -> PathBuf

Path to the per-partition .leader-epoch-checkpoint file in this segment’s directory. All segments in a partition share the checkpoint, and epoch history accumulates over the log’s lifetime.

Source

pub fn last_offset(&self) -> Offset

Highest absolute offset (inclusive) of any batch appended to this segment. Returns base_offset - 1 for an empty segment.

Source

pub fn size(&self) -> ByteSize

Current .log file size.

The field itself stays a raw u64, because it is a file position and goes directly into a pread or seek argument. It converts here, where the value becomes a magnitude that the roll and retention policies compare.

Source

pub fn max_timestamp(&self) -> i64

Highest timestamp observed across all batches in this segment. Returns i64::MIN for an empty segment, and can also read -1 (NO_TIMESTAMP) when every batch in the segment carries no timestamp. See Segment::last_modified for Kafka’s fallback for both cases.

Source

pub fn last_modified(&self) -> Option<SystemTime>

The .log file’s last-modified time, as Kafka’s LogSegment. lastModified reads it. Time retention falls back to this when the segment carries no record timestamp of its own (max_timestamp() is i64::MIN or -1), the same as UnifiedLog’s largestRecordTimestamp().orElse(lastModified()).

None when the filesystem cannot report it, which retention treats as “unknown, not overdue” rather than as license to guess.

Source

pub fn is_sealed(&self) -> bool

true once Segment::seal has sealed the segment. Sealed segments reject appends.

Source

pub fn dir(&self) -> &Path

Directory that holds this segment’s .log, .index, and .timeindex files. The compactor uses it to read the .log file directly and to avoid the Segment::read path. That path depends on the in-memory last_offset, which is stale for sealed segments loaded through the log’s no-scan loader.

Trait Implementations§

Source§

impl Debug for Segment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<F> FnGhost for F

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Paint for T
where T: ?Sized,

§

fn fg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the foreground set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like red() and green(), which have the same functionality but are pithier.

§Example

Set foreground color to white using fg():

use yansi::{Paint, Color};

painted.fg(Color::White);

Set foreground color to white using white().

use yansi::Paint;

painted.white();
§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Primary].

§Example
println!("{}", value.primary());
§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Fixed].

§Example
println!("{}", value.fixed(color));
§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Rgb].

§Example
println!("{}", value.rgb(r, g, b));
§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Black].

§Example
println!("{}", value.black());
§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Red].

§Example
println!("{}", value.red());
§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Green].

§Example
println!("{}", value.green());
§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Yellow].

§Example
println!("{}", value.yellow());
§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Blue].

§Example
println!("{}", value.blue());
§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Magenta].

§Example
println!("{}", value.magenta());
§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Cyan].

§Example
println!("{}", value.cyan());
§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: White].

§Example
println!("{}", value.white());
§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlack].

§Example
println!("{}", value.bright_black());
§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightRed].

§Example
println!("{}", value.bright_red());
§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightGreen].

§Example
println!("{}", value.bright_green());
§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightYellow].

§Example
println!("{}", value.bright_yellow());
§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlue].

§Example
println!("{}", value.bright_blue());
§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.bright_magenta());
§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightCyan].

§Example
println!("{}", value.bright_cyan());
§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightWhite].

§Example
println!("{}", value.bright_white());
§

fn bg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the background set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like on_red() and on_green(), which have the same functionality but are pithier.

§Example

Set background color to red using fg():

use yansi::{Paint, Color};

painted.bg(Color::Red);

Set background color to red using on_red().

use yansi::Paint;

painted.on_red();
§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Primary].

§Example
println!("{}", value.on_primary());
§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Fixed].

§Example
println!("{}", value.on_fixed(color));
§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Rgb].

§Example
println!("{}", value.on_rgb(r, g, b));
§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Black].

§Example
println!("{}", value.on_black());
§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Red].

§Example
println!("{}", value.on_red());
§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Green].

§Example
println!("{}", value.on_green());
§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Yellow].

§Example
println!("{}", value.on_yellow());
§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Blue].

§Example
println!("{}", value.on_blue());
§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Magenta].

§Example
println!("{}", value.on_magenta());
§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Cyan].

§Example
println!("{}", value.on_cyan());
§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: White].

§Example
println!("{}", value.on_white());
§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlack].

§Example
println!("{}", value.on_bright_black());
§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightRed].

§Example
println!("{}", value.on_bright_red());
§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightGreen].

§Example
println!("{}", value.on_bright_green());
§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightYellow].

§Example
println!("{}", value.on_bright_yellow());
§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlue].

§Example
println!("{}", value.on_bright_blue());
§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.on_bright_magenta());
§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightCyan].

§Example
println!("{}", value.on_bright_cyan());
§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightWhite].

§Example
println!("{}", value.on_bright_white());
§

fn attr(&self, value: Attribute) -> Painted<&T>

Enables the styling [Attribute] value.

This method should be used rarely. Instead, prefer to use attribute-specific builder methods like bold() and underline(), which have the same functionality but are pithier.

§Example

Make text bold using attr():

use yansi::{Paint, Attribute};

painted.attr(Attribute::Bold);

Make text bold using using bold().

use yansi::Paint;

painted.bold();
§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Bold].

§Example
println!("{}", value.bold());
§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Dim].

§Example
println!("{}", value.dim());
§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Italic].

§Example
println!("{}", value.italic());
§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Underline].

§Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute :: Blink].

§Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute :: RapidBlink].

§Example
println!("{}", value.rapid_blink());
§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Invert].

§Example
println!("{}", value.invert());
§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Conceal].

§Example
println!("{}", value.conceal());
§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Strike].

§Example
println!("{}", value.strike());
§

fn quirk(&self, value: Quirk) -> Painted<&T>

Enables the yansi [Quirk] value.

This method should be used rarely. Instead, prefer to use quirk-specific builder methods like mask() and wrap(), which have the same functionality but are pithier.

§Example

Enable wrapping using .quirk():

use yansi::{Paint, Quirk};

painted.quirk(Quirk::Wrap);

Enable wrapping using wrap().

use yansi::Paint;

painted.wrap();
§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Mask].

§Example
println!("{}", value.mask());
§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Wrap].

§Example
println!("{}", value.wrap());
§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Linger].

§Example
println!("{}", value.linger());
§

fn clear(&self) -> Painted<&T>

👎Deprecated since 1.0.1:

renamed to resetting() due to conflicts with Vec::clear(). The clear() method will be removed in a future release.

Returns self with the quirk() set to [Quirk :: Clear].

§Example
println!("{}", value.clear());
§

fn resetting(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Resetting].

§Example
println!("{}", value.resetting());
§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Bright].

§Example
println!("{}", value.bright());
§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: OnBright].

§Example
println!("{}", value.on_bright());
§

fn whenever(&self, value: Condition) -> Painted<&T>

Conditionally enable styling based on whether the [Condition] value applies. Replaces any previous condition.

See the crate level docs for more details.

§Example

Enable styling painted only when both stdout and stderr are TTYs:

use yansi::{Paint, Condition};

painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
§

fn new(self) -> Painted<Self>
where Self: Sized,

Create a new [Painted] with a default [Style]. Read more
§

fn paint<S>(&self, style: S) -> Painted<&Self>
where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more