Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
log_file.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/log_file_error.hpp>
6#include <portals/log_navigation.hpp>
7#include <portals/window_source.hpp>
8
9#include <filesystem>
10#include <memory_resource>
11#include <mutex>
12#include <optional>
13#include <string>
14#include <string_view>
15
16namespace portals {
18struct LogLine {
19 std::uint64_t byteOffset{};
20 std::pmr::string text;
21
25 LogLine(std::uint64_t offset, std::string_view utf8, std::pmr::memory_resource *resource);
26};
27
30 std::size_t maxRecordBytes{32 * 1024};
31 std::size_t ioBlockBytes{16 * 1024};
32 std::size_t maxSearchBytes{1024 * 1024};
33 std::size_t maxSearchPatternBytes{4096};
34};
35
38 std::uint64_t byteExtent{};
39 std::uint64_t generation{};
40};
41
44 std::optional<std::uint64_t> matchByteOffset;
45 std::uint64_t nextByteOffset{};
46 std::uint64_t generation{};
47 bool reachedEnd{};
48};
49
63
64namespace detail {
65class FileReadSessionImpl;
66}
67
74class FileLogModel final : public WindowProvider<LogLine>, public LogBookmarkProvider {
75public:
82 static std::expected<std::shared_ptr<FileLogModel>, FileLogFailure>
83 create(std::filesystem::path path, FileLogOptions options = {}, ModelResource continuation = {});
85 SourceCapabilities capabilities() const noexcept override;
87 ModelResource resource() const noexcept override;
92 std::expected<ModelBookmark, SourceError> capture(const SourceReadCommand &command, const WindowRow<LogLine> &row,
93 BookmarkOptions options, std::stop_token stop = {}) const override;
98 std::expected<PositionReply, SourceError> resolve(const PositionRequest &request,
99 std::stop_token stop = {}) const override;
107 std::expected<SourceReadReply<LogLine>, SourceError> load(const SourceReadCommand &command, PageBuilder<LogLine> &out,
108 std::stop_token stop) override;
113 std::expected<SourceReadReply<LogLine>, FileLogReadFailure> loadFile(const SourceReadCommand &command,
114 PageBuilder<LogLine> &out, std::stop_token stop);
116 std::expected<FileLogMetadata, FileLogFailure> metadata(std::stop_token stop = {});
122 std::expected<std::size_t, FileLogFailure> readBytes(std::uint64_t byteOffset, std::span<std::byte> destination,
123 std::stop_token stop = {});
130 std::expected<FileLogSearchResult, FileLogFailure> findLiteral(std::string_view pattern, std::uint64_t byteOffset,
131 std::size_t maxScanBytes, std::stop_token stop = {});
135 std::optional<FileLogFailure> lastFailure() const noexcept;
137 FileLogOptions options() const noexcept;
138
139private:
140 friend class detail::FileReadSessionImpl;
141
142 struct Observation {
143 std::uint64_t identityHigh{};
144 std::uint64_t identityLow{};
145 std::uint64_t byteExtent{};
146 std::uint64_t modified{};
147 };
148
149 FileLogModel(std::filesystem::path path, FileLogOptions options, ModelResource resource, SourceStamp instance);
150 std::expected<std::uint64_t, FileLogFailure> beginOperation(std::stop_token stop) const;
151 std::expected<std::uint64_t, FileLogFailure> observe(Observation observation, std::uint64_t operation,
152 std::stop_token stop) const;
153 void recordFailure(FileLogFailure failure) noexcept;
154 std::filesystem::path path_;
155 FileLogOptions options_;
156 ModelResource resource_;
157 SourceStamp instance_;
158 mutable std::mutex mutex_;
159 mutable Observation observed_;
160 mutable std::uint64_t generation_{};
161 mutable std::uint64_t operationSequence_{};
162 mutable std::uint64_t observedOperation_{};
163 std::optional<FileLogFailure> failure_;
164};
165} // namespace portals
File-backed rich model with optional bounded WindowSource delivery. Creation owns the path without op...
Definition log_file.hpp:74
std::optional< FileLogFailure > lastFailure() const noexcept
std::expected< FileLogSearchResult, FileLogFailure > findLiteral(std::string_view pattern, std::uint64_t byteOffset, std::size_t maxScanBytes, std::stop_token stop={})
std::expected< SourceReadReply< LogLine >, FileLogReadFailure > loadFile(const SourceReadCommand &command, PageBuilder< LogLine > &out, std::stop_token stop)
std::expected< SourceReadReply< LogLine >, SourceError > load(const SourceReadCommand &command, PageBuilder< LogLine > &out, std::stop_token stop) override
static std::expected< std::shared_ptr< FileLogModel >, FileLogFailure > create(std::filesystem::path path, FileLogOptions options={}, ModelResource continuation={})
std::expected< PositionReply, SourceError > resolve(const PositionRequest &request, std::stop_token stop={}) const override
std::expected< FileLogMetadata, FileLogFailure > metadata(std::stop_token stop={})
FileLogOptions options() const noexcept
std::expected< std::size_t, FileLogFailure > readBytes(std::uint64_t byteOffset, std::span< std::byte > destination, std::stop_token stop={})
SourceCapabilities capabilities() const noexcept override
std::expected< ModelBookmark, SourceError > capture(const SourceReadCommand &command, const WindowRow< LogLine > &row, BookmarkOptions options, std::stop_token stop={}) const override
ModelResource resource() const noexcept override
Bounded worker extension for log positions; unrelated to total extent. The provider owns its logical ...
Definition log_navigation.hpp:26
Copyable bookmark owning metadata only; never retains model, snapshot, view or page....
Definition model_position.hpp:66
One reserved worker-only builder. Never expose its writable resource after publication.
Definition window_source.hpp:224
Worker provider; the source captures capabilities once and never calls load on UI.
Definition window_source.hpp:323
Public Portals API.
Definition action.hpp:8
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ INVALID_ARGUMENT
Invalid options, enum, identity or command shape.
Definition source_budget.hpp:15
Checked bookmark capacity, independent of logical source length.
Definition model_position.hpp:57
Copyable diagnostic; nativeCode is errno/GetLastError for native failures, otherwise zero.
Definition log_file_error.hpp:31
Worker-observed byte metadata; byteExtent is never a row count.
Definition log_file.hpp:37
std::uint64_t generation
Positive model-local identity; changes on rotation/truncation.
Definition log_file.hpp:39
std::uint64_t byteExtent
Exact extent observed on an opened file handle.
Definition log_file.hpp:38
Fixed bounds independent of file length; no row index is constructed.
Definition log_file.hpp:29
std::size_t maxSearchPatternBytes
Maximum literal pattern; ceiling 4096 bytes.
Definition log_file.hpp:33
std::size_t maxRecordBytes
Maximum record payload; ceiling 64 KiB.
Definition log_file.hpp:30
std::size_t ioBlockBytes
Single-operation block cache; ceiling 64 KiB.
Definition log_file.hpp:31
std::size_t maxSearchBytes
Maximum bytes examined by one literal-search call.
Definition log_file.hpp:32
Exact failure of one worker load; safe to copy into that job's completion.
Definition log_file.hpp:51
SourceError sourceError
Delivery protocol failure.
Definition log_file.hpp:52
FileLogReadFailure(SourceError error)
Definition log_file.hpp:56
FileLogReadFailure(SourceError error, FileLogFailure cause)
Definition log_file.hpp:60
std::optional< FileLogFailure > fileFailure
Native/domain cause, absent for protocol-only failures.
Definition log_file.hpp:53
One bounded search step; repeat from nextByteOffset when reachedEnd is false.
Definition log_file.hpp:43
std::optional< std::uint64_t > matchByteOffset
First literal match within this step.
Definition log_file.hpp:44
std::uint64_t generation
Generation searched by this step.
Definition log_file.hpp:46
bool reachedEnd
This opened file's observed byte boundary was reached.
Definition log_file.hpp:47
std::uint64_t nextByteOffset
Resume position, including overlap needed for a split match.
Definition log_file.hpp:45
Immutable UTF-8 record with its original byte position; separators are excluded.
Definition log_file.hpp:18
std::uint64_t byteOffset
Position of the first byte in the source file generation.
Definition log_file.hpp:19
std::pmr::string text
Owning UTF-8 payload allocated in the delivery page.
Definition log_file.hpp:20
LogLine(std::uint64_t offset, std::string_view utf8, std::pmr::memory_resource *resource)
Logical resource namespace, distinct from its current delivery session. Reuse an existing SourceStamp...
Definition model_position.hpp:21
Immutable qualified navigation request; does not start work by itself.
Definition model_position.hpp:134
Immutable navigation capabilities of the delivery channel, not of the complete model.
Definition window_source.hpp:73
Immutable worker command; maxRows is an upper bound, never an extent.
Definition window_source.hpp:90
Immutable source namespace; replacement sessions must share their old budget root.
Definition window_source.hpp:27
Immutable typed row; use arena storage for all dynamically owned payload.
Definition window_source.hpp:101