Everett
Loading...
Searching...
No Matches
sort_runtime.h
Go to the documentation of this file.
1
12#pragma once
13
18#include <everett/typed_world.h>
19
20namespace everett {
21 // Logical query keys carry their length separately: sort bits followed by
22 // leaf order bits. They never acquire the opaque string transport's escapes.
23 template <class P, class Selector = registry_selector<typename P::registry_type>> struct sort_key_transport {
24 template <class S> static bit_string encode(typename sort_codec<S>::key_codec::value_type const & key) {
25 return sort_profile_query<P, S, Selector>(key);
26 }
27 template <class S> static bit_string prefix() {
28 bit_string result; sort_bit_writer out(result); Selector::template write<S>(out); return result;
29 }
30 template <class S> static auto decode(bit_view bits) {
32 }
33 template <class F> static decltype(auto) dispatch(bit_view bits, F && fn) {
34 sort_bit_reader input(bits);
35 return Selector::select(input, [&]<class S>(std::type_identity<S> tag, auto & source) -> decltype(auto) {
36 auto key = decode<S>(source.take_bits(source.remaining()));
37 return std::invoke(std::forward<F>(fn), tag, key);
38 });
39 }
40 };
41
42 template <class P, class Selector = registry_selector<typename P::registry_type>> struct sort_runtime_native {
43 using policy_type = P;
47 using pointer = std::shared_ptr<sort_runtime_native const>;
49 return pointer(new sort_runtime_native(std::make_shared<array_type const>(std::move(value))));
50 }
51 static pointer from_owned(std::shared_ptr<array_type const> value) {
52 if (!value) throw std::invalid_argument("null owned sort runtime native");
53 return pointer(new sort_runtime_native(std::move(value)));
54 }
55 static pointer from_mapped(std::shared_ptr<mapped_type const> value) {
56 if (!value) throw std::invalid_argument("null mapped sort runtime native");
57 return pointer(new sort_runtime_native(std::move(value)));
58 }
59 auto view() const { return owned_ ? owned_->view() : mapped_->view(); }
60 std::uint64_t size() const { return view().size(); }
61 std::shared_ptr<array_type const> owned() const noexcept { return owned_; }
62 std::shared_ptr<mapped_type const> mapped() const noexcept { return mapped_; }
63 std::shared_ptr<native_seal const> sealed() const noexcept { return seal_; }
64 private:
65 template <class, class, class, class> friend struct runtime_store;
66 template <class, class, class, class> friend struct runtime_store_detail::graph_sealer;
67 template <class, class, class, class, class> friend struct sort_runtime_context;
68 // Only an acknowledged catalog seal can construct this descriptor. The
69 // context opens the expected object path itself, never a supplied mapping.
70 static pointer from_sealed(std::filesystem::path const & root, object_id catalog, object_seal_receipt receipt) {
71 auto path = root / object_path(receipt.object, file_kind::native_blob);
72 if (std::filesystem::canonical(receipt.path) != std::filesystem::canonical(path))
73 throw std::invalid_argument("sealed native path differs from object identity");
74 auto mapped = std::make_shared<mapped_type const>(mapped_type::open(path));
75 auto result = std::shared_ptr<sort_runtime_native>(new sort_runtime_native(mapped));
76 auto binding = std::make_shared<native_binding<mapped_type> const>(
77 native_seal{std::move(catalog), std::move(receipt)}, std::move(mapped));
78 result->seal_ = binding;
79 result->bindings_.get_or_create(binding->catalog, root, [&] { return binding; });
80 return result;
81 }
84 std::shared_ptr<native_seal const> seal_;
85 std::shared_ptr<array_type const> owned_;
86 std::shared_ptr<mapped_type const> mapped_;
87 explicit sort_runtime_native(std::shared_ptr<array_type const> value) : owned_(std::move(value)) {}
88 explicit sort_runtime_native(std::shared_ptr<mapped_type const> value) : mapped_(std::move(value)) {}
89 };
90
91 namespace sort_runtime_detail {
92 // Input owns the full order keys for this synchronous call. Borrow them
93 // directly, retaining the previous view only long enough to compute LCP.
94 template <class P, class Selector, class Writer>
95 void write_sorted_native(Writer & writer, std::span<profile_record const> records) {
97 bit_view previous;
98 bool first = true;
99 for (auto const & record : records) {
100 auto key = record.key.view();
101 auto comparison = compare_common_bits<typename P::architecture>(previous, key);
102 if (!first && comparison.order >= 0)
103 throw std::invalid_argument("sort native requires unique sorted keys");
104 sort_bit_reader input(key);
105 Selector::select(input, [&]<class S>(std::type_identity<S>, auto & source) {
106 sort_codec_detail::validate_value_width<S>();
107 sort_profile_frame frame;
108 frame.path = key.prefix(source.position());
110 frame.key_units = key.size();
111 frame.retained_limit_bits = record.retained_limit_bits;
113 std::array<bit_view, 1> spans{key};
114 writer.append_frame(frame, spans, comparison.common_bits, record.value.view());
115 });
116 previous = key; first = false;
117 }
118 }
119 }
120
121 template <class P, class Selector = registry_selector<typename P::registry_type>> struct sort_runtime_storage {
122 static_assert(P::unit == profile_unit::bit, "sort runtime uses bit-addressed records");
128 template <class Compose> static auto make_merge(std::shared_ptr<native_type const> older,
129 std::shared_ptr<native_type const> newer, Compose compose) {
130 return std::make_unique<merge_type<Compose>>(std::move(older), std::move(newer), std::move(compose));
131 }
132 template <class Merge> static auto finish_merge(Merge & merge) { return native_type::from_owned(merge.finish()); }
134 template <class Node> static auto make_index(std::shared_ptr<native_type const> native,
135 typename Node::pair_type main = {}, std::shared_ptr<native_type const> secondary = {}) {
136 return std::make_unique<index_type<Node>>(std::move(native), std::move(main), std::move(secondary));
137 }
138 template <class Node> static auto finish_index(index_type<Node> & index) { return Node::from_built(index.finish()); }
139 static auto empty() { sort_profile_writer<P, Selector> writer; return native_type::from_owned(writer.finish()); }
140 static auto sorted_native(std::span<profile_record const> records) {
142 sort_runtime_detail::write_sorted_native<P, Selector>(writer, records);
143 return native_type::from_owned(writer.finish());
144 }
145 static auto singleton(profile_record const & record) { return sorted_native({&record, 1}); }
146 };
147
148 // Policy/schema dispatch can select this concrete family before entering a
149 // run. Selector is a protocol implementation, not necessarily a binary tree.
150 template <class P = string_policy, class Selector = registry_selector<typename P::registry_type>,
151 class Storage = sort_runtime_storage<P, Selector>>
155 static auto open_storage(std::filesystem::path const & root) requires requires { Storage::open(root); } {
156 return Storage::open(root);
157 }
158 static auto open_storage(std::filesystem::path const & root, std::string_view schema)
159 requires requires { Storage::open(root); } {
160 if constexpr (requires { { Storage::open_for_schema(root, schema) } -> std::same_as<Storage>; })
161 return Storage::open_for_schema(root, schema);
162 else return Storage::open(root);
163 }
164 template <class Options> static auto open_storage(std::filesystem::path const & root,
165 std::string_view schema, Options options)
166 requires requires { Storage::open_for_schema(root, schema, options); } {
167 return Storage::open_for_schema(root, schema, std::move(options));
168 }
169 static std::string default_schema() {
170 if constexpr (std::same_as<typename P::registry_type, string_registry> &&
171 std::same_as<Selector, registry_selector<typename P::registry_type>>)
172 return "everett.optional-string/code0/sort-profile-v1";
173 else return {};
174 }
175 };
176}
void write_sorted_native(Writer &writer, std::span< profile_record const > records)
Definition sort_runtime.h:95
Definition active_engine.h:18
std::filesystem::path object_path(object_id const &id, file_kind kind)
Definition object_path.h:52
Executes redundant COLA slots with charged jobs and immutable frontiers.
Describes completed native and index objects retained by runtime owners.
Maps sort-owned KV03 records and binds their ordinary fractional indexes.
Merges sort-owned records while retaining inherited keys as borrowed spans.
Definition profile.h:166
bit_view view() const &
Definition profile.h:178
Definition profile.h:56
Definition catalog_bindings.h:25
Definition cola_index.h:496
auto finish()
Definition cola_index.h:581
Definition sort_profile_file.h:29
Definition sort_profile_file.h:185
Definition sort_profile_file.h:116
static mapped_sort_profile open(file< P > source)
Definition sort_profile_file.h:120
Definition runtime_seal.h:19
Definition object_path.h:38
Definition object_writer.h:88
std::filesystem::path path
Definition object_writer.h:91
object_id object
Definition object_writer.h:89
Definition profile.h:294
Definition redundant_runtime.h:918
Definition registry.h:122
Definition runtime_graph_sealer.h:22
Definition runtime_store.h:58
Definition sort_codec.h:56
Definition sort_codec.h:38
Definition sort_codec.h:401
Definition sort_runtime.h:23
static bit_string prefix()
Definition sort_runtime.h:27
static auto decode(bit_view bits)
Definition sort_runtime.h:30
static decltype(auto) dispatch(bit_view bits, F &&fn)
Definition sort_runtime.h:33
static bit_string encode(typename sort_codec< S >::key_codec::value_type const &key)
Definition sort_runtime.h:24
Definition sort_profile.h:405
Definition sort_profile.h:143
Definition sort_profile.h:397
Definition sort_profile.h:163
std::uint64_t key_units
Definition sort_profile.h:164
bool front_coded
Definition sort_profile.h:169
std::size_t leaf
Definition sort_profile.h:168
bit_view path
Definition sort_profile.h:167
std::optional< std::uint64_t > retained_limit_bits
Definition sort_profile.h:171
bit_view value
Definition sort_profile.h:166
Definition sort_profile.h:32
Definition sort_profile_merge.h:66
Definition sort_profile.h:531
array_type finish()
Definition sort_profile.h:569
Definition sort_runtime_context.h:33
Definition sort_runtime.h:152
static std::string default_schema()
Definition sort_runtime.h:169
static auto open_storage(std::filesystem::path const &root)
Definition sort_runtime.h:155
static auto open_storage(std::filesystem::path const &root, std::string_view schema, Options options)
Definition sort_runtime.h:164
static auto open_storage(std::filesystem::path const &root, std::string_view schema)
Definition sort_runtime.h:158
Definition sort_runtime.h:42
P policy_type
Definition sort_runtime.h:43
catalog_bindings< sort_runtime_native > mapped_owners_
Definition sort_runtime.h:83
static pointer from_owned(std::shared_ptr< array_type const > value)
Definition sort_runtime.h:51
sort_runtime_native(std::shared_ptr< array_type const > value)
Definition sort_runtime.h:87
std::shared_ptr< array_type const > owned_
Definition sort_runtime.h:85
std::uint64_t size() const
Definition sort_runtime.h:60
std::shared_ptr< mapped_type const > mapped() const noexcept
Definition sort_runtime.h:62
sort_runtime_native(std::shared_ptr< mapped_type const > value)
Definition sort_runtime.h:88
std::shared_ptr< array_type const > owned() const noexcept
Definition sort_runtime.h:61
std::shared_ptr< native_seal const > sealed() const noexcept
Definition sort_runtime.h:63
static pointer from_mapped(std::shared_ptr< mapped_type const > value)
Definition sort_runtime.h:55
std::shared_ptr< sort_runtime_native const > pointer
Definition sort_runtime.h:47
static pointer from_sealed(std::filesystem::path const &root, object_id catalog, object_seal_receipt receipt)
Definition sort_runtime.h:70
std::shared_ptr< native_seal const > seal_
Definition sort_runtime.h:84
auto view() const
Definition sort_runtime.h:59
static pointer from_owned(array_type value)
Definition sort_runtime.h:48
catalog_bindings< native_binding< mapped_type > > bindings_
Definition sort_runtime.h:82
std::shared_ptr< mapped_type const > mapped_
Definition sort_runtime.h:86
Definition sort_runtime.h:121
static auto sorted_native(std::span< profile_record const > records)
Definition sort_runtime.h:140
static auto make_index(std::shared_ptr< native_type const > native, typename Node::pair_type main={}, std::shared_ptr< native_type const > secondary={})
Definition sort_runtime.h:134
static auto empty()
Definition sort_runtime.h:139
static auto singleton(profile_record const &record)
Definition sort_runtime.h:145
static auto encode_native(sort_profile_array< P, Selector > const &value)
Definition sort_runtime.h:126
static auto finish_merge(Merge &merge)
Definition sort_runtime.h:132
static auto make_merge(std::shared_ptr< native_type const > older, std::shared_ptr< native_type const > newer, Compose compose)
Definition sort_runtime.h:128
static auto finish_index(index_type< Node > &index)
Definition sort_runtime.h:138
Connects sort-owned semantics to encoded COLA updates and immutable typed snapshots.