Repository navigation
Expand file tree
/
Copy pathmutation_policy_plan.cpp
More file actions
219 lines (213 loc) · 9.4 KB
/
Copy pathmutation_policy_plan.cpp
File metadata and controls
219 lines (213 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/entity_runtime.hpp>
#include <teaql/runtime/dynamic_fields.hpp>
#include <teaql/runtime/detail/mutation_version.hpp>
#include <charconv>
#include <mutex>
namespace teaql::detail {
namespace {
RuntimeHookResult validate_versions(Memory resource, const MutationPlan& plan) {
for (const auto& operation : plan.operations) {
if (operation.kind == MutationOperationKind::Delete) {
auto version = mutation_version::deleted(
resource, operation.entity.entity, operation.original_version);
if (!version) {
return RuntimeHookResult::err(resource, std::move(version).error());
}
} else if (operation.kind == MutationOperationKind::Recover) {
if (!operation.original_version) {
return RuntimeHookResult::err(
resource,
RuntimeError::graph(resource,
"recovery requires a loaded original tombstone version"));
}
auto version = mutation_version::recovered(
resource, operation.entity.entity, *operation.original_version);
if (!version) {
return RuntimeHookResult::err(resource, std::move(version).error());
}
} else if (operation.kind == MutationOperationKind::Update &&
!operation.changed_values.empty()) {
auto version = mutation_version::updated(
resource, operation.entity.entity, operation.original_version);
if (!version) {
return RuntimeHookResult::err(resource, std::move(version).error());
}
}
}
return RuntimeHookResult::ok(resource, {});
}
String next_execution_id(const UserContext& context) {
// A process-local scalar namespace, as in Rust. It owns no Context data.
// A mutex avoids requiring a 64-bit atomic runtime on 32-bit MIPS.
static std::mutex lock;
static std::uint64_t sequence = 1;
std::uint64_t value;
{
std::lock_guard guard(lock);
value = sequence++;
}
String result(context.trace_id(), context.memory_resource());
result += "-mutation-";
char buffer[32];
auto end = std::to_chars(buffer, buffer + sizeof(buffer), value).ptr;
result.append(buffer, end);
return result;
}
}
RuntimeHookResult
validate_ledger_models(const UserContext& context,
const EntityChangeSet& changes,
std::span<const std::pmr::set<EntityKey>* const> lifecycle_keys) {
auto memory = context.memory_resource();
std::optional<RuntimeError> error;
auto admit = [&](const EntityKey&, const EntitySymbols* symbols) {
if (symbols && !error) {
auto registered = context.require_entity_symbols(*symbols);
if (!registered) {
error.emplace(std::move(registered).error());
}
}
};
changes.visit_scoped_keys(admit);
for (const auto* keys : lifecycle_keys) {
for (const auto& key : *keys) {
admit(key, key.symbols());
}
}
return error ? RuntimeHookResult::err(memory, std::move(*error))
: RuntimeHookResult::ok(memory, {});
}
Result<MutationPlan, RuntimeError> graph_policy_plan(const UserContext& context,
const GraphMutationPlan& graph) {
auto r = context.memory_resource();
if (!graph.planned_root) {
return Result<MutationPlan, RuntimeError>::err(
r, RuntimeError::graph(r, "graph mutation plan has no planned root"));
}
MutationPlan plan(std::allocator_arg, Allocator(r));
const auto& root = *graph.planned_root;
plan.root_entity_type = root.entity;
plan.request_key = root.entity;
plan.request_key += ".saveGraph";
plan.audit_reason = clone_in(r, root.comment);
for (const auto& item : graph.items) {
if (item.kind == GraphMutationKind::Reference) {
continue;
}
auto kind = item.kind == GraphMutationKind::Create ? MutationOperationKind::Create
: item.kind == GraphMutationKind::Delete ? MutationOperationKind::Delete
: item.kind == GraphMutationKind::Recover ? MutationOperationKind::Recover
: MutationOperationKind::Update;
const auto& values = item.values;
auto id = values.get("id");
Value missing_id(r, std::int64_t(0));
EntityKey key(std::allocator_arg, Allocator(r), item.entity, id ? *id : missing_id);
plan.operations.emplace_back(kind, key);
auto& operation = plan.operations.back();
if (const auto* version = values.get("version")) {
operation.original_version = version->try_i64();
}
if (kind == MutationOperationKind::Create) {
operation.changed_values = MutationValues(r, values);
} else if (kind == MutationOperationKind::Update ||
kind == MutationOperationKind::Recover) {
if (const auto* symbols = values.symbols()) {
operation.changed_values = MutationValues::indexed(r, *symbols);
}
for (const auto& field : item.update_fields) {
if (const auto* value = values.get(field)) {
operation.changed_values.insert_named(field, Value(r, *value));
}
}
}
}
plan.execution_id = next_execution_id(context);
auto validated = validate_versions(r, plan);
if (!validated) {
return Result<MutationPlan, RuntimeError>::err(r, std::move(validated).error());
}
return Result<MutationPlan, RuntimeError>::ok(r, std::move(plan));
}
Result<MutationPlan, RuntimeError> ledger_policy_plan(const UserContext& context,
const GraphNode& node,
const EntityRuntimeState& root) {
auto changes = root.current_change_set(context.memory_resource());
return ledger_policy_plan_from(context, node, root, changes);
}
Result<MutationPlan, RuntimeError> ledger_policy_plan_from(const UserContext& context,
const GraphNode& node,
const EntityRuntimeState& root,
const EntityChangeSet& changes) {
auto r = context.memory_resource();
auto deleted = root.deleted_keys();
auto fresh = root.new_keys();
auto recovered = root.recovered_keys();
const std::array<const EntityRuntimeState::Keys*, 3> lifecycle{&deleted, &fresh, &recovered};
auto admitted = validate_ledger_models(context, changes, lifecycle);
if (!admitted) {
return Result<MutationPlan, RuntimeError>::err(r, std::move(admitted).error());
}
for (const auto& key : recovered) {
if (fresh.contains(key) || deleted.contains(key)) {
return Result<MutationPlan, RuntimeError>::err(
r,
RuntimeError::graph(
r, "cannot recover a new entity or conflicting lifecycle intentions"));
}
}
EntityRuntimeState::Keys keys(r);
for (const auto& [key, values] : changes.changes()) {
keys.emplace(key);
}
if (const auto* dynamic = changes.dynamic_changes()) {
for (const auto& [key, values] : *dynamic) {
keys.emplace(key);
}
}
for (const auto& key : deleted) {
keys.emplace(key);
}
for (const auto& key : recovered) {
keys.emplace(key);
}
MutationPlan plan(std::allocator_arg, Allocator(r));
plan.request_key = node.entity;
plan.request_key += ".saveGraph";
plan.root_entity_type = node.entity;
auto comment = root.get_comment();
plan.audit_reason = clone_in(r, comment ? comment : node.comment);
for (const auto& key : keys) {
if (fresh.contains(key) && deleted.contains(key)) {
continue;
}
auto kind = deleted.contains(key) ? MutationOperationKind::Delete
: recovered.contains(key) ? MutationOperationKind::Recover
: fresh.contains(key) ? MutationOperationKind::Create
: MutationOperationKind::Update;
plan.operations.emplace_back(kind, key);
auto& operation = plan.operations.back();
operation.original_version = root.get_original_version(key);
auto found = changes.changes().find(key);
if (found != changes.changes().end()) {
operation.changed_values = MutationValues(r, found->second);
}
if (const auto* dynamic = changes.dynamic_changes()) {
if (auto fields = dynamic->find(key); fields != dynamic->end()) {
for (const auto& [code, mutation] : fields->second) {
String field("#", r);
field += code;
operation.changed_values.insert_named(
field, mutation_audit_value(r, mutation, nullptr));
}
}
}
}
plan.execution_id = next_execution_id(context);
auto validated = validate_versions(r, plan);
if (!validated) {
return Result<MutationPlan, RuntimeError>::err(r, std::move(validated).error());
}
return Result<MutationPlan, RuntimeError>::ok(r, std::move(plan));
}
}