Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
document_source.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4#include <filesystem>
5#include <memory>
6#include <portals/log_file_error.hpp>
7#include <portals/window_source.hpp>
8#include <string>
9#include <system_error>
10
11namespace portals {
15 std::error_code native;
16 std::optional<FileLogFailure> fileFailure;
17 std::optional<SourceBudgetUsage>
19 std::optional<SourceBudgetUsage> budgetPeak{};
21};
22
26 std::uint64_t dataRevision{};
27 bool operator==(const DocumentRevision &) const noexcept = default;
28};
29
32 bool seekable{true};
33 bool immutable{true};
34 bool readSessions{};
35};
36
39 std::uint64_t dataRevision{};
40 std::optional<std::uint64_t> byteExtent;
41};
42
49
52 std::uint64_t dataRevision{};
53 std::uint64_t offset{};
54};
55
58 std::size_t bytesRead{};
59 bool finalEnd{};
60};
61
67public:
68 virtual ~ByteDocumentReadSession() = default;
70 virtual ByteDocumentMetadata metadata() const noexcept = 0;
76 virtual std::expected<ByteReadResult, DocumentError> read(const ByteReadCommand &command,
77 std::span<std::byte> destination, std::stop_token stop) = 0;
81 virtual std::expected<void, DocumentError> seal(std::stop_token stop) = 0;
82};
83
89public:
90 virtual ~ByteDocumentProvider() = default;
92 virtual ByteDocumentCapabilities capabilities() const noexcept = 0;
93
98 virtual std::size_t pinnedBudgetBytes(const SourceBudget &root) const noexcept {
99 (void)root;
100 return 0;
101 }
102
107 virtual bool tryReleaseOptionalStorage() noexcept {
108 return false;
109 }
110
114 virtual std::expected<ByteDocumentMetadata, DocumentError> metadata(std::stop_token stop) = 0;
115
122 virtual std::expected<ByteDocumentMetadata, DocumentError> discoverExtent(std::stop_token stop) {
123 return metadata(stop);
124 }
125
131 virtual std::expected<ByteReadResult, DocumentError> read(const ByteReadCommand &command,
132 std::span<std::byte> destination, std::stop_token stop) = 0;
133
138 virtual std::expected<std::unique_ptr<ByteDocumentReadSession>, DocumentError> openReadSession(std::stop_token stop) {
139 (void)stop;
140 return std::unexpected(DocumentError{SourceError::UNSUPPORTED, {}, {}});
141 }
142};
143
146public:
150 explicit MemoryDocumentProvider(std::shared_ptr<const std::string> text, std::uint64_t revision = 1);
151 ByteDocumentCapabilities capabilities() const noexcept override;
152 std::expected<ByteDocumentMetadata, DocumentError>
153 metadata(std::stop_token stop) override;
154 std::expected<ByteReadResult, DocumentError> read(const ByteReadCommand &command, std::span<std::byte> destination,
155 std::stop_token stop)
156 override;
158private:
159 std::shared_ptr<const std::string> text_;
160 std::uint64_t revision_;
161};
162
163class FileLogModel;
164struct FileLogMetadata;
165
170class FileDocumentProvider final : public ByteDocumentProvider {
171public:
176 static std::expected<std::shared_ptr<FileDocumentProvider>, DocumentError>
177 create(std::filesystem::path path, std::size_t ioBlockBytes = 64 * 1024);
181 static std::expected<std::shared_ptr<FileDocumentProvider>, DocumentError>
182 create(std::shared_ptr<FileLogModel> model);
183 ByteDocumentCapabilities capabilities() const noexcept override;
184 std::expected<ByteDocumentMetadata, DocumentError>
185 metadata(std::stop_token stop) override;
186 std::expected<ByteReadResult, DocumentError> read(const ByteReadCommand &command, std::span<std::byte> destination,
187 std::stop_token stop)
188 override;
190
193 std::expected<std::unique_ptr<ByteDocumentReadSession>, DocumentError> openReadSession(std::stop_token stop) override;
194
195private:
196 explicit FileDocumentProvider(std::shared_ptr<FileLogModel> model);
197 std::expected<ByteDocumentMetadata, DocumentError> observeMetadata(FileLogMetadata observed);
198 std::shared_ptr<FileLogModel> model_;
199 std::uint64_t fileGeneration_{};
200 std::uint64_t byteExtent_{};
201 std::uint64_t revision_{};
202};
203
206 std::size_t maxPageBytes{64 * 1024};
207 std::size_t envelopeBytes{4096};
208};
209
213 std::uint64_t offset{};
214 std::size_t maxBytes{};
215 std::uint64_t generation{};
216};
217
219namespace detail {
220struct DocumentPageData;
221}
222
224
227public:
228 DocumentBytePage() = default;
229 std::span<const std::byte> bytes() const noexcept;
230 DocumentRevision revision() const noexcept;
231 std::uint64_t offset() const noexcept;
232 std::uint64_t generation() const noexcept;
233 bool finalEnd() const noexcept;
234private:
235 friend class ByteDocumentSource;
236 explicit DocumentBytePage(std::shared_ptr<const detail::DocumentPageData> data);
237 std::shared_ptr<const detail::DocumentPageData> data_;
238};
239
245class ByteDocumentSource {
246public:
252 static std::expected<std::shared_ptr<ByteDocumentSource>, DocumentError>
253 create(std::shared_ptr<ByteDocumentProvider> provider, std::shared_ptr<SourceBudget> budget,
254 DocumentSourceOptions options = {});
255
257 std::shared_ptr<SourceBudget> budget() const noexcept {
258 return budget_;
259 }
260
261 SourceStamp stamp() const noexcept;
263 std::size_t maxPageBytes() const noexcept;
264
266 std::size_t envelopeBytes() const noexcept {
267 return options_.envelopeBytes;
268 }
269
271 std::size_t pinnedBudgetBytes() const noexcept;
274
278 std::expected<DocumentMetadata, DocumentError> inspect(std::stop_token stop = {});
283 std::expected<DocumentMetadata, DocumentError> inspectEnd(std::stop_token stop = {});
284
289 std::expected<DocumentBytePage, DocumentError> read(DocumentReadRequest request, std::stop_token stop = {});
290
291private:
292 friend class DocumentReadBatch;
293 std::expected<DocumentBytePage, DocumentError> readImpl(DocumentReadRequest request, std::stop_token stop,
294 ByteDocumentReadSession *session);
295 std::expected<DocumentMetadata, DocumentError> inspectOnWorker(std::stop_token stop, bool discoverEnd);
296 ByteDocumentSource(std::shared_ptr<ByteDocumentProvider> provider, std::shared_ptr<SourceBudget> budget,
297 SourceCredit credit, DocumentSourceOptions options, SourceStamp stamp);
298 std::shared_ptr<ByteDocumentProvider> provider_;
299 std::shared_ptr<SourceBudget> budget_;
300 SourceCredit credit_;
301 DocumentSourceOptions options_;
302 SourceStamp stamp_;
303 ByteDocumentCapabilities capabilities_;
304 std::thread::id ownerThread_;
305 std::mutex providerMutex_;
306};
307
314public:
315 static constexpr std::size_t READ_BATCH_METADATA_BYTES = 4096;
320 static std::expected<DocumentReadBatch, DocumentError> create(std::shared_ptr<ByteDocumentSource> source,
321 std::stop_token stop = {});
323 DocumentReadBatch(DocumentReadBatch &&other) noexcept = default;
326 DocumentReadBatch(const DocumentReadBatch &) = delete;
327 ~DocumentReadBatch() = default;
328
330 const DocumentMetadata &metadata() const noexcept {
331 return metadata_;
332 }
333
338 std::expected<DocumentBytePage, DocumentError> read(DocumentReadRequest request, std::stop_token stop = {});
342 std::expected<void, DocumentError> seal(std::stop_token stop = {});
343
344private:
345 DocumentReadBatch(SourceCredit credit, std::shared_ptr<ByteDocumentSource> source,
346 std::unique_ptr<ByteDocumentReadSession> session, DocumentMetadata metadata);
347 SourceCredit credit_; // Backend storage is destroyed before returning this credit.
348 std::shared_ptr<ByteDocumentSource> source_;
349 std::unique_ptr<ByteDocumentReadSession> session_;
350 DocumentMetadata metadata_;
351 std::thread::id workerThread_;
352 std::optional<DocumentError> failure_;
353 bool sealed_{};
354};
355
363
370
373 std::uint64_t line{};
374 std::uint64_t offset{};
375};
376
382
385 std::uint64_t scannedBytes{};
386 std::uint64_t completedLines{};
387 std::optional<std::uint64_t> exactLineCount;
388 std::size_t checkpoints{};
389};
390
393 std::size_t maxCheckpoints{256};
394 std::uint64_t checkpointBytes{1024 * 1024};
395 std::size_t scanBytes{64 * 1024};
397};
398
403class SparseDocumentIndex {
404public:
410 static std::expected<std::unique_ptr<SparseDocumentIndex>, DocumentError>
411 create(std::shared_ptr<SourceBudget> budget, DocumentRevision revision, DocumentIndexOptions options = {});
413 DocumentRevision revision() const noexcept;
414 DocumentLinePolicy policy() const noexcept;
419 DocumentCheckpoint checkpointBeforeByte(std::uint64_t offset) const noexcept;
424 std::expected<DocumentIndexProgress, DocumentError> advance(ByteDocumentSource &source, std::stop_token stop = {});
430 std::expected<DocumentLineLocation, DocumentError> locateLine(ByteDocumentSource &source, std::uint64_t line,
431 std::stop_token stop = {});
432
433private:
434 struct Cursor {
435 std::uint64_t offset{};
436 std::uint64_t line{};
437 std::uint64_t lineStart{};
438 bool pendingCR{};
439 bool ended{};
440 };
441
442 SparseDocumentIndex(std::shared_ptr<SourceArena> arena, DocumentRevision revision, DocumentIndexOptions options);
443 std::expected<void, DocumentError> validate(ByteDocumentSource &source, std::stop_token stop);
444 std::expected<void, DocumentError> scan(ByteDocumentSource &source, Cursor &cursor,
445 std::optional<std::uint64_t> target, bool extendPrefix, std::stop_token stop);
446 std::expected<void, DocumentError> nextLine(Cursor &cursor, std::uint64_t start, bool extendPrefix);
447 void addCheckpoint(DocumentCheckpoint checkpoint) noexcept;
448 DocumentCheckpoint checkpointBeforeLine(std::uint64_t line) const noexcept;
449 std::shared_ptr<SourceArena> arena_;
450 DocumentCheckpoint *checkpoints_{};
451 std::size_t checkpointCount_{1};
452 DocumentRevision revision_;
453 DocumentIndexOptions options_;
454 std::uint64_t interval_;
455 Cursor prefix_;
456 Cursor lookup_;
457 std::optional<std::uint64_t> lookupTarget_;
458};
459} // namespace portals
Read-only worker provider; callers serialize operations using the existing executor....
Definition document_source.hpp:88
virtual std::size_t pinnedBudgetBytes(const SourceBudget &root) const noexcept
Definition document_source.hpp:98
virtual std::expected< ByteDocumentMetadata, DocumentError > metadata(std::stop_token stop)=0
virtual ByteDocumentCapabilities capabilities() const noexcept=0
virtual std::expected< std::unique_ptr< ByteDocumentReadSession >, DocumentError > openReadSession(std::stop_token stop)
Definition document_source.hpp:138
virtual ~ByteDocumentProvider()=default
Safe final destruction after worker release.
virtual bool tryReleaseOptionalStorage() noexcept
Definition document_source.hpp:107
virtual std::expected< ByteDocumentMetadata, DocumentError > discoverExtent(std::stop_token stop)
Definition document_source.hpp:122
virtual std::expected< ByteReadResult, DocumentError > read(const ByteReadCommand &command, std::span< std::byte > destination, std::stop_token stop)=0
One worker-owned provisional read transaction; destroy it on its creating worker. A successful seal q...
Definition document_source.hpp:66
virtual ByteDocumentMetadata metadata() const noexcept=0
virtual std::expected< void, DocumentError > seal(std::stop_token stop)=0
virtual std::expected< ByteReadResult, DocumentError > read(const ByteReadCommand &command, std::span< std::byte > destination, std::stop_token stop)=0
virtual ~ByteDocumentReadSession()=default
Release worker-owned backend storage.
Cold source; inspect/read reject its creating (UI) thread. Dispatch synchronous operations through th...
Definition document_source.hpp:245
std::expected< DocumentBytePage, DocumentError > read(DocumentReadRequest request, std::stop_token stop={})
ByteDocumentCapabilities capabilities() const noexcept
std::expected< DocumentMetadata, DocumentError > inspect(std::stop_token stop={})
std::expected< DocumentMetadata, DocumentError > inspectEnd(std::stop_token stop={})
std::size_t maxPageBytes() const noexcept
std::size_t pinnedBudgetBytes() const noexcept
static std::expected< std::shared_ptr< ByteDocumentSource >, DocumentError > create(std::shared_ptr< ByteDocumentProvider > provider, std::shared_ptr< SourceBudget > budget, DocumentSourceOptions options={})
SourceStamp stamp() const noexcept
std::shared_ptr< SourceBudget > budget() const noexcept
Definition document_source.hpp:257
std::size_t envelopeBytes() const noexcept
Definition document_source.hpp:266
bool tryReleaseOptionalStorage() noexcept
DocumentRevision revision() const noexcept
std::uint64_t offset() const noexcept
std::span< const std::byte > bytes() const noexcept
std::uint64_t generation() const noexcept
DocumentBytePage()=default
Empty allocation-free pin.
bool finalEnd() const noexcept
Explicit source-owning provisional batch, confined to its creating worker. Read pages can outlive the...
Definition document_source.hpp:313
std::expected< void, DocumentError > seal(std::stop_token stop={})
std::expected< DocumentBytePage, DocumentError > read(DocumentReadRequest request, std::stop_token stop={})
DocumentReadBatch(DocumentReadBatch &&other) noexcept=default
DocumentReadBatch & operator=(DocumentReadBatch other) noexcept
~DocumentReadBatch()=default
Destroy on the creating worker; does not terminate on misuse.
static constexpr std::size_t READ_BATCH_METADATA_BYTES
Held only by an actual supported batch.
Definition document_source.hpp:315
static std::expected< DocumentReadBatch, DocumentError > create(std::shared_ptr< ByteDocumentSource > source, std::stop_token stop={})
const DocumentMetadata & metadata() const noexcept
Definition document_source.hpp:330
std::expected< ByteDocumentMetadata, DocumentError > metadata(std::stop_token stop) override
static std::expected< std::shared_ptr< FileDocumentProvider >, DocumentError > create(std::filesystem::path path, std::size_t ioBlockBytes=64 *1024)
static std::expected< std::shared_ptr< FileDocumentProvider >, DocumentError > create(std::shared_ptr< FileLogModel > model)
std::expected< ByteReadResult, DocumentError > read(const ByteReadCommand &command, std::span< std::byte > destination, std::stop_token stop) override
ByteDocumentCapabilities capabilities() const noexcept override
std::expected< std::unique_ptr< ByteDocumentReadSession >, DocumentError > openReadSession(std::stop_token stop) override
File-backed rich model with optional bounded WindowSource delivery. Creation owns the path without op...
Definition log_file.hpp:74
MemoryDocumentProvider(std::shared_ptr< const std::string > text, std::uint64_t revision=1)
std::expected< ByteReadResult, DocumentError > read(const ByteReadCommand &command, std::span< std::byte > destination, std::stop_token stop) override
ByteDocumentCapabilities capabilities() const noexcept override
std::expected< ByteDocumentMetadata, DocumentError > metadata(std::stop_token stop) override
Thread-safe credit ledger; final page/worker release may happen on any thread.
Definition source_budget.hpp:119
Move-only credit; backing storage must be destroyed before this final credit owner.
Definition source_budget.hpp:64
std::expected< DocumentIndexProgress, DocumentError > advance(ByteDocumentSource &source, std::stop_token stop={})
DocumentIndexProgress progress() const noexcept
DocumentCheckpoint checkpointBeforeByte(std::uint64_t offset) const noexcept
DocumentRevision revision() const noexcept
std::expected< DocumentLineLocation, DocumentError > locateLine(ByteDocumentSource &source, std::uint64_t line, std::stop_token stop={})
DocumentLinePolicy policy() const noexcept
static std::expected< std::unique_ptr< SparseDocumentIndex >, DocumentError > create(std::shared_ptr< SourceBudget > budget, DocumentRevision revision, DocumentIndexOptions options={})
~SparseDocumentIndex()
Release fixed arena and its credit.
Public Portals API.
Definition action.hpp:8
@ PENDING
Complete or abandon the owning session before submitting another frame.
Definition diff.hpp:108
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
DocumentLinePolicy
Policy qualifying every index checkpoint; UTF16 requires a decoder adapter.
Definition document_source.hpp:357
@ UTF16_BE
Unsupported by this bytewise index.
Definition document_source.hpp:361
@ UTF16_LE
Unsupported by this bytewise index.
Definition document_source.hpp:360
@ RAW_BYTES
CR/LF/CRLF byte separators without decoding.
Definition document_source.hpp:358
@ UTF8
UTF8 byte separators; offsets remain raw byte positions.
Definition document_source.hpp:359
DocumentLocationState
Precision of a requested line start.
Definition document_source.hpp:365
@ UNKNOWN
EOF proved that the line does not exist.
Definition document_source.hpp:368
@ PENDING
More bounded worker scans required.
Definition document_source.hpp:367
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ UNSUPPORTED
A required optional capability is unavailable.
Definition source_budget.hpp:23
@ INVALID_ARGUMENT
Invalid options, enum, identity or command shape.
Definition source_budget.hpp:15
@ UTF8
UTF-8.
Definition document_encoding.hpp:16
Capabilities captured without I/O during cold source creation.
Definition document_source.hpp:31
bool immutable
Provider revision never changes.
Definition document_source.hpp:33
bool readSessions
Worker-owned provisional read sessions are supported.
Definition document_source.hpp:34
bool seekable
Arbitrary byte offsets can be requested.
Definition document_source.hpp:32
Worker-observed provider metadata.
Definition document_source.hpp:38
std::optional< std::uint64_t > byteExtent
Exact extent, or unknown without a forced scan.
Definition document_source.hpp:40
std::uint64_t dataRevision
Nonzero exact revision.
Definition document_source.hpp:39
Exact provider request; destination size bounds the byte window.
Definition document_source.hpp:51
std::uint64_t dataRevision
Required revision; never silently substituted.
Definition document_source.hpp:52
std::uint64_t offset
First requested byte, including offsets above4GiB.
Definition document_source.hpp:53
Short reads alone never imply EOF.
Definition document_source.hpp:57
std::size_t bytesRead
Initialized prefix of the borrowed destination.
Definition document_source.hpp:58
bool finalEnd
Definitive EOF immediately after that prefix.
Definition document_source.hpp:59
Exact sparse anchor, never an approximate requested location.
Definition document_source.hpp:372
std::uint64_t offset
Exact line-start byte.
Definition document_source.hpp:374
std::uint64_t line
Zero-based logical line.
Definition document_source.hpp:373
Failure with an optional native provider cause, preserved by source operations.
Definition document_source.hpp:13
std::optional< FileLogFailure > fileFailure
Exact file semantic/operation cause when applicable.
Definition document_source.hpp:16
std::error_code native
Native operation cause, empty for protocol failures.
Definition document_source.hpp:15
SourceError code
Protocol/admission failure.
Definition document_source.hpp:14
std::optional< SourceBudgetUsage > budgetPeak
Definition document_source.hpp:19
std::optional< SourceBudgetUsage > budgetDemand
Exact failed reservation on this source root; absent for provider waiting.
Definition document_source.hpp:18
Fixed RAM and per-call worker bounds.
Definition document_source.hpp:392
DocumentLinePolicy policy
Fixed newline policy identity.
Definition document_source.hpp:396
std::size_t maxCheckpoints
At least2 anchors, reserved once.
Definition document_source.hpp:393
std::size_t scanBytes
Maximum read payload per advance/lookup call.
Definition document_source.hpp:395
std::uint64_t checkpointBytes
Initial byte distance between stored line starts.
Definition document_source.hpp:394
Prefix knowledge; exact total exists only after EOF.
Definition document_source.hpp:384
std::optional< std::uint64_t > exactLineCount
Includes trailing empty line, only at EOF.
Definition document_source.hpp:387
std::uint64_t scannedBytes
Examined contiguous prefix.
Definition document_source.hpp:385
std::size_t checkpoints
Bounded resident anchor count.
Definition document_source.hpp:388
std::uint64_t completedLines
Separators proven in that prefix.
Definition document_source.hpp:386
Bounded lookup result.
Definition document_source.hpp:378
DocumentCheckpoint checkpoint
Requested start if EXACT; preceding anchor otherwise.
Definition document_source.hpp:380
DocumentLocationState state
Precision/availability.
Definition document_source.hpp:379
Metadata qualified by the source namespace.
Definition document_source.hpp:44
DocumentRevision revision
Exact namespace/provider revision.
Definition document_source.hpp:45
std::optional< std::uint64_t > byteExtent
Exact byte extent when known.
Definition document_source.hpp:46
ByteDocumentCapabilities capabilities
Captured immutable capabilities.
Definition document_source.hpp:47
Owning request dispatched through the existing executor.
Definition document_source.hpp:211
DocumentRevision revision
Required exact namespace/revision.
Definition document_source.hpp:212
std::uint64_t offset
First requested byte.
Definition document_source.hpp:213
std::uint64_t generation
Caller navigation generation echoed without interpretation.
Definition document_source.hpp:215
std::size_t maxBytes
Positive bounded payload size.
Definition document_source.hpp:214
Exact document namespace and provider revision.
Definition document_source.hpp:24
bool operator==(const DocumentRevision &) const noexcept=default
SourceStamp source
Immutable source namespace.
Definition document_source.hpp:25
std::uint64_t dataRevision
Nonzero provider revision.
Definition document_source.hpp:26
Page bounds limit windows, never logical line length.
Definition document_source.hpp:205
std::size_t envelopeBytes
Source/work metadata allowance; minimum4096.
Definition document_source.hpp:207
std::size_t maxPageBytes
Maximum byte payload per read.
Definition document_source.hpp:206
Worker-observed byte metadata; byteExtent is never a row count.
Definition log_file.hpp:37
Immutable source namespace; replacement sessions must share their old budget root.
Definition window_source.hpp:27