Everett
Loading...
Searching...
No Matches
multiverse.h
Go to the documentation of this file.
1
13#pragma once
14
15#include <everett/file.h>
16#include <everett/fingerprint.h>
19#include <everett/mapped_blob.h>
20#include <everett/mapped_cola.h>
25#include <everett/query.h>
26
27#include <cerrno>
28#include <filesystem>
29#include <span>
30#include <stdexcept>
31#include <string>
32#include <string_view>
33#include <system_error>
34#include <utility>
35#include <vector>
36
37namespace everett {
38 // Aggregate state/progression runtimes are intentionally incomplete. These
39 // declarations carry the family identity without disguising reference_world
40 // as an implemented persistent world or timeline.
41 template <class P> struct world;
42 template <class P> struct timeline;
43 template <class P> struct branch_point;
44 template <class P> struct active_engine;
45 template <class Core> struct connection;
46 struct connection_options;
47
48 // An individual policy-bound sort code. Construction validates its packed
49 // representation and unit alignment only. The registry establishes the
50 // family's prefix freedom and selects its record and semantic handlers.
51 template <class P> struct sort {
52 using policy_type = P;
53 using registry_type = typename P::registry_type;
54
55 explicit sort(bit_string code) : code_(std::move(code)) {
57 if (code_.bit_size & (P::bits_per_unit - 1))
58 throw std::invalid_argument("sort code does not match policy units");
59 }
60 bit_view code() const & { return code_.view(); }
61 bit_view code() const && = delete;
62 bool operator==(sort const &) const = default;
63
64 private:
66 };
67
68 // Holds an existing canonical object root. Reads check envelopes by default;
69 // sealing creates immutable files under caller-reserved identities. The
70 // optional SQLite catalog owns persistent roots and reservations separately.
71 // Files/slices retain their mappings independently of this path holder.
72 template <class P = storage_policy<>> struct multiverse {
73 using policy_type = P;
74 using registry_type = typename P::registry_type;
80 template <class Native = native_array, class Compose = replace_native_value>
82 template <class Native = native_array, class Compose = replace_native_value>
85 template <class Native = native_array>
87 template <class Native = native_array>
91 template <class Target = blob>
108 template <class Compose = replace_native_value>
122
123 explicit multiverse(std::filesystem::path root) : root_(checked_root(std::move(root))) {}
124
125 // Establish missing directory names with a barrier on each new directory
126 // and its parent. Existing ancestors must already be durable and trusted.
127 // A failed barrier leaves the names in place for explicit recovery.
128 static multiverse create(std::filesystem::path root) {
129 if (root.empty()) throw std::invalid_argument("multiverse root must name a directory");
130#if defined(__APPLE__) || defined(__linux__)
131 auto path = std::filesystem::weakly_canonical(std::filesystem::absolute(root));
132 auto ancestor = path;
133 std::vector<std::string> missing;
134 while (!std::filesystem::exists(ancestor)) {
135 missing.push_back(ancestor.filename().string());
136 ancestor = ancestor.parent_path();
137 }
138 ancestor = checked_root(ancestor);
140 struct descriptor {
141 int fd;
142 explicit descriptor(int value) : fd(value) {
143 if (fd < 0) throw std::system_error(errno, std::generic_category(), "open multiverse directory");
144 }
145 descriptor(descriptor const &) = delete;
146 ~descriptor() { if (fd >= 0) ::close(fd); }
147 } parent(ops.open_root(ancestor));
148 for (auto name = missing.rbegin(); name != missing.rend(); ++name) {
149 if (ops.make_directory(parent.fd, name->c_str()) && errno != EEXIST)
150 throw std::system_error(errno, std::generic_category(), "create multiverse directory");
151 descriptor child(ops.open_directory(parent.fd, name->c_str()));
152 if (ops.sync_directory(child.fd) || ops.sync_directory(parent.fd))
153 throw std::system_error(errno, std::generic_category(), "sync multiverse directory; creation outcome unknown");
154 if (ops.close(std::exchange(parent.fd, -1)))
155 throw std::system_error(errno, std::generic_category(), "close multiverse directory; creation outcome unknown");
156 parent.fd = std::exchange(child.fd, -1);
157 }
158 if (ops.close(std::exchange(parent.fd, -1)))
159 throw std::system_error(errno, std::generic_category(), "close multiverse directory; creation outcome unknown");
160 return multiverse(path);
161#else
162 throw std::system_error(std::make_error_code(std::errc::operation_not_supported), "create multiverse directory");
163#endif
164 }
165
166 std::filesystem::path const & root() const & noexcept { return root_; }
167 std::filesystem::path const & root() const && = delete;
168
169 // Include everett/connection.h and link everett::sqlite for these operations.
170 session connect(std::string_view name) const;
171 session connect(std::string_view name, connection_options const & options) const;
172 std::size_t recover_transactions() const;
173
174 file open_object(object_id const & id, file_kind kind, file_open_mode mode = file_open_mode::checked) const {
175 auto result = file::open(root_ / object_path(id, kind), mode);
176 if (mode == file_open_mode::checked && result.header().kind != kind)
177 throw std::invalid_argument("unexpected Everett object kind");
178 return result;
179 }
180
182 return everett::open_mapped_query<P>(root_, head);
183 }
185 return everett::open_mapped_cola_query<P>(root_, head);
186 }
187
188 // The caller establishes root durability and reserves both identities.
189 // Sealing acknowledges object persistence operations, not world adoption.
191 file_header<P> const & header, std::span<std::byte const> body) const {
192 return object_writer::seal(root_, id, attempt, header, body);
193 }
195 file_header<P> const & header, std::span<std::span<std::byte const> const> chunks) const {
196 return object_writer::seal(root_, id, attempt, header, chunks);
197 }
198
199 private:
200 static std::filesystem::path checked_root(std::filesystem::path root) {
201 if (root.empty()) throw std::invalid_argument("multiverse root must name an existing directory");
202 auto absolute = std::filesystem::canonical(root);
203 if (!std::filesystem::is_directory(absolute))
204 throw std::invalid_argument("multiverse root is not a directory");
205 return absolute;
206 }
207 std::filesystem::path root_;
208 };
209}
Stages one owning native merge and its exact COLA destination/carrier indexes.
Declares Everett's file support.
Streams complete fractional-index chains over pinned mapped inputs.
Declares Everett's fingerprint support.
Binds immutable mapped blobs to exact dependency chains and validates their samples.
Binds mapped COLA main and secondary targets with exact immutable pins.
Definition active_engine.h:18
connection< Core > connect(std::filesystem::path const &root, std::string_view name, connection_options options={})
Definition connection.h:409
file_open_mode
Definition file.h:32
std::filesystem::path object_path(object_id const &id, file_kind kind)
Definition object_path.h:52
file_kind
Definition object_path.h:25
Merges pinned native inputs into a streamed immutable object.
Merges ordered native streams incrementally with policy-specific value composition.
Appends immutable object bodies before their final envelope is known.
Streams and seals immutable object files without catalog publication.
Prepares immutable catalog chains and enumerates every matching native segment.
Definition active_engine.h:38
Definition profile.h:166
void validate() const
Definition profile.h:170
bit_view view() const &
Definition profile.h:178
std::uint64_t bit_size
Definition profile.h:168
Definition profile.h:56
Definition sections.h:34
Definition multiverse.h:43
Definition cola_local_merge.h:31
Definition cola_index.h:496
Definition cola_local_merge.h:81
Definition cola_local_merge.h:55
Definition cola_query.h:173
Definition cola_query.h:40
Definition connection.h:37
Definition connection.h:294
Definition file.h:34
Definition file_index_builder.h:87
Definition file_index_pipeline.h:49
Definition file_index_pipeline.h:30
Definition file.h:242
static file open(std::filesystem::path const &path, file_open_mode mode=file_open_mode::checked)
Definition file.h:257
Definition index_builder.h:47
Definition mapped_blob.h:31
Definition mapped_cola.h:33
Definition cola_sections.h:266
Definition sections.h:486
Definition sections.h:442
Definition multiverse.h:72
multiverse(std::filesystem::path root)
Definition multiverse.h:123
P policy_type
Definition multiverse.h:73
std::filesystem::path root_
Definition multiverse.h:207
mapped_query_root open_query(blob_identity const &head) const
Definition multiverse.h:181
std::filesystem::path const & root() const &&=delete
static multiverse create(std::filesystem::path root)
Definition multiverse.h:128
std::filesystem::path const & root() const &noexcept
Definition multiverse.h:166
object_seal_receipt seal_object(object_id const &id, object_attempt_id const &attempt, file_header< P > const &header, std::span< std::span< std::byte const > const > chunks) const
Definition multiverse.h:194
typename P::registry_type registry_type
Definition multiverse.h:74
static std::filesystem::path checked_root(std::filesystem::path root)
Definition multiverse.h:200
mapped_cola_query_root open_cola_query(blob_identity const &head) const
Definition multiverse.h:184
object_seal_receipt seal_object(object_id const &id, object_attempt_id const &attempt, file_header< P > const &header, std::span< std::byte const > body) const
Definition multiverse.h:190
Definition native_file_merge.h:38
Definition native_file_writer.h:29
Definition native_merge.h:174
Definition object_writer.h:39
Definition object_path.h:38
Definition object_writer.h:88
Definition object_stream.h:32
Definition object_writer.h:161
static object_seal_receipt seal(std::filesystem::path const &root, object_id const &id, object_attempt_id const &attempt, file_header< P > const &header, std::span< std::span< std::byte const > const > chunks, Ops &ops)
Definition object_writer.h:164
Definition object_writer.h:100
Definition profile.h:1168
Definition profile_blob.h:187
Definition profile_index.h:39
Definition native_writer.h:158
Definition profile.h:328
Definition query.h:161
Definition query.h:103
Definition query.h:51
Definition multiverse.h:51
bit_view code() const &
Definition multiverse.h:60
bit_view code() const &&=delete
bit_string code_
Definition multiverse.h:65
sort(bit_string code)
Definition multiverse.h:55
typename P::registry_type registry_type
Definition multiverse.h:53
P policy_type
Definition multiverse.h:52
Definition policy.h:55
Definition multiverse.h:42
Definition multiverse.h:41