18 namespace transaction_detail {
20 std::same_as<S, unsorted<std::optional<std::string>>>;
21 template <simd::architecture Arch>
struct key_less {
48 std::filesystem::path
root;
54 std::shared_ptr<scope_type>
scope;
64 auto encoded = transport::template encode<S>(key);
66 if (!entry)
return stage_->world.template get<S>(key);
69 else return stage_->world.template get<S>(key);
79 snapshot_type(std::shared_ptr<origin const> source, std::shared_ptr<stage const> base,
96 auto encoded = transport::template encode<S>(key);
97 auto entry =
nursery_->find(encoded);
98 if (!entry)
return stage_->world.template get<S>(key);
101 else return stage_->world.template get<S>(key);
105 template <
class S = typed_detail::default_sort_t<policy_type>>
void change(
109 auto encoded = transport::template encode<S>(key);
110 auto previous =
nursery_->find(encoded);
112 if constexpr (transaction_detail::state_is_arrow<S>) {
113 if (previous)
return semantics::apply(key, semantics::initial(key),
114 typed_detail::value<policy_type, S>(previous->original->view()));
116 return stage_->world.template get<S>(key);
118 auto before = previous ? semantics::apply(key, base,
119 typed_detail::value<policy_type, S>(previous->arrow.view())) : base;
120 auto after = semantics::apply(key, before, arrow);
121 if constexpr (typed_detail::replacement<S>) {
122 if (!semantics::present(key, before) && !semantics::present(key, after))
123 throw std::invalid_argument(
"transaction deletion targets an absent key");
126 auto combined = [&] {
127 if constexpr (typed_detail::replacement<S>)
return arrow;
128 else if (previous)
return semantics::compose(key,
129 typed_detail::value<policy_type, S>(previous->arrow.view()), arrow);
132 auto value = typed_detail::value<policy_type, S>(combined);
133 if constexpr (typed_detail::replacement<S>) {
134 if (after == base) {
nursery_->erase(encoded);
return; }
136 std::optional<bit_string> original;
137 if constexpr (transaction_detail::state_is_arrow<S>) original.emplace(typed_detail::value<policy_type, S>(base));
138 nursery_->insert_or_assign(std::move(encoded),
nursery_value{std::move(value), std::move(original)});
140 template <
class S = typed_detail::default_sort_t<policy_type>>
void put(
142 requires typed_detail::replacement<S> { change<S>(key, value); }
151 auto batch = Core::batch();
153 transport::dispatch(key.
view(), [&]<
class S>(std::type_identity<S>,
auto const & decoded) {
154 batch.template change<S>(decoded, typed_detail::value<policy_type, S>(value.arrow.view()));
157 auto input = std::move(batch).finish();
161 while (!
core_->admission_ready()) (void)
core_->advance(
origin_->maintenance_budget);
162 auto updated =
core_->contribute(std::move(input));
163 store_->prepare(updated.runtime());
164 auto next = std::make_shared<stage const>(
stage{std::move(updated),
scope_,
true});
169 }
catch (...) {
failed_ =
true;
throw; }
174 auto target =
origin_->session.lock();
178 typename engine_type::prepared_transaction prepared{std::move(
core_),
origin_->base,
scope_};
179 typename engine_type::contribution_type command(std::move(prepared));
184 auto result = target->submit(std::move(command));
195 std::optional<nursery_type>
nursery_{std::in_place};
208 : origin_(saved.origin_), stage_(saved.stage_), nursery_(
nursery_type::thaw(saved.nursery_)), scope_(stage_->scope) {}
211 if (!scope_) scope_ = scope_type::create(origin_->root);
212 if (!store_) store_ = std::make_unique<store_type>(store_type::open(origin_->root, {}, scope_->options()));
213 auto const & schema = stage_->world.metadata().schema_id;
214 if constexpr (
requires { family_type::open_storage(origin_->root, schema, scope_->options()); })
215 core_ = std::make_unique<Core>(Core::from_snapshot(stage_->world,
216 family_type::open_storage(origin_->root, schema, scope_->options())));
218 static_assert(!
requires { family_type::open_storage(origin_->root); },
219 "streaming transaction storage must accept private catalog options");
220 core_ = std::make_unique<Core>(Core::from_snapshot(stage_->world));
224 if (closed_ || !origin_)
throw std::logic_error(
"closed Everett transaction");
225 if (failed())
throw std::logic_error(
"failed Everett transaction; abort it");
228 core_.reset(); store_.reset(); scope_.reset(); stage_.reset(); origin_.reset();
Opens durable named typed sessions with mutable commands and immutable saved worlds.
constexpr bool state_is_arrow
Definition transaction.h:19
typename sort_codec< S >::value_codec::value_type arrow_t
Definition typed_world.h:53
typename transport< P, Family >::type transport_t
Definition typed_world.h:152
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
Keeps ordered nursery edits behind fully persistent snapshots.
bit_view view() const &
Definition profile.h:178
Definition connection.h:294
runtime_store< policy_type, random_object_ids, sqlite_catalog_ops, typename Core::runtime_family > store_type
Definition connection.h:304
persistent_engine< Core > engine_type
Definition connection.h:297
typename session_type::ticket ticket
Definition connection.h:302
session< engine_type > session_type
Definition connection.h:298
typename engine_type::world_type world_type
Definition connection.h:299
Definition nursery_map.h:67
std::size_t size() const noexcept
Definition nursery_map.h:80
Value const * find(Key const &key) const
Definition nursery_map.h:79
Definition nursery_map.h:33
Definition private_construction.h:27
Definition typed_world.h:31
Definition transaction.h:38
std::optional< bit_string > original
Definition transaction.h:40
bit_string arrow
Definition transaction.h:39
Definition transaction.h:47
std::weak_ptr< session_type > session
Definition transaction.h:49
std::uint64_t maintenance_budget
Definition transaction.h:51
std::filesystem::path root
Definition transaction.h:48
stored_type base
Definition transaction.h:50
Definition transaction.h:62
std::shared_ptr< stage const > stage_
Definition transaction.h:77
auto get(typed_detail::key_t< S > const &key) const
Definition transaction.h:63
std::size_t buffered_keys() const noexcept
Definition transaction.h:73
snapshot_type(std::shared_ptr< origin const > source, std::shared_ptr< stage const > base, typename nursery_type::snapshot_type values)
Definition transaction.h:79
nursery_type::snapshot_type nursery_
Definition transaction.h:78
friend transaction
Definition transaction.h:82
std::shared_ptr< origin const > origin_
Definition transaction.h:76
transaction branch() const
Definition transaction.h:74
Definition transaction.h:53
world_type world
Definition transaction.h:55
bool changed
Definition transaction.h:56
stage(world_type state, std::shared_ptr< scope_type > lease, bool modified)
Definition transaction.h:57
std::shared_ptr< scope_type > scope
Definition transaction.h:54
Definition transaction.h:21
bool operator()(bit_string const &a, bit_string const &b) const
Definition transaction.h:22
Definition transaction.h:28
bool failed_
Definition transaction.h:201
typename connection_type::engine_type engine_type
Definition transaction.h:34
typename Core::policy_type policy_type
Definition transaction.h:29
typename Core::world_type world_type
Definition transaction.h:32
transaction(snapshot_type const &saved)
Definition transaction.h:207
std::shared_ptr< origin const > origin_
Definition transaction.h:193
snapshot_type snapshot()
Definition transaction.h:90
std::shared_ptr< scope_type > scope_
Definition transaction.h:198
void erase(typed_detail::key_t< S > const &key)
Definition transaction.h:143
friend connection_type
Definition transaction.h:231
transaction(transaction const &)=delete
void ensure_core()
Definition transaction.h:209
bool closed_
Definition transaction.h:201
typename Core::runtime_family family_type
Definition transaction.h:30
transaction & operator=(transaction &&)=delete
transaction(connection_type &connection)
Definition transaction.h:203
void release() noexcept
Definition transaction.h:227
void change(typed_detail::key_t< S > const &key, typed_detail::arrow_t< S > const &arrow)
Definition transaction.h:105
typed_detail::transport_t< policy_type, family_type > transport
Definition transaction.h:31
typename connection_type::store_type store_type
Definition transaction.h:44
typename connection_type::session_type session_type
Definition transaction.h:35
std::shared_ptr< stage const > stage_
Definition transaction.h:194
auto get(typed_detail::key_t< S > const &key) const
Definition transaction.h:94
transaction & operator=(transaction const &)=delete
typename connection_type::ticket ticket
Definition transaction.h:37
typename connection_type::world_type stored_type
Definition transaction.h:36
stored_type commit()
Definition transaction.h:188
void abort() noexcept
Definition transaction.h:189
std::unique_ptr< store_type > store_
Definition transaction.h:199
ticket commit_async()
Definition transaction.h:172
transaction(transaction &&)=default
snapshot_type flush()
Definition transaction.h:148
void put(typed_detail::key_t< S > const &key, typed_detail::state_t< S > const &value)
Definition transaction.h:140
std::unique_ptr< Core > core_
Definition transaction.h:200
bool failed() const noexcept
Definition transaction.h:190
void require_active() const
Definition transaction.h:223
std::optional< nursery_type > nursery_
Definition transaction.h:195
Definition multiverse.h:41