/** * Reads one generation's frames in byte order and applies the multi-block * batch reader rule (ADR-0030 ยง3). Every reader in the library goes through * {@link readSegment}: live consumer dispatch, catch-up and pending seals, * replay, and the compactor. * * A multi-block batch starts at a data frame carrying `bm`. Its frames are * never buffered: when the reader meets that frame it runs a separate * validation scan forward from it, keeping only {@link BatchScanState}, until * the scan reaches a verdict. A committed batch is then delivered by reading * on from its first frame; an invalid or abandoned one is skipped whole. A * batch still open at the end of a growing generation is held: the reader * stops at its first frame, and the next call resumes the scan where it left * off, so each poll scans only new bytes and every byte is read at most twice. * An abandoned batch's id is remembered until its `_commit` or `_abort`, so * frames of it that follow the frame that abandoned it are never delivered * ({@link AbandonedBatches}). The ids are bounded and never forgotten early: * once the bound is reached the reader fails closed, handing the next * abandoned batch, and any later batch frame it cannot rule out as a stray, to * the poison policy. A malformed line that still reads as a batch * start invalidates its batch as a whole rather than being skipped alone. */ import { type Frame, PoisonFrameError } from './frame.js'; /** Chunk size for every segment read: bounds peak transient allocation to one chunk. */ export declare const READ_CHUNK_BYTES: number; /** Reads bytes `[start, end]` (inclusive) of one segment. */ export type RangeReader = (start: number, end: number) => Promise; /** Constant-size validation state for one multi-block batch, carried across polls. */ export type BatchScanState = { readonly batchId: string; /** Start byte of the batch's first (`bm`) frame. */ readonly startByte: number; /** First byte the scan has not yet consumed. */ scannedByte: number; /** Data frames of the batch seen so far, the first included. */ count: number; readonly firstOffset: bigint; lastOffset: bigint; /** Lowest and highest readable data-frame offsets; they differ from first and last when frames are out of sequence. */ minOffset: bigint | undefined; maxOffset: bigint | undefined; /** A poison line or an out-of-sequence frame fell inside the batch. */ poison: boolean; }; /** * Outcome of a batch's validation scan. `endByte` is where delivery resumes: * after the `_commit` line for a committed or invalid batch, or at the start * of the frame that abandoned it. */ export type BatchVerdict = { readonly kind: 'pending'; } | { readonly kind: 'committed'; readonly endByte: number; } | { readonly kind: 'invalid'; readonly endByte: number; readonly reason: string; } | { readonly kind: 'abandoned'; readonly endByte: number; }; export type BatchScan = { readonly state: BatchScanState; verdict: BatchVerdict; }; /** Keeps a batch's scan between calls to {@link readSegment} on the same segment. */ export type BatchScanCache = { get(startByte: number): BatchScan | undefined; set(scan: BatchScan): void; }; export type SegmentItem = { readonly kind: 'frame'; readonly frame: Frame; readonly bytes: Buffer; readonly startByte: number; readonly endByte: number; } | { readonly kind: 'poison'; readonly error: PoisonFrameError; readonly bytes: Buffer; readonly startByte: number; readonly endByte: number; /** Set when the line decodes as a data frame that still may not be delivered. */ readonly offset?: bigint; } | { /** A multi-block batch none of which may be delivered: invalid (poison policy applies) or abandoned. */ readonly kind: 'batch-skipped'; readonly state: BatchScanState; readonly verdict: Extract; readonly startByte: number; readonly endByte: number; } | { /** A frame of an abandoned batch read after the frame that abandoned it: never delivered. */ readonly kind: 'batch-stray'; readonly batchId: string; readonly bytes: Buffer; readonly startByte: number; readonly endByte: number; } | { readonly kind: 'held'; readonly startByte: number; } | { readonly kind: 'chunk-end'; readonly endByte: number; } | { readonly kind: 'unterminated'; readonly bytes: Buffer; readonly startByte: number; }; /** Most abandoned batch ids {@link AbandonedBatches} remembers at once. */ export declare const MAX_ABANDONED_BATCHES = 16; /** * Ids of batches that ended uncommitted before their own `_commit` or `_abort` * was read. Frames still tagged with one of them are strays of that batch and * are never delivered. An id is forgotten only at its batch's `_commit` or * `_abort`. At most {@link MAX_ABANDONED_BATCHES} are kept: past that, * {@link add} refuses the id and {@link overflowed} stays set for the segment. */ export declare class AbandonedBatches { private readonly ids; /** An abandoned batch was refused, so a frame of it could follow unrecognised. */ overflowed: boolean; has(batchId: string): boolean; /** Remember `batchId`; returns false, and sets {@link overflowed}, when the set is full. */ add(batchId: string): boolean; delete(batchId: string): void; } export type SegmentReadOptions = { readonly read: RangeReader; readonly gen: number; readonly startByte: number; /** Bytes of the segment the read covers. */ readonly length: number; /** The segment cannot grow (sealed, pending seal, or a snapshot read): a batch open at its end is abandoned, not held. */ readonly frozen: boolean; readonly scans?: BatchScanCache; /** Carries abandoned batch ids across calls on the same segment; a frozen read can start empty. */ readonly abandoned?: AbandonedBatches; }; /** * Yield the segment's lines from `startByte` to `length`, one chunk at a time, * resolving multi-block batches as described in the file header. Lines of a * committed batch are yielded as ordinary frames; `_commit` and `_abort` are * yielded too, for the caller to skip. Stops after a `held` item. */ export declare function readSegment(opts: SegmentReadOptions): AsyncGenerator; /** Start the scan state for a multi-block batch whose first line spans `[startByte, endByte)`. */ export declare function openBatchScan(batchId: string, firstOffset: bigint, startByte: number, endByte: number, poison?: boolean): BatchScanState; /** * Scan forward from `state.scannedByte` to `length` without dispatching, * updating `state`, until the batch's verdict is known or the bytes run out * (`pending`). `prefetched` holds bytes already read, so the scan does not * read them again. */ export declare function scanBatch(state: BatchScanState, read: RangeReader, length: number, gen: number, prefetched?: { readonly bytes: Buffer; readonly startByte: number; }): Promise; //# sourceMappingURL=segmentReader.d.ts.map