Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
window_source_model.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/model.hpp>
6#include <portals/window_source.hpp>
7
8namespace portals {
18template <class T>
19 requires std::is_trivially_copyable_v<T>
20class FiniteWindowProvider final : public WindowProvider<T> {
21public:
25 explicit FiniteWindowProvider(std::shared_ptr<const ModelSnapshot<T>> snapshot, bool seekRow = false)
26 : snapshot_(std::move(snapshot)), seekRow_(seekRow) {
27 }
28
30 SourceCapabilities capabilities() const noexcept override {
31 return {true, seekRow_, true};
32 }
33
39 std::expected<SourceReadReply<T>, SourceError> load(const SourceReadCommand &command, PageBuilder<T> &out,
40 std::stop_token stop) override {
41 if (!snapshot_) {
42 return std::unexpected(SourceError::INVALID_ARGUMENT);
43 }
44
45 if (command.dataRevision != snapshot_->revision()) {
46 return std::unexpected(SourceError::REVISION_CHANGED);
47 }
48
49 auto position = std::uint64_t{};
50
51 switch (command.anchor.kind) {
53 position = command.direction == SourceDirection::FORWARD ? 0 : snapshot_->rowCount();
54 break;
56 position = snapshot_->rowCount();
57 break;
59 if (command.anchor.cursor.token[1] != 0 || command.anchor.cursor.token[0] > snapshot_->rowCount()) {
60 return std::unexpected(SourceError::INVALID_CURSOR);
61 }
62
63 position = command.anchor.cursor.token[0];
64 break;
66 auto located = snapshot_->locateRow(command.anchor.row);
67
68 if (!seekRow_ || located.state == RowLookupState::UNSUPPORTED) {
69 return std::unexpected(SourceError::UNSUPPORTED);
70 }
71
72 if (located.state != RowLookupState::FOUND || located.index >= snapshot_->rowCount()) {
73 return std::unexpected(SourceError::ROW_NOT_FOUND);
74 }
75
76 position = located.index + (command.direction == SourceDirection::BACKWARD ? 1 : 0);
77 break;
78 }
79 }
80
81 const bool backwards = command.direction == SourceDirection::BACKWARD;
82 // TAIL addresses the available final window regardless of the traversal direction.
83 const bool tail = command.anchor.kind == SourceAnchorKind::TAIL;
84 auto available = backwards || tail ? position : snapshot_->rowCount() - position;
85 auto count = static_cast<std::size_t>(std::min<std::uint64_t>(available, command.maxRows));
86 auto first = backwards || tail ? position - count : position;
87 auto range = ModelRange<T>::read(snapshot_, {first, count}, command.maxRows);
88
89 if (!range) {
90 return std::unexpected(detail::sourceTreeError(range.error()));
91 }
92
93 if (range->state() == RangeState::PENDING) {
94 SourceReadReply<T> pending;
95 pending.observedRevision = command.dataRevision;
96
97 return pending;
98 }
99
100 if (range->state() == RangeState::FAILED) {
101 return std::unexpected(SourceError::CALLBACK_FAILED);
102 }
103
104 for (const auto &row : range->rows()) {
105 if (stop.stop_requested()) {
106 return std::unexpected(SourceError::CANCELLED);
107 }
108
109 auto appended = out.emplace(row.id, snapshot_->revision(), row.value);
110
111 if (!appended) {
112 return std::unexpected(appended.error());
113 }
114 }
115
116 auto edge =
117 (backwards ? first == 0 : first + count == snapshot_->rowCount()) ? SourceEdge::FINAL_END : SourceEdge::MORE;
118
119 return out.ready({command.source, {first, 0}}, {command.source, {first + count, 0}}, edge);
120 }
121
122private:
123 std::shared_ptr<const ModelSnapshot<T>> snapshot_;
124 bool seekRow_;
125};
126} // namespace portals
std::expected< SourceReadReply< T >, SourceError > load(const SourceReadCommand &command, PageBuilder< T > &out, std::stop_token stop) override
Definition window_source_model.hpp:39
SourceCapabilities capabilities() const noexcept override
Definition window_source_model.hpp:30
FiniteWindowProvider(std::shared_ptr< const ModelSnapshot< T > > snapshot, bool seekRow=false)
Definition window_source_model.hpp:25
static std::expected< ModelRange, TreeError > read(std::shared_ptr< const ModelSnapshot< T > > owner, RowRange range, std::size_t max_rows)
Read only memory and retain its snapshot. Bounds/provider contract failures are explicit....
Definition model.hpp:244
Immutable list/table provider storage, independent of view/source lifetime. Implement read as bounded...
Definition model.hpp:94
One reserved worker-only builder. Never expose its writable resource after publication.
Definition window_source.hpp:224
std::expected< void, SourceError > emplace(RowId id, std::uint64_t revision, Args &&...args)
Definition window_source.hpp:242
std::expected< SourceReadReply< T >, SourceError > ready(SourceCursor before, SourceCursor after, SourceEdge edge)
Definition window_source.hpp:278
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
@ TAIL
Capability-gated available-tail window.
Definition window_source.hpp:55
@ ROW
Capability-gated stable-identity lookup.
Definition window_source.hpp:56
@ ENTRY
Provider entry, without a global-origin promise.
Definition window_source.hpp:53
@ CURSOR
Valid gap token in this immutable session.
Definition window_source.hpp:54
@ FAILED
Provider failure is represented by snapshot-owned text and no rows.
Definition model.hpp:48
@ PENDING
Data is not ready; reading must not block or start work.
Definition model.hpp:46
@ FOUND
The index identifies this RowId in the same snapshot.
Definition model.hpp:74
@ UNSUPPORTED
Provider does not implement an identity index; ordinal scrolling remains available.
Definition model.hpp:72
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ INVALID_CURSOR
The provider cannot validate this opaque token.
Definition source_budget.hpp:25
@ CALLBACK_FAILED
Provider construction/preparation threw an unexpected exception.
Definition source_budget.hpp:21
@ UNSUPPORTED
A required optional capability is unavailable.
Definition source_budget.hpp:23
@ REVISION_CHANGED
Requested provider revision is unavailable or a notification is stale.
Definition source_budget.hpp:26
@ CANCELLED
Explicit navigation cancellation or cooperative worker cancellation.
Definition source_budget.hpp:35
@ ROW_NOT_FOUND
A supported stable-identity lookup did not find its row.
Definition source_budget.hpp:28
@ INVALID_ARGUMENT
Invalid options, enum, identity or command shape.
Definition source_budget.hpp:15
@ BACKWARD
Read before the requested gap.
Definition window_source.hpp:49
@ FORWARD
Read after the requested gap.
Definition window_source.hpp:48
@ FINAL_END
Definitive boundary in this direction for this session.
Definition window_source.hpp:62
@ MORE
Continuation exists; short/empty pages do not imply end.
Definition window_source.hpp:60
RowId row
Used for ROW only.
Definition window_source.hpp:69
SourceCursor cursor
Used for CURSOR only.
Definition window_source.hpp:68
SourceAnchorKind kind
Operation to resolve on a worker.
Definition window_source.hpp:67
Immutable navigation capabilities of the delivery channel, not of the complete model.
Definition window_source.hpp:73
std::array< std::uint64_t, 2 > token
Provider-validated gap representation.
Definition window_source.hpp:39
Immutable worker command; maxRows is an upper bound, never an extent.
Definition window_source.hpp:90
SourceAnchor anchor
Latest bounded navigation intent.
Definition window_source.hpp:93
SourceStamp source
Expected session namespace.
Definition window_source.hpp:91
std::uint64_t dataRevision
Exact requested readiness/data revision.
Definition window_source.hpp:92
SourceDirection direction
Traversal direction.
Definition window_source.hpp:94
std::size_t maxRows
Positive remaining demand, capped per page.
Definition window_source.hpp:95
Owning result with fixed metadata; no optional/variant deep copies.
Definition window_source.hpp:212
std::uint64_t observedRevision
PENDING must match the command revision.
Definition window_source.hpp:216