Virtual file registry

Document loading, virtual files

Description

The native PDFlibVirtualFile unit inspects and mutates registered in-memory files: PLGetVirtualFiles snapshots names and sizes, rename succeeds without replacing the destination, and process-wide byte and count quotas, job-prefix scoping, atomic copies, range access, checkpointed snapshots and revision-checked conditional replacement all apply. Use it to move job payloads between stages without touching disk.

Registry inspection

The native Pascal PDFlibVirtualFile unit provides PLGetVirtualFiles, returning an independent array of TPDFVirtualFileInfo records with the display Name and byte Size of every registered entry

Snapshot enumeration and file existence and size queries do not copy payload bytes. Editing a returned snapshot does not change the registry, and later registry changes do not change an earlier snapshot

Rename without replacement

Renamed:= PLRenameVirtualFile('mem://draft.pdf', 'mem://completed.pdf');

The function returns True only when the source exists and the destination is available. It changes the name under the registry lock without changing the contents or file count and without writing to disk

An occupied destination is never replaced. Names retain the registry's existing case-insensitive lookup policy, and a case-only rename updates display spelling. Invalid or missing source names return False

These are native Pascal unit functions; use the existing registration and retrieval APIs to exchange the underlying bytes

Capacity limits and job scopes

function PLSetVirtualFileLimits(MaxBytes: Int64; MaxFiles: Integer): Boolean;
procedure PLGetVirtualFileUsage(out Bytes, MaxBytes: Int64; out Files, MaxFiles: Integer);
function PLGetVirtualFilesByPrefix(const Prefix: WideString): TPDFVirtualFileInfos;
function PLDeleteVirtualFilesByPrefix(const Prefix: WideString): Integer;

Limits apply process-wide to registered payload bytes and file count; zero means unlimited, negative limits are invalid, and a limit below current usage is rejected without changing existing limits

Registration, copying, range edits, conditional replacement and snapshot restoration check capacity under the registry lock before changing any entry; a rejected replacement leaves the previous contents available; payload quotas do not include caller-owned copies or snapshots

Use a trailing slash in a job prefix such as mem://job-42/ to avoid matching neighboring names; prefix lookup follows the existing ASCII case-insensitive policy, and an empty prefix matches the entire registry

Copy, range access and consuming outputs

function PLCopyVirtualFile(const SourceName, TargetName: WideString;
  ReplaceExisting: Boolean): Boolean;
function PLReadVirtualFileRange(const FileName: WideString;
  Offset, Count: Int64; out Data: TBytes): Boolean;
function PLWriteVirtualFileRange(const FileName: WideString;
  Offset: Int64; const Data: TBytes): Boolean;
function PLTruncateVirtualFile(const FileName: WideString; NewSize: Int64): Boolean;
function PLTakeVirtualFile(const FileName: WideString; out Data: TBytes): Boolean;
function PLRegisterVirtualFileStreamRange(const FileName: WideString;
  Source: TStream; Offset, Count: Int64): Boolean;

Copy atomically checks the destination policy and makes independent payload bytes; an occupied destination is rejected unless ReplaceExisting is True

Range reads copy only the requested bytes; the entire range must fit the existing file, and a zero-length read at EOF succeeds; writes overwrite existing bytes or append at EOF, never create holes; truncation may only shrink an existing file

Offsets and counts are nonnegative and individual payloads remain limited to MaxInt bytes; invalid ranges, missing files and quota failures return False before mutation; allocation failures may raise an exception while retaining registered contents

PLTakeVirtualFile copies and deletes under one lock so one consumer receives the result; True with empty bytes means an existing empty file was consumed, while False means the file was absent

Stream-range registration reads exactly the specified absolute range from a seekable source and restores its original position before committing; a short read or seek failure returns False and preserves any previous registered file; the original stream registration function still consumes from its current position

Checkpoints and optimistic updates

function PLCaptureVirtualFiles(const Prefix: WideString): TPDFVirtualFileSnapshot;
function PLRestoreVirtualFiles(Snapshot: TPDFVirtualFileSnapshot): Boolean;
function PLVirtualFileRevision(const FileName: WideString): Int64;
function PLReplaceVirtualFileIfRevision(const FileName: WideString;
  ExpectedRevision: Int64; const Data: TBytes): Boolean;

A caller-owned snapshot stores independent names and payloads for its captured prefix; its Count, Name(Index) and Bytes(Index) accessors do not expose mutable registry state; free the snapshot after use

Restore replaces the complete captured scope, removing files created in that scope since capture and reinstating captured names and bytes; unrelated scopes remain unchanged; a current quota failure returns False with the complete registry untouched

Restoration prepares the replacement before swapping it under one lock and assigns fresh revisions; snapshots restore resources rather than the global limits or write mode

Inventory records also expose Revision; direct revision lookup returns zero for a missing file; successful registration, content edits, replacement, rename and restore assign a new positive process-wide revision

Read the revision associated with a staged input, then pass it to the conditional replacement function; only a still-existing entry with that revision can be updated, so stale writers cannot overwrite newer output

See also

Workflow data tools, RegisterVirtualFile, GetVirtualFileCount, GetVirtualFileSize