44 std::optional<session_limits>
limits{};
56 std::shared_ptr<connection_detail::logical_state const> logical = std::make_shared<connection_detail::logical_state>())
62 std::shared_ptr<catalog_session_head const>
head_;
63 std::shared_ptr<connection_detail::logical_state const>
logical_;
66 namespace connection_detail {
67 template <
class Core>
auto restore_checkpoint(
typename Core::runtime_family::snapshot_type runtime,
68 std::span<std::byte const> semantic, std::string_view schema) {
69 if constexpr (
requires { Core::restore_checkpoint(std::move(runtime), semantic, schema); })
70 return Core::restore_checkpoint(std::move(runtime), semantic, schema);
72 auto metadata = Core::metadata_type::decode(semantic);
73 return Core::world_type::restore(std::move(runtime), std::move(metadata), schema);
80 template <
class Core = active_engine<>,
class Ids = random_
object_
ids>
struct persistent_engine {
88 std::shared_ptr<private_construction<policy_type>>
scope;
90 std::unique_ptr<Core>
core;
93 :
scope(std::move(lease)),
base(std::move(original)),
core(std::move(candidate)) {}
99 using value_type = std::variant<ordinary_contribution, prepared_transaction, borrowed>;
108 template <
class C>
requires std::constructible_from<ordinary_contribution, C &&>
110 if constexpr (std::is_rvalue_reference_v<C &&> && !std::is_const_v<std::remove_reference_t<C>>)
116 if (
auto p = std::get_if<borrowed>(&other.value)) {
117 if (p->move)
return value_type(std::in_place_index<0>, std::move(*p->move));
118 if constexpr (std::copy_constructible<ordinary_contribution>)
119 return value_type(std::in_place_index<0>, *p->copy);
120 else throw std::logic_error(
"move-only command requires an rvalue");
122 return std::move(other.value);
133 Core seed = options.schema_id.empty() ? Core{} : Core(options.schema_id);
134 auto initial = seed.snapshot();
135 auto schema = initial.metadata().schema_id;
136 auto store =
open_store(root, options.create_if_missing, std::move(ids));
137 auto found = store.find(name);
139 if (!options.create_if_missing)
throw std::out_of_range(
"unknown named Everett session");
140 found.emplace(store.create_session(name, initial.runtime(), initial.metadata().encode()));
142 auto current =
restore(std::move(*found), schema);
144 if constexpr (
requires { family_type::open_storage(store.root(), schema); })
145 return Core::from_snapshot(current, family_type::open_storage(store.root(), schema));
146 else if constexpr (
requires { family_type::open_storage(store.root()); })
147 return Core::from_snapshot(current, family_type::open_storage(store.root()));
148 else return Core::from_snapshot(current);
150 return persistent_engine(std::move(store), std::move(core), std::move(current), std::move(schema));
155 std::is_nothrow_move_constructible_v<Core> && std::is_nothrow_move_constructible_v<
world_type>) = default;
164 if (
auto ordinary = std::get_if<ordinary_contribution>(&input.
value))
return Core::reservation(*ordinary);
165 if (
auto borrowed = std::get_if<typename contribution_type::borrowed>(&input.
value))
166 return Core::reservation(*
borrowed->copy);
186 if (
auto prepared = std::get_if<prepared_transaction>(&input.
value))
187 return install(std::move(*prepared));
188 if (!std::holds_alternative<ordinary_contribution>(input.
value))
189 throw std::logic_error(
"borrowed contribution escaped admission");
196 std::optional<typed_world_type> before;
197 if constexpr (
requires { input.records().empty(); })
198 if (input.records().empty()) before.emplace(
core_->snapshot());
203 try {
return core_->contribute(std::move(input)); }
207 if (before && updated.runtime().same_layout(before->runtime()) && updated.metadata() == before->metadata())
209 return publish(std::move(updated),
true);
212 std::optional<world_type>
advance(std::uint64_t budget) {
215 auto updated =
core_->advance(budget);
216 if (!updated)
return std::nullopt;
217 return publish(std::move(*updated),
false);
231 :
store_(std::move(store)),
core_(std::make_unique<Core>(std::move(core))),
current_(std::move(current)),
schema_(std::move(schema)) {}
233 if (!std::filesystem::is_directory(root))
throw std::invalid_argument(
"Everett backing directory must already exist");
234 if (std::filesystem::exists(root /
"catalog.sqlite3") || !create)
239 std::shared_ptr<connection_detail::logical_state const> logical = std::make_shared<connection_detail::logical_state>()) {
240 auto world = connection_detail::restore_checkpoint<Core>(std::move(saved.
snapshot), saved.
semantic, schema);
241 return {std::move(
world), std::move(saved.
head), std::move(logical)};
249 if (!
core_->pending()) {
250 if constexpr (
requires {
core_->rebase(mapped); })
core_->rebase(mapped);
251 else core_ = std::make_unique<Core>(Core::from_snapshot(mapped));
261 auto updated = input.
core->snapshot();
264 if (!input.
core->pending()) {
265 if constexpr (
requires { input.
core->rebase(mapped); }) input.
core->rebase(mapped);
266 else input.
core = std::make_unique<Core>(Core::from_snapshot(mapped));
284 void require_active()
const {
if (
failed())
throw std::logic_error(
"failed persistent Everett engine; reconnect it"); }
287 if constexpr (
requires { {
core_->poison() }
noexcept; })
core_->poison();
319 std::filesystem::path
const &
root() const & noexcept {
return root_; }
320 std::filesystem::path
const &
root() const && = delete;
322 std::optional<typed_detail::key_t<S>> lo = {}, std::optional<typed_detail::key_t<S>> hi = {})
const {
323 return everett::range<S>(
snapshot(), std::move(lo), std::move(hi));
327 requires typed_detail::replacement<S> {
328 return apply(everett::erase_range<S>(
snapshot(), std::move(lo), std::move(hi)));
332 requires typed_detail::replacement<S> {
333 return submit(everett::erase_range<S>(
snapshot(), std::move(lo), std::move(hi)));
336 return session_->snapshot()->world.template get<S>(key);
338 template <
class S = typed_detail::default_sort_t<policy_type>>
world_type put(
340 requires typed_detail::replacement<S> {
return apply(Core::template put<S>(key, value)); }
342 requires typed_detail::replacement<S> {
return apply(Core::template erase<S>(key)); }
345 return apply(Core::template change<S>(key, arrow));
349 requires typed_detail::replacement<S> {
return submit(Core::template put<S>(key, value)); }
351 requires typed_detail::replacement<S> {
return submit(Core::template erase<S>(key)); }
354 return submit(Core::template change<S>(key, arrow));
357 template <
class C>
requires std::same_as<std::remove_cvref_t<C>,
contribution_type>
358 std::optional<ticket>
try_submit(C && value) {
return session_->try_submit(engine_type::contribution_type::borrow(std::forward<C>(value))); }
359 template <
class C>
requires std::same_as<std::remove_cvref_t<C>,
contribution_type>
360 ticket submit(C && value) {
return session_->submit(engine_type::contribution_type::borrow(std::forward<C>(value))); }
361 template <
class C>
requires std::same_as<std::remove_cvref_t<C>,
contribution_type>
373 storage.save(name, state.head());
375 std::optional<world_type>
load(std::string_view name)
const {
377 auto saved = storage.find_save(name);
378 if (!saved)
return std::nullopt;
379 auto world = connection_detail::restore_checkpoint<Core>(std::move(saved->snapshot), saved->semantic,
380 session_->snapshot()->world.metadata().schema_id);
386 (void)storage.fork(name, state.head());
389 options.schema_id = state.metadata().schema_id;
395 std::uint64_t work = 128'000'000;
396 if constexpr (
requires { Core::reservation_work(std::uint64_t{}); }) work = Core::reservation_work(1024);
399 if (!options.
limits->contributions || !options.
limits->maintenance_budget)
400 throw std::invalid_argument(
"connection needs a positive contribution limit and maintenance budget");
410 std::filesystem::path
const & root, std::string_view name,
connection_options options = {}) {
411 return connection<Core>(root, name, std::move(options));
422 return session(root(), name, options);
Selects the charged runtime and streamed output used by ordinary named sessions.
Declares Everett's multiverse support.
void name(std::string_view value)
Definition sqlite_catalog.h:198
constexpr char schema[]
Definition sqlite_catalog.h:282
auto restore_checkpoint(typename Core::runtime_family::snapshot_type runtime, std::span< std::byte const > semantic, std::string_view schema)
Definition connection.h:67
typename sort_codec< S >::value_codec::value_type arrow_t
Definition typed_world.h:53
typename sort_codec< S >::key_codec::value_type key_t
Definition typed_world.h:52
typename sort_semantics< S >::state_type state_t
Definition typed_world.h:54
Definition active_engine.h:18
connection< Core > connect(std::filesystem::path const &root, std::string_view name, connection_options options={})
Definition connection.h:409
Keeps private transaction construction pinned until its last owner or crash recovery.
Encodes the full redundant frontier, including hidden completed artifacts and restart recipes.
Seals runtime frontiers and restores named sessions through immutable mappings.
Persists sort-owned native files behind the redundant runtime's exact checkpoints.
Definition sqlite_catalog.h:92
std::string operation
Definition sqlite_catalog.h:97
Definition sqlite_catalog.h:76
Definition connection.h:47
Definition connection.h:37
bool create_if_missing
Definition connection.h:41
std::optional< session_limits > limits
Definition connection.h:44
std::string schema_id
Definition connection.h:40
Definition connection.h:294
typename Core::policy_type policy_type
Definition connection.h:296
transaction_type begin()
Definition transaction.h:234
connection(connection &&)=delete
connection(std::filesystem::path const &root, std::string_view name, connection_options options={})
Definition connection.h:306
world_type apply(C &&value)
Definition connection.h:362
session_reservation outstanding() const
Definition connection.h:364
std::optional< world_type > load(std::string_view name) const
Definition connection.h:375
bool cancel(ticket const &value)
Definition connection.h:363
static connection_options checked_options(connection_options options)
Definition connection.h:393
std::uint64_t pending_count() const
Definition connection.h:365
connection fork(std::string_view name, world_type const &state) const
Definition connection.h:384
void close()
Definition connection.h:367
persistent_engine< Core > engine_type
Definition connection.h:297
void save(std::string_view name) const
Definition connection.h:370
std::filesystem::path const & root() const &noexcept
Definition connection.h:319
std::filesystem::path const & root() const &&=delete
connection & operator=(connection const &)=delete
ticket erase_range_async(std::optional< typed_detail::key_t< S > > lo={}, std::optional< typed_detail::key_t< S > > hi={})
Definition connection.h:330
friend transaction_type
Definition connection.h:406
transaction< Core > transaction_type
Definition connection.h:301
connection fork(std::string_view name) const
Definition connection.h:383
void save(std::string_view name, world_type const &state) const
Definition connection.h:371
world_type erase(typed_detail::key_t< S > const &key)
Definition connection.h:341
ticket erase_async(typed_detail::key_t< S > const &key)
Definition connection.h:350
world_type put(typed_detail::key_t< S > const &key, typed_detail::state_t< S > const &value)
Definition connection.h:338
typename Core::contribution_type contribution_type
Definition connection.h:300
connection(connection const &)=delete
world_type erase_range(std::optional< typed_detail::key_t< S > > lo={}, std::optional< typed_detail::key_t< S > > hi={})
Definition connection.h:325
void shutdown()
Definition connection.h:368
std::filesystem::path root_
Definition connection.h:403
publication_type publication() const noexcept
Definition connection.h:318
ticket change_async(typed_detail::key_t< S > const &key, typed_detail::arrow_t< S > const &arrow)
Definition connection.h:352
connection_options options_
Definition connection.h:404
session_limits limits() const noexcept
Definition connection.h:317
auto range(std::optional< typed_detail::key_t< S > > lo={}, std::optional< typed_detail::key_t< S > > hi={}) const
Definition connection.h:321
typename session_type::ticket ticket
Definition connection.h:302
std::optional< ticket > try_submit(C &&value)
Definition connection.h:358
session< engine_type > session_type
Definition connection.h:298
typename engine_type::world_type world_type
Definition connection.h:299
std::exception_ptr failure() const
Definition connection.h:366
Core core_type
Definition connection.h:295
~connection()
Definition connection.h:314
ticket submit(C &&value)
Definition connection.h:360
typename session_type::snapshot_type publication_type
Definition connection.h:303
world_type snapshot() const
Definition connection.h:316
connection & operator=(connection &&)=delete
world_type change(typed_detail::key_t< S > const &key, typed_detail::arrow_t< S > const &arrow)
Definition connection.h:343
auto get(typed_detail::key_t< S > const &key) const
Definition connection.h:335
ticket put_async(typed_detail::key_t< S > const &key, typed_detail::state_t< S > const &value)
Definition connection.h:347
std::shared_ptr< session_type > session_
Definition connection.h:405
session connect(std::string_view name) const
Definition connection.h:418
std::size_t recover_transactions() const
Definition connection.h:414
Definition connection.h:98
ordinary_contribution const * copy
Definition connection.h:98
ordinary_contribution * move
Definition connection.h:98
Definition connection.h:97
contribution_type & operator=(contribution_type const &)=delete
contribution_type & operator=(contribution_type &&)=delete
contribution_type(contribution_type const &)=delete
static auto take(contribution_type &other) -> value_type
Definition connection.h:115
std::variant< ordinary_contribution, prepared_transaction, borrowed > value_type
Definition connection.h:99
contribution_type(prepared_transaction input)
Definition connection.h:102
contribution_type(borrowed input)
Definition connection.h:103
value_type value
Definition connection.h:100
contribution_type(ordinary_contribution input)
Definition connection.h:101
static contribution_type borrow(C &&input)
Definition connection.h:109
contribution_type(contribution_type &&other)
Definition connection.h:106
Definition connection.h:87
std::shared_ptr< private_construction< policy_type > > scope
Definition connection.h:88
std::unique_ptr< Core > core
Definition connection.h:90
prepared_transaction(std::unique_ptr< Core > candidate, world_type original, std::shared_ptr< private_construction< policy_type > > lease)
Definition connection.h:91
world_type base
Definition connection.h:89
Definition connection.h:80
std::unique_ptr< Core > core_
Definition connection.h:224
void remember_failure() noexcept
Definition connection.h:277
static store_type open_store(std::filesystem::path const &root, bool create, Ids ids)
Definition connection.h:232
void poison() noexcept
Definition connection.h:285
void require_active() const
Definition connection.h:284
world_type contribute(contribution_type input)
Definition connection.h:184
typename Core::contribution_type ordinary_contribution
Definition connection.h:86
persistent_engine(store_type store, Core core, world_type current, std::string schema)
Definition connection.h:230
bool failed() const noexcept
Definition connection.h:172
world_type contribute_ordinary(ordinary_contribution input)
Definition connection.h:192
std::string const & last_operation() const noexcept
Definition connection.h:175
world_type current_
Definition connection.h:225
static persistent_engine connect(std::filesystem::path const &root, std::string_view name, connection_options const &options={}, Ids ids={})
Definition connection.h:130
bool admission_ready() const noexcept
Definition connection.h:174
world_type publish(typed_world_type updated, bool mutation)
Definition connection.h:243
persistent_engine(persistent_engine const &)=delete
world_type install(prepared_transaction input)
Definition connection.h:257
static contribution_type borrow_contribution(C &&input)
Definition connection.h:160
persistent_engine & operator=(persistent_engine const &)=delete
typename Core::metadata_type metadata_type
Definition connection.h:125
Core core_type
Definition connection.h:81
typename Core::runtime_family family_type
Definition connection.h:82
std::shared_ptr< private_construction< policy_type > > core_scope_
Definition connection.h:223
std::exception_ptr catalog_failure_
Definition connection.h:227
store_type store_
Definition connection.h:222
world_type snapshot() const
Definition connection.h:171
bool pending() const noexcept
Definition connection.h:173
static session_reservation reservation(contribution_type const &input)
Definition connection.h:163
typename Core::policy_type policy_type
Definition connection.h:83
stored_world< typed_world_type > world_type
Definition connection.h:85
static world_type restore(typename store_type::stored_type saved, std::string_view schema, std::shared_ptr< connection_detail::logical_state const > logical=std::make_shared< connection_detail::logical_state >())
Definition connection.h:238
std::optional< world_type > advance(std::uint64_t budget)
Definition connection.h:212
typename Core::world_type typed_world_type
Definition connection.h:84
bool failed_
Definition connection.h:228
std::string schema_
Definition connection.h:226
persistent_engine(persistent_engine &&) noexcept(std::is_nothrow_move_constructible_v< store_type > &&std::is_nothrow_move_constructible_v< Core > &&std::is_nothrow_move_constructible_v< world_type >)=default
Definition private_construction.h:27
static std::size_t recover(std::filesystem::path const &root)
Definition private_construction.h:55
Definition runtime_store.h:58
stored_type publish(catalog_session_head const &expected, snapshot_type const &source, std::span< std::byte const > semantic={})
Definition runtime_store.h:122
static runtime_store open(std::filesystem::path const &root, Ids ids={}, catalog_options options={}, Ops ops={})
Definition runtime_store.h:79
bool failed() const noexcept
Definition runtime_store.h:89
std::string const & last_operation() const noexcept
Definition runtime_store.h:146
static runtime_store create(std::filesystem::path const &root, Ids ids={}, catalog_options options={}, Ops ops={})
Definition runtime_store.h:74
std::shared_ptr< publication const > snapshot_type
Definition session.h:82
Definition runtime_store.h:49
catalog_session_head head
Definition runtime_store.h:50
Family::snapshot_type snapshot
Definition runtime_store.h:51
std::vector< std::byte > semantic
Definition runtime_store.h:52
Definition connection.h:54
std::shared_ptr< catalog_session_head const > head_
Definition connection.h:62
auto logical_identity() const noexcept
Definition connection.h:60
catalog_session_head const & head() const &&=delete
std::shared_ptr< connection_detail::logical_state const > logical_
Definition connection.h:63
catalog_session_head const & head() const &noexcept
Definition connection.h:58
stored_world(World value, catalog_session_head head, std::shared_ptr< connection_detail::logical_state const > logical=std::make_shared< connection_detail::logical_state >())
Definition connection.h:55
Definition connection.h:50
transaction_conflict()
Definition connection.h:51
Definition transaction.h:28
Definition multiverse.h:41
Private typed nurseries, persistent read snapshots and checked durable commit.
Scans one sort in an immutable typed world with chronological resolution.
Connects sort-owned semantics to encoded COLA updates and immutable typed snapshots.