TByteRangeStream
Document management, Performance, Concurrency
Description
TByteRangeStream is a seekable, read-only TStream over an IPLByteRangeSource. It satisfies reads from an aligned least-recently-used cache of chunks, so a parser that performs bounded seeks and reads drives a remote or progressively available document unchanged. A request that misses the cache fetches one aligned chunk, and callers that miss the same chunk while a fetch is pending share that fetch.
ReadAt performs cursor-independent absolute reads, Read uses the shared stream position, and StartPrefetch runs a planner in the background to warm the cache ahead of rendering. GetCacheStats and ClearCache expose and reset the counters maintained for monitoring.
Syntax
Delphi
TByteRangeStream = Class(TPDFReadAtStream)
Public
Constructor Create(Source: IPLByteRangeSource; ChunkSize: Integer= 0;
CacheLimitBytes: Int64= 0;
CoalesceWindowMS: Cardinal= PLNET_DEFAULT_COALESCE_WINDOW_MS);
Destructor Destroy; Override;
Procedure ClearCache(ResetStatistics: Boolean= True);
Function GetCacheStats: TPDFByteRangeCacheStats;
Procedure SetCacheLimitBytes(Value: Int64);
Procedure SetCoalesceWindowMS(Value: Cardinal);
Function IsRangeCached(Offset, Count: Int64): Boolean;
Function IsRangeAvailable(Offset, Count: Int64): Boolean;
Function Read(Var Buffer; Count: Longint): Longint; Override;
Function ReadAt(Offset: Int64; Var Buffer;
Count: Integer): Integer; Override;
Function StartPrefetch(Planner: IPLByteRangePrefetchPlanner;
DataKind, DataIndex, Priority: Integer; MaxBytes: Int64;
ReadAheadBytes: Integer; MaxPasses: Integer): Integer;
Function CancelPrefetch(JobHandle: Integer): Boolean;
Function WaitForPrefetch(JobHandle: Integer;
TimeoutMS: Cardinal): Integer;
Function GetPrefetchState(JobHandle: Integer): Integer;
Function GetPrefetchInfo(JobHandle: Integer;
Out Info: WideString): Boolean;
Function Write(Const Buffer; Count: Longint): Longint; Override;
Function Seek(Const Offset: Int64; Origin: TSeekOrigin): Int64; Override;
Property CacheLimitBytes: Int64 Read FCacheLimitBytes;
Property ChunkSize: Integer Read FChunkSize;
Property CoalesceWindowMS: Cardinal Read FCoalesceWindowMS;
Property Source: IPLByteRangeSource Read FSource;
End;Fields
| Create | Takes the source, chunk size, cache budget and coalesce window; a chunk size below 4096 selects 65536 bytes, a cache budget of zero or less selects 4 MiB and is raised to at least one chunk, and the coalesce window defaults to 2 ms |
|---|---|
| ReadAt | Reads up to Count bytes at an absolute offset without moving the stream cursor; the result can be short at the source end or after a failed source request |
| Read | Reads from the current position and advances it; the position has its own lock, so it does not interfere with ReadAt |
| Seek | Supports the three seek origins; out-of-range offsets clamp to 0 or the total source size, and only an unknown origin raises EStreamError |
| Write | Reports zero bytes written and stores nothing, because the source is read-only |
| IsRangeCached / IsRangeAvailable | Test whether an absolute range is covered by retained chunks, and whether the source reports it as immediately available |
| GetCacheStats / ClearCache | Return a consistent statistics snapshot, and drop cached chunks with an optional counter reset |
| SetCacheLimitBytes / SetCoalesceWindowMS | Change the LRU byte budget and the coalesce window; shrinking the budget evicts immediately |
| StartPrefetch / CancelPrefetch / WaitForPrefetch / GetPrefetchState / GetPrefetchInfo | Start a background planner job and track it by handle; a zero budget selects 64 MiB, a zero pass count selects 128 passes, and WaitForPrefetch returns 0 on timeout or an unknown handle |
| CacheLimitBytes / ChunkSize / CoalesceWindowMS / Source | Read-only properties reporting the effective budget, chunk size, coalesce window and the source interface |
Remarks
The source supplies the authoritative total size that bounds reads and seeks and performs every chunk fetch; the stream keeps its interface reference for its whole lifetime.
Adjacent misses queued within the coalesce window can merge into one source request, capped at 16 MiB, while positional Read calls never wait for coalescing.
The counters accumulate from construction until ClearCache(True); ClearCache(False) drops cached chunks but keeps the counters.
Cache access, the stream position and source calls have separate locks, so ReadAt is safe from concurrent threads and a prefetch job can run while the main thread reads; do not call back into the same stream from a source implementation.
See also
TByteRangeStreamReadAt, TByteRangeStreamAvailability, GetCacheStats, SetRangeSourceCacheLimit, LoadFromRangeSource, IPLByteRangeSource, ClearCache, IsRangeCached, StartRangeSourcePrefetch, TPDFByteRangeCacheStats