26#include <unordered_set>
38 if (!value) error_detail::raise<std::invalid_argument>(
"null mapped runtime native");
39 return std::shared_ptr<cola_runtime_native const>(
new cola_runtime_native(std::move(value)));
42 std::uint64_t
size()
const {
return view().size(); }
43 std::shared_ptr<profile_array<P>
const>
owned() const noexcept {
return owned_; }
44 std::shared_ptr<mapped_native<P>
const>
mapped() const noexcept {
return mapped_; }
50 std::shared_ptr<profile_array<P>
const>
owned_;
51 std::shared_ptr<mapped_native<P>
const>
mapped_;
62 using pair_type = std::shared_ptr<cola_runtime_node const>;
68 if (!head) error_detail::raise<std::invalid_argument>(
"null mapped runtime root");
69 std::vector<std::shared_ptr<mapped_cola_blob<P>
const>> nodes;
70 std::unordered_set<mapped_cola_blob<P>
const *> seen;
71 for (
auto p = std::move(head); p; p = p->main_target()) {
72 if (!seen.insert(p.get()).second || p->secondary_target())
73 error_detail::raise<std::invalid_argument>(
"runtime restore requires an acyclic main-only graph");
77 for (
auto i = nodes.rbegin(); i != nodes.rend(); ++i)
83 if (!source || !
native ||
native->mapped() != source->native_object() ||
secondary || source->secondary_target() ||
84 bool(
main) !=
bool(source->main_target()) || (
main &&
main->mapped() != source->main_target()))
85 error_detail::raise<std::invalid_argument>(
"runtime mapped parts do not retain exact targets");
97 std::shared_ptr<built_type const>
built() const noexcept {
return built_; }
98 std::shared_ptr<mapped_cola_blob<P>
const>
mapped() const noexcept {
return mapped_; }
107 std::shared_ptr<built_type const>
built_;
108 std::shared_ptr<mapped_cola_blob<P>
const>
mapped_;
114 if (
built_->secondary_target()) error_detail::raise<std::invalid_argument>(
"runtime node has a secondary");
132 std::shared_ptr<cola_runtime_node<P>
const>
node;
136 template <
class P,
class Compose>
struct cola_runtime;
148 auto const & r =
state_->runs;
149 return r.size() < 2 || r[r.size() - 2].mass() != r.back().mass();
151 std::span<run_type const>
runs() const & noexcept {
return state_->runs; }
152 std::span<run_type const>
runs() const && = delete;
164 std::vector<pair_type> sources;
165 for (
auto p = query.head(); p; p = p->main_target())
166 if (p->native_owner()->size()) sources.push_back(p);
167 if (sources.size() != intervals.size())
168 error_detail::raise<std::invalid_argument>(
"runtime interval count differs from query graph");
169 std::vector<run_type>
runs;
170 std::uint64_t next = 0, previous = 0;
171 for (std::size_t i = 0; i != intervals.size(); ++i) {
172 auto span = intervals[i];
173 if (span.first != next || span.last <= span.first || !std::has_single_bit(span.mass()) ||
174 (i && span.mass() >= previous && !(i + 1 == intervals.size() && previous == 1 && span.mass() == 1)))
175 error_detail::raise<std::invalid_argument>(
"invalid runtime admission frontier");
176 auto source = sources[sources.size() - i - 1];
177 if (source->native_owner()->size() > span.mass())
178 error_detail::raise<std::invalid_argument>(
"runtime native exceeds admission mass");
179 runs.push_back({span.first, span.last, std::move(source)});
180 next = span.last; previous = span.mass();
182 return make(std::move(query), std::move(
runs), next);
221 template <
class P,
class Compose = replace_native_value>
struct cola_runtime {
232 return cola_runtime(std::make_unique<execution>(std::move(source), std::move(compose)));
245 auto const & e =
active();
246 if (e.job) error_detail::raise<std::logic_error>(
"runtime admission requires service");
247 auto r = e.published.runs();
248 auto head = e.published.query_root().head();
249 auto borrowed = head->group_count();
259 if (e.job)
return std::nullopt;
267 if (!budget || !e.job)
return e.published;
268 try { e.grant(budget); e.serve(); }
269 catch (...) { e.failed =
true;
throw; }
273 return contribute(std::span<profile_record const>(&record, 1), service_budget);
282 for (std::size_t i = 0; i != records.size(); ++i) {
283 auto key = records[i].key.
view(), value = records[i].value.
view();
284 if ((key.size() & (P::bits_per_unit - 1)) || (value.size() & (P::bits_per_unit - 1)) ||
285 (P::value_width && (value.size() >> P::unit_shift) != *P::value_width) ||
286 (i && compare_bits<typename P::architecture>(previous, key) > 0))
287 error_detail::raise<std::invalid_argument>(
"invalid encoded runtime contribution");
290 auto before = e.published;
292 for (
auto const & record : records) {
295 if (service_budget && e.job) { e.grant(service_budget); e.serve(); }
297 }
catch (...) { e.published = std::move(before); e.failed =
true;
throw; }
301 using merge_compose = std::conditional_t<std::is_same_v<Compose, replace_native_value>,
302 Compose, std::reference_wrapper<Compose>>;
318 carry = newest.native_owner();
first = newest.first;
last = newest.last;
327 std::unique_ptr<continuation>
job;
347 static std::uint64_t
blocks(std::uint64_t n) {
return n / P::codec_block_size + (n % P::codec_block_size != 0); }
354 next.
charged =
add(next.charged, amount); next.granted =
add(next.granted, amount);
355 next.*field =
add(next.*field, amount);
work = next;
361 if constexpr (std::is_same_v<Compose, replace_native_value>)
return compose;
366 auto limit = mass > std::numeric_limits<std::uint64_t>::max() / P::group_size ?
367 std::numeric_limits<std::uint64_t>::max() : mass * P::group_size;
368 while (target && !target->native_owner()->size() && target->main_target() &&
369 target->main_target()->virtual_size() <= limit) target = target->main_target();
373 auto borrowed = target ? target->group_count() : 0;
376 while (!builder.
done()) {
384 auto result = builder.
finish();
410 auto const & j = *
job;
420 error_detail::raise<std::logic_error>(
"invalid runtime stage");
425 switch (
job->stage) {
429 next.setup =
add(next.setup, 1);
break;
431 next.index_occurrences =
add(next.index_occurrences, 1);
432 next.target_scan_allowance =
add(next.target_scan_allowance, P::group_size);
433 next.navigation_allowance =
add(next.navigation_allowance, 5);
break;
435 next.navigation_allowance =
add(next.navigation_allowance, 3);
break;
444 j.merge = std::make_unique<merge_type>(j.older.native_owner(), j.carry,
merger());
448 auto done = j.merge->step(1);
457 j.first = j.older.first;
458 if (!j.prefix.empty() && j.prefix.back().mass() == j.last - j.first) {
459 j.older = j.prefix.back(); j.prefix.pop_back(); j.target = j.older.node->main_target();
462 j.target =
near(std::move(j.target), j.last - j.first);
467 j.index = std::make_unique<index_type>(j.carrier ?
empty : j.carry, j.target);
478 else j.result_node = j.head;
479 if (j.head->virtual_size() > P::group_size) {
484 j.prefix.push_back({j.first, j.last, j.result_node});
485 auto result =
snapshot_type::make(query_type::adopt_prepared(j.head), std::move(j.prefix), j.last);
493 auto amount =
price();
494 if (amount >
credit)
break;
500 auto amount =
price();
509 if (!
execution_) error_detail::raise<std::logic_error>(
"moved-from COLA runtime");
514 if (
execution_->failed) error_detail::raise<std::logic_error>(
"failed COLA runtime");
Enumerates native matches along a COLA main chain and its terminal secondaries.
Binds mapped COLA main and secondary targets with exact immutable pins.
std::uint64_t add(std::uint64_t a, std::uint64_t b)
Definition profile.h:39
std::uint64_t multiply(std::uint64_t a, std::uint64_t b)
Definition profile.h:44
Definition active_engine.h:18
Merges ordered native streams incrementally with policy-specific value composition.
Describes completed native and index objects retained by runtime owners.
Definition cola_runtime.h:519
P policy_type
Definition cola_runtime.h:520
bit_view view() const &
Definition profile.h:178
Definition catalog_bindings.h:25
Definition cola_index.h:496
auto finish()
Definition cola_index.h:581
bool done() const noexcept
Definition cola_index.h:520
std::uint64_t step(std::uint64_t budget)
Definition cola_index.h:529
Definition cola_index.h:114
Definition cola_index.h:315
static cola_query_root adopt_prepared(pair_type source)
Definition cola_query.h:47
pair_type head() const noexcept
Definition cola_query.h:63
cola_query_cursor< P, Blob > cursor(bit_view query) const
Definition cola_query.h:64
Definition cola_runtime.h:306
pair_type head
Definition cola_runtime.h:311
std::vector< run_type > prefix
Definition cola_runtime.h:307
pair_type result_node
Definition cola_runtime.h:311
phase stage
Definition cola_runtime.h:313
std::unique_ptr< index_type > index
Definition cola_runtime.h:315
native_pointer carry
Definition cola_runtime.h:309
bool carrier
Definition cola_runtime.h:312
pair_type target
Definition cola_runtime.h:311
std::uint64_t first
Definition cola_runtime.h:310
std::uint64_t last
Definition cola_runtime.h:310
std::unique_ptr< merge_type > merge
Definition cola_runtime.h:314
continuation(snapshot_type const &source)
Definition cola_runtime.h:316
run_type older
Definition cola_runtime.h:308
Definition cola_runtime.h:323
void charge(std::uint64_t amount)
Definition cola_runtime.h:422
static pair_type near(pair_type target, std::uint64_t mass)
Definition cola_runtime.h:365
void grant(std::uint64_t amount)
Definition cola_runtime.h:348
execution(snapshot_type source, Compose value)
Definition cola_runtime.h:341
void direct_setup(std::uint64_t n)
Definition cola_runtime.h:357
static std::uint64_t blocks(std::uint64_t n)
Definition cola_runtime.h:347
void direct_publication(std::uint64_t n)
Definition cola_runtime.h:359
execution(Compose value)
Definition cola_runtime.h:337
void admit(profile_record const &record)
Definition cola_runtime.h:392
void serve()
Definition cola_runtime.h:491
snapshot_type published
Definition cola_runtime.h:326
std::uint64_t price() const
Definition cola_runtime.h:409
cola_runtime_work work
Definition cola_runtime.h:328
native_pointer empty
Definition cola_runtime.h:325
std::uint64_t credit
Definition cola_runtime.h:329
void perform()
Definition cola_runtime.h:440
void direct(std::uint64_t amount, std::uint64_t cola_runtime_work::*field)
Definition cola_runtime.h:352
std::unique_ptr< continuation > job
Definition cola_runtime.h:327
static native_pointer empty_native()
Definition cola_runtime.h:331
pair_type immediate_index(native_pointer native, pair_type target)
Definition cola_runtime.h:372
void direct_directory(std::uint64_t n)
Definition cola_runtime.h:358
merge_compose merger()
Definition cola_runtime.h:360
void drain()
Definition cola_runtime.h:498
static std::uint64_t add(std::uint64_t a, std::uint64_t b)
Definition cola_runtime.h:346
void start_job()
Definition cola_runtime.h:388
static snapshot_type initial(native_pointer native)
Definition cola_runtime.h:332
Compose compose
Definition cola_runtime.h:324
bool failed
Definition cola_runtime.h:330
Definition cola_runtime.h:125
std::uint64_t mass() const noexcept
Definition cola_runtime.h:127
std::uint64_t last
Definition cola_runtime.h:126
bool operator==(cola_runtime_interval const &) const =default
std::uint64_t first
Definition cola_runtime.h:126
Definition cola_runtime.h:31
std::shared_ptr< mapped_native< P > const > mapped() const noexcept
Definition cola_runtime.h:44
profile_view< P > view() const
Definition cola_runtime.h:41
cola_runtime_native(std::shared_ptr< mapped_native< P > const > value)
Definition cola_runtime.h:53
std::shared_ptr< profile_array< P > const > owned_
Definition cola_runtime.h:50
catalog_bindings< native_binding< mapped_native< P > > > bindings_
Definition cola_runtime.h:48
std::shared_ptr< mapped_native< P > const > mapped_
Definition cola_runtime.h:51
static std::shared_ptr< cola_runtime_native const > from_owned(profile_array< P > value)
Definition cola_runtime.h:33
catalog_bindings< cola_runtime_native > mapped_owners_
Definition cola_runtime.h:49
cola_runtime_native(std::shared_ptr< profile_array< P > const > value)
Definition cola_runtime.h:52
std::uint64_t size() const
Definition cola_runtime.h:42
std::shared_ptr< profile_array< P > const > owned() const noexcept
Definition cola_runtime.h:43
P policy_type
Definition cola_runtime.h:32
static std::shared_ptr< cola_runtime_native const > from_mapped(std::shared_ptr< mapped_native< P > const > value)
Definition cola_runtime.h:37
Definition cola_runtime.h:58
cola_index_view< P > view() const
Definition cola_runtime.h:88
std::uint64_t virtual_size() const
Definition cola_runtime.h:94
static pair_type from_built(built_type value)
Definition cola_runtime.h:64
cola_runtime_node(std::shared_ptr< built_type const > value)
Definition cola_runtime.h:111
bool canonical_mapped() const noexcept
Definition cola_runtime.h:99
catalog_bindings< cola_runtime_node > mapped_owners_
Definition cola_runtime.h:104
catalog_bindings< pair_binding< mapped_cola_blob< P > > > bindings_
Definition cola_runtime.h:103
native_type const & native() const &noexcept
Definition cola_runtime.h:90
bool canonical_mapped_
Definition cola_runtime.h:110
std::shared_ptr< mapped_cola_blob< P > const > mapped_
Definition cola_runtime.h:108
std::shared_ptr< mapped_cola_blob< P > const > mapped() const noexcept
Definition cola_runtime.h:98
static pair_type from_mapped(std::shared_ptr< mapped_cola_blob< P > const > head)
Definition cola_runtime.h:67
pair_type main_
Definition cola_runtime.h:106
cola_runtime_node(std::shared_ptr< mapped_cola_blob< P > const > value, native_pointer native, pair_type main)
Definition cola_runtime.h:119
native_type const & native() const &&=delete
P policy_type
Definition cola_runtime.h:59
native_pointer native_owner() const noexcept
Definition cola_runtime.h:89
std::shared_ptr< built_type const > built() const noexcept
Definition cola_runtime.h:97
std::shared_ptr< built_type const > built_
Definition cola_runtime.h:107
std::uint64_t depth_
Definition cola_runtime.h:109
native_pointer native_
Definition cola_runtime.h:105
pair_type main_target() const noexcept
Definition cola_runtime.h:92
std::uint64_t group_count() const
Definition cola_runtime.h:95
cola_runtime_node(std::shared_ptr< mapped_cola_blob< P > const > value, pair_type main)
Definition cola_runtime.h:116
std::shared_ptr< cola_runtime_node const > pair_type
Definition cola_runtime.h:62
static pair_type from_mapped_parts(std::shared_ptr< mapped_cola_blob< P > const > source, native_pointer native, pair_type main, native_pointer secondary={})
Definition cola_runtime.h:81
native_pointer secondary_target() const noexcept
Definition cola_runtime.h:93
std::shared_ptr< native_type const > native_pointer
Definition cola_runtime.h:61
std::uint64_t depth() const noexcept
Definition cola_runtime.h:96
Definition cola_runtime.h:130
std::uint64_t last
Definition cola_runtime.h:131
std::uint64_t first
Definition cola_runtime.h:131
std::shared_ptr< cola_runtime_node< P > const > node
Definition cola_runtime.h:132
std::uint64_t mass() const noexcept
Definition cola_runtime.h:133
auto native_owner() const noexcept
Definition cola_runtime.h:134
Definition cola_runtime.h:186
std::vector< run_type > runs
Definition cola_runtime.h:188
std::uint64_t admissions
Definition cola_runtime.h:189
query_type query
Definition cola_runtime.h:187
Definition cola_runtime.h:140
static cola_runtime_snapshot restore(pair_type head, std::span< cola_runtime_interval const > intervals)
Definition cola_runtime.h:162
bool settled() const noexcept
Definition cola_runtime.h:147
bool same_layout(cola_runtime_snapshot const &other) const noexcept
Definition cola_runtime.h:157
std::shared_ptr< state const > state_
Definition cola_runtime.h:191
cola_query_root< P, node_type > query_type
Definition cola_runtime.h:144
std::span< run_type const > runs() const &noexcept
Definition cola_runtime.h:151
std::span< run_type const > runs() const &&=delete
auto cursor(bit_view key) const
Definition cola_runtime.h:155
query_type const & query_root() const &&=delete
static cola_runtime_snapshot make(query_type query, std::vector< run_type > runs, std::uint64_t admissions)
Definition cola_runtime.h:193
P policy_type
Definition cola_runtime.h:141
query_type const & query_root() const &noexcept
Definition cola_runtime.h:153
cola_runtime_snapshot(std::shared_ptr< state const > value)
Definition cola_runtime.h:192
std::uint64_t admissions() const noexcept
Definition cola_runtime.h:146
auto cursor_owned(bit_string key) const
Definition cola_runtime.h:156
typename node_type::pair_type pair_type
Definition cola_runtime.h:143
Definition cola_runtime.h:201
std::uint64_t granted
Definition cola_runtime.h:202
std::uint64_t charged
Definition cola_runtime.h:202
std::uint64_t publication
Definition cola_runtime.h:205
std::uint64_t target_scan_allowance
Definition cola_runtime.h:204
std::uint64_t index_occurrences
Definition cola_runtime.h:204
std::uint64_t native_input_records
Definition cola_runtime.h:206
std::uint64_t directory_entries
Definition cola_runtime.h:205
std::uint64_t admitted_records
Definition cola_runtime.h:207
std::uint64_t navigation_allowance
Definition cola_runtime.h:204
std::uint64_t carriers
Definition cola_runtime.h:207
std::uint64_t indexes
Definition cola_runtime.h:207
std::uint64_t native_merges
Definition cola_runtime.h:207
std::uint64_t native_output_records
Definition cola_runtime.h:206
std::uint64_t native_allowance
Definition cola_runtime.h:203
std::uint64_t setup
Definition cola_runtime.h:203
Definition cola_runtime.h:221
cola_runtime(cola_runtime const &)=delete
std::uint64_t admission_cost() const
Definition cola_runtime.h:244
typename node_type::native_pointer native_pointer
Definition cola_runtime.h:226
snapshot_type contribute(std::span< profile_record const > records, std::uint64_t service_budget=64)
Definition cola_runtime.h:278
bool failed() const noexcept
Definition cola_runtime.h:262
execution const & active() const
Definition cola_runtime.h:508
typename node_type::pair_type pair_type
Definition cola_runtime.h:227
cola_runtime & operator=(cola_runtime const &)=delete
snapshot_type contribute(profile_record const &record, std::uint64_t service_budget=64)
Definition cola_runtime.h:272
phase
Definition cola_runtime.h:305
bool admission_ready() const noexcept
Definition cola_runtime.h:240
std::uint64_t next_service_cost() const
Definition cola_runtime.h:241
cola_runtime(Compose compose={})
Definition cola_runtime.h:230
cola_runtime_work work() const
Definition cola_runtime.h:263
std::optional< snapshot_type > try_contribute(profile_record const &record, std::uint64_t service_budget=0)
Definition cola_runtime.h:257
static cola_runtime from_snapshot(snapshot_type source, Compose compose={})
Definition cola_runtime.h:231
typename snapshot_type::query_type query_type
Definition cola_runtime.h:229
execution & writable()
Definition cola_runtime.h:512
bool pending() const noexcept
Definition cola_runtime.h:239
cola_runtime(std::unique_ptr< execution > value)
Definition cola_runtime.h:507
std::unique_ptr< execution > execution_
Definition cola_runtime.h:506
cola_runtime(cola_runtime &&) noexcept=default
P policy_type
Definition cola_runtime.h:222
std::uint64_t credit() const
Definition cola_runtime.h:264
snapshot_type advance(std::uint64_t budget)
Definition cola_runtime.h:265
snapshot_type snapshot() const
Definition cola_runtime.h:238
std::conditional_t< std::is_same_v< Compose, replace_native_value >, Compose, std::reference_wrapper< Compose > > merge_compose
Definition cola_runtime.h:302
Definition mapped_cola.h:33
Definition sections.h:442
Definition native_merge.h:174
Definition profile.h:1168
Definition native_writer.h:158
void append(bit_view key, bit_view value, std::optional< std::uint64_t > retained_limit_bits={})
Definition native_writer.h:181
profile_array< P > finish()
Definition native_writer.h:208
Definition runtime_graph_sealer.h:22
Definition runtime_store.h:58