5#include <portals/model.hpp>
18 requires std::copy_constructible<T> && std::equality_comparable<T>
30 if (!limits.maxRows) {
34 if (
rows.size() > limits.maxRows) {
39 auto data = std::make_shared<Data>();
41 data->rows.reserve(
rows.size());
42 data->index.reserve(
rows.size());
44 for (
const auto &row :
rows) {
45 if (!row.id.value || !row.id.generation) {
49 data->index.emplace_back(row.id, data->rows.size());
50 data->rows.emplace_back(row);
53 std::ranges::sort(data->index, [](
const auto &left,
const auto &right) {
54 return less(left.first, right.first);
57 for (std::size_t i = 1; i < data->index.size(); ++i) {
58 if (data->index[i - 1].first == data->index[i].first) {
63 return RowCollection(std::move(data));
64 }
catch (
const std::bad_alloc &) {
66 }
catch (
const std::length_error &) {
74 std::span<const ModelRow<T>>
rows() const noexcept {
82 const auto found = std::ranges::lower_bound(data_->index,
id, less, [](
const auto &entry) {
94 if (data_ == other.data_) {
98 return std::ranges::equal(data_->rows, other.data_->rows, [](
const auto &left,
const auto &right) {
99 return left.id == right.id && left.value == right.value;
105 std::vector<ModelRow<T>>
rows = std::vector<ModelRow<T>>(std::initializer_list<ModelRow<T>>{});
106 std::vector<std::pair<RowId, std::uint64_t>> index = std::vector<std::pair<RowId, std::uint64_t>>(0);
109 static bool less(RowId left, RowId right)
noexcept {
110 return left.value < right.value || (left.value == right.value && left.generation < right.generation);
113 explicit RowCollection(std::shared_ptr<const Data> data) : data_(std::move(data)) {
116 std::shared_ptr<const Data> data_;
std::span< const ModelRow< T > > rows() const noexcept
Definition collection.hpp:74
bool operator==(const RowCollection &other) const
Definition collection.hpp:93
~RowCollection()=default
Release shared row ownership; transferring this value retains a valid shared-owner source.
static std::expected< RowCollection, TreeError > from(std::span< const ModelRow< T > > rows, CollectionLimits limits={})
Copy a checked whole collection into private immutable storage.
Definition collection.hpp:29
RowLookup locate(RowId id) const noexcept
Definition collection.hpp:81
Public Portals API.
Definition action.hpp:8
@ FOUND
The index identifies this RowId in the same snapshot.
Definition model.hpp:74
@ NOT_FOUND
Provider supports lookup, but this full RowId is absent.
Definition model.hpp:73
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ NODE_LIMIT
The configured live node, observer or consumer limit was reached.
Definition widget_types.hpp:35
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
Bound the row/index storage of an immutable in-memory collection; domain T payload is source-owned.
Definition collection.hpp:9
std::size_t maxRows
Positive whole-collection row cap; no silent truncation.
Definition collection.hpp:10
Policies for a scene-owned in-memory model adapter.
Definition collection.hpp:120
ModelOptions model
Existing range/consumer/work budgets.
Definition collection.hpp:121
std::size_t maxRows
Positive full collection/index cap.
Definition collection.hpp:122
Finite per-model storage and work budgets; all fields must be positive.
Definition model.hpp:165
A typed domain row and its stable identity; a table uses a typed record as T.
Definition model.hpp:55
Provider-assigned row identity, independent of position; increment generation on reuse.
Definition row_id.hpp:9
Owning lookup result; no row storage is borrowed.
Definition model.hpp:78