5#include <portals/model.hpp>
6#include <portals/model_position.hpp>
12std::expected<PositionHistoryReply, SourceError> modelPositionHistory(
const ModelSnapshot<T> &snapshot,
13 std::uint64_t revision)
noexcept {
15 auto owner = snapshot.positionHistory();
17 return PositionHistoryReply{};
19 auto reply = owner->changesFrom(revision);
21 return std::unexpected(reply.error());
30 (reply->changes.fromRevision() != revision || reply->changes.toRevision() != snapshot.revision() ||
31 reply->changes.afterCount() != snapshot.rowCount())) {
35 }
catch (
const std::bad_alloc &) {
65 if (!snapshot || resource.identity.instance == 0 || resource.identity.epoch == 0 || resource.lineage == 0 ||
66 session.
instance == 0 || session.
epoch == 0 || snapshot->revision() == 0 ||
67 (changes && (changes.toRevision() != snapshot->revision() || changes.afterCount() != snapshot->rowCount()))) {
71 return FinitePositionResolver(resource, session, snapshot, std::move(changes));
79 std::expected<ModelBookmark, SourceError>
capture(std::uint64_t index,
82 auto snapshot = snapshot_.lock();
83 const auto resource = resource_;
89 auto row = readRow(snapshot, index);
92 return std::unexpected(row.error());
109 std::stop_token stop = {})
const override {
114 if (request.
session != session_) {
118 if (stop.stop_requested()) {
122 auto snapshot = snapshot_.lock();
128 if (request.
revision != snapshot->revision()) {
132 auto reply = receipt(request);
135 if (request.
bookmark.
resource() != resource_ || position.revision > snapshot->revision()) {
148 return resolveGap(reply, position, *snapshot, stop);
153 const auto changes = changes_;
156 return resolveItem(reply, position, snapshot, changes, stop);
157 }
catch (
const std::bad_alloc &) {
165 FinitePositionResolver(ModelResource resource, SourceStamp session,
166 const std::shared_ptr<
const ModelSnapshot<T>> &snapshot, PositionChangeMap changes)
167 : resource_(resource), session_(session), snapshot_(snapshot), changes_(std::move(changes)) {
170 PositionReply receipt(
const PositionRequest &request)
const noexcept {
174 static std::expected<std::optional<RowId>,
SourceError>
175 readRow(
const std::shared_ptr<
const ModelSnapshot<T>> &snapshot, std::uint64_t index) {
176 if (index >= snapshot->rowCount()) {
183 return std::unexpected(detail::sourceTreeError(range.error()));
187 return std::optional<RowId>{};
194 const auto id = range->rows().front().id;
196 if (
id.value == 0 ||
id.generation == 0) {
200 return std::optional<RowId>{
id};
203 std::expected<PositionReply, SourceError> resolveGap(PositionReply reply, ModelPosition position,
204 const ModelSnapshot<T> &snapshot,
205 std::stop_token stop)
const noexcept {
206 auto changes = changes_;
207 if (position.revision != snapshot.revision()) {
208 if (!changes || changes.fromRevision() != position.revision) {
209 auto history = detail::modelPositionHistory(snapshot, position.revision);
210 if (stop.stop_requested()) {
214 return std::unexpected(history.error());
220 changes = std::move(history->changes);
226 auto mapped = changes.map(position);
229 return std::unexpected(mapped.error());
232 position.offset = mapped->offset;
235 if (position.offset > snapshot.rowCount()) {
239 position.revision = snapshot.revision();
241 reply.position = position;
246 static std::expected<PositionReply, SourceError> resolveItem(PositionReply reply, ModelPosition position,
247 const std::shared_ptr<
const ModelSnapshot<T>> &snapshot,
248 const PositionChangeMap &provided,
249 std::stop_token stop) {
250 const auto found = snapshot->locateRow(position.row);
252 if (stop.stop_requested()) {
262 auto offset = found.index;
266 auto changes = provided;
267 if (!changes || changes.fromRevision() != position.revision) {
268 auto history = detail::modelPositionHistory(*snapshot, position.revision);
269 if (stop.stop_requested()) {
273 return std::unexpected(history.error());
279 changes = std::move(history->changes);
285 auto mapped = changes.map(position);
288 return std::unexpected(mapped.error());
291 if (!mapped->removed) {
295 if (snapshot->rowCount() == 0) {
301 offset = mapped->offset;
307 offset = std::min(offset, snapshot->rowCount() - 1);
313 auto row = readRow(snapshot, offset);
316 return std::unexpected(row.error());
319 if (stop.stop_requested()) {
333 position.row = **row;
334 position.offset = offset;
335 position.revision = snapshot->revision();
336 reply.state = outcome;
337 reply.position = position;
342 ModelResource resource_;
343 SourceStamp session_;
344 std::weak_ptr<const ModelSnapshot<T>> snapshot_;
345 PositionChangeMap changes_;
static std::expected< FinitePositionResolver, SourceError > create(ModelResource resource, SourceStamp session, std::shared_ptr< const ModelSnapshot< T > > snapshot, PositionChangeMap changes={})
Definition model_position_finite.hpp:62
std::expected< PositionReply, SourceError > resolve(const PositionRequest &request, std::stop_token stop={}) const override
Definition model_position_finite.hpp:108
std::expected< ModelBookmark, SourceError > capture(std::uint64_t index, PositionAffinity affinity=PositionAffinity::FORWARD, BookmarkOptions options={}) const
Definition model_position_finite.hpp:79
static std::expected< ModelBookmark, SourceError > create(ModelResource resource, ModelPosition position, std::span< const std::byte > metadata={}, BookmarkOptions options={})
std::span< const std::byte > metadata() const noexcept
ModelPosition position() const noexcept
ModelResource resource() const noexcept
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
Immutable bounded provider proof between two revisions of a finite order. The provider must report al...
Definition model_position.hpp:224
Bounded provider extension independent of views, schedulers, and total extent. Implementations must n...
Definition model_position.hpp:163
Public Portals API.
Definition action.hpp:8
PositionAffinity
Preferred surviving side when a position is deleted or text is inserted beside a mark.
Definition model_position.hpp:42
@ BACKWARD
Prefer the preceding item; an entry boundary falls back to the following item.
Definition model_position.hpp:44
@ FORWARD
Prefer the following item; an end boundary falls back to the preceding item.
Definition model_position.hpp:43
@ ITEM
Full RowId with a version-qualified ordinal hint.
Definition model_position.hpp:36
@ TEXT
Provider-defined text mark; providers must preserve grapheme boundaries.
Definition model_position.hpp:38
@ GAP
Boundary between rows, including the entry and end gaps.
Definition model_position.hpp:37
@ READY
Exactly the requested immutable rows are available.
Definition model.hpp:47
@ PENDING
Data is not ready; reading must not block or start work.
Definition model.hpp:46
@ UNAVAILABLE
Supported position history or storage is no longer available.
Definition model_position.hpp:130
@ UNSUPPORTED
The provider does not implement the requested kind or identity capability.
Definition model_position.hpp:128
@ INCOMPATIBLE
Resource, lineage or change proof cannot identify this position safely.
Definition model_position.hpp:129
@ NEAREST
The original item was removed; a declared surviving neighbor was selected.
Definition model_position.hpp:125
@ EMPTY
Compatible source is empty and has no current item.
Definition model_position.hpp:126
@ EXACT
The same item or a proven mapped gap/mark is present.
Definition model_position.hpp:124
@ PENDING
Provider has not materialized the required bounded data yet.
Definition model_position.hpp:127
@ 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
@ NOT_FOUND
Provider supports lookup, but this full RowId is absent.
Definition model.hpp:73
SourceError
Source protocol and admission failures, independent of a provider's storage format.
Definition source_budget.hpp:14
@ CLOSED
Source, consumer or node delivery scope has retired.
Definition source_budget.hpp:17
@ CALLBACK_FAILED
Provider construction/preparation threw an unexpected exception.
Definition source_budget.hpp:21
@ STALE_SOURCE
The supplied immutable source namespace does not match.
Definition source_budget.hpp:24
@ INVALID_REPLY
Result shape, identities, revision or boundaries violate the protocol.
Definition source_budget.hpp:31
@ 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
@ WOULD_BLOCK
Root, work, frame or consumer capacity rejected admission.
Definition source_budget.hpp:18
@ INVALID_ARGUMENT
Invalid options, enum, identity or command shape.
Definition source_budget.hpp:15
@ NO_MEMORY
Allocation failed inside a fallible operation.
Definition source_budget.hpp:19
Checked bookmark capacity, independent of logical source length.
Definition model_position.hpp:57
std::uint64_t revision
Positive revision in which the hint was captured.
Definition model_position.hpp:52
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
std::uint64_t sequence
Positive request generation assigned by the existing caller.
Definition model_position.hpp:137
std::uint64_t revision
Exact target data revision.
Definition model_position.hpp:136
ModelBookmark bookmark
Owning source position, independent of the originating view.
Definition model_position.hpp:138
SourceStamp session
Current delivery session, not the bookmark origin.
Definition model_position.hpp:135
Immutable source namespace; replacement sessions must share their old budget root.
Definition window_source.hpp:27
std::uint64_t instance
Nonzero process-unique session identity.
Definition window_source.hpp:28
std::uint64_t epoch
Nonzero namespace fixed at creation.
Definition window_source.hpp:29