Repository navigation
Expand file tree
/
Copy pathentity_save_preparation.cpp
More file actions
674 lines (660 loc) · 29.2 KB
/
Copy pathentity_save_preparation.cpp
File metadata and controls
674 lines (660 loc) · 29.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#include <teaql/runtime/entity_save.hpp>
#include <teaql/runtime/detail/context_roots.hpp>
#include <teaql/runtime/detail/checker_intent.hpp>
#include <teaql/runtime/detail/ledger_keys.hpp>
#include <teaql/core/detail/debug.hpp>
#include <teaql/runtime/detail/checker_rendering.hpp>
namespace teaql::detail {
RuntimeHookResult capture_ledger_trace_scopes(const EntityRuntimeState& root,
const GraphNode& node) {
auto r = root.memory_resource();
auto intent = MutationIntent::from_optional(
r, node.comment ? std::optional<std::string_view>(*node.comment) : std::nullopt);
if (!intent) {
return RuntimeHookResult::err(r, RuntimeError::request_intent(r, intent.error()));
}
using Scopes = EntityRuntimeAccess::TraceScopes;
Scopes scopes(r), unassigned(r);
auto annotated = [&](const GraphNode& current) {
auto text = current.comment ? std::string_view(*current.comment) : std::string_view{};
trim(text);
return !text.empty();
};
auto unallocated = [](const Value& value) {
if (std::holds_alternative<std::monostate>(value.data)) {
return true;
}
if (auto* id = std::get_if<std::uint64_t>(&value.data)) {
return *id == 0;
}
if (auto* id = std::get_if<std::int64_t>(&value.data)) {
return *id == 0;
}
return false;
};
auto make_scope =
[&](const GraphNode& current, const TraceScopeToken* parent, std::uint64_t index) {
if (!annotated(current)) {
return parent;
}
auto* value = current.id();
auto id = value ? value->try_u64() : std::nullopt;
if (id && *id == 0) {
id.reset();
}
TraceNode track(std::allocator_arg,
Allocator(r),
TraceKind::AuditReason,
current.entity,
*current.comment,
id);
return static_cast<const TraceScopeToken*>(
&resource_arena(r).make<TraceScopeToken>(parent, track, index));
};
auto* root_scope = make_scope(node, nullptr, 0);
struct Pending {
const GraphNode* node;
const TraceScopeToken* parent;
bool root;
};
Vector<Pending> pending(r);
pending.push_back({&node, root_scope, true});
while (!pending.empty()) {
auto entry = pending.back();
pending.pop_back();
const auto& current = *entry.node;
auto* scope = entry.root ? root_scope : make_scope(current, entry.parent, scopes.size());
if (auto* id = current.id(); id && scope) {
EntityKey key(std::allocator_arg, Allocator(r), current.entity, *id);
if (unallocated(*id) && annotated(current)) {
unassigned.try_emplace(clone_in(r, key), scope);
}
scopes.try_emplace(std::move(key), scope);
}
for (auto relation = current.relations.rbegin(); relation != current.relations.rend();
++relation) {
for (auto child = relation->second.rbegin(); child != relation->second.rend();
++child) {
pending.push_back({&*child, scope, false});
}
}
}
auto keys = root.deleted_keys();
for (const auto& key : root.recovered_keys()) {
keys.emplace(clone_in(r, key));
}
for (const auto& key : root.new_keys()) {
keys.emplace(clone_in(r, key));
}
auto changes = root.current_change_set();
for (const auto& [key, fields] : changes.changes()) {
keys.emplace(clone_in(r, key));
}
if (auto* dynamic = changes.dynamic_changes()) {
for (const auto& [key, fields] : *dynamic) {
keys.emplace(clone_in(r, key));
}
}
for (const auto& key : keys) {
if (scopes.contains(key)) {
continue;
}
const TraceScopeToken* scope = root_scope;
auto comment = root.get_entity_comment(key);
auto text = comment ? std::string_view(*comment) : std::string_view{};
trim(text);
if (!text.empty()) {
auto id = key.id.try_u64();
if (id && *id == 0) {
id.reset();
}
TraceNode track(
std::allocator_arg, Allocator(r), TraceKind::AuditReason, key.entity, *comment, id);
scope = &resource_arena(r).make<TraceScopeToken>(root_scope, track, 0);
if (unallocated(key.id)) {
unassigned.emplace(clone_in(r, key), scope);
}
}
scopes.emplace(clone_in(r, key), scope);
}
root.set_comment(intent.value().comment());
EntityRuntimeAccess::replace_trace_scopes(root, scopes, unassigned);
return RuntimeHookResult::ok(r, {});
}
Result<GraphNode, RuntimeError>
graph_node_from_values(const UserContext& context, std::string_view entity, MutationValues values) {
auto r = context.memory_resource();
using Output = Result<GraphNode, RuntimeError>;
auto descriptor = context.require_entity(entity);
if (!descriptor) {
return Output::err(r, std::move(descriptor).error());
}
GraphNode node(std::allocator_arg, Allocator(r), entity);
const auto* symbols = values.symbols() ? values.symbols() : descriptor.value().get().symbols;
if (symbols) {
node.values =
EntityValues(std::allocator_arg,
Allocator(r),
MutationValues::indexed_with_capacity(r, *symbols, values.len()));
}
for (auto& [field_id, value] : std::move(values).into_fields()) {
const auto field = field_id.name();
if (field == "_comment") {
if (auto* text = std::get_if<String>(&value.data)) {
node.set_comment(*text);
}
continue;
}
if (field == "_dirty_fields") {
if (auto* list = std::get_if<Value::List>(&value.data)) {
node.dirty_fields.emplace(r);
for (const auto& field : *list) {
if (auto* text = std::get_if<String>(&field.data)) {
node.dirty_fields->emplace(*text);
}
}
}
continue;
}
if (field == "_original_values") {
if (auto* record = value.as_object()) {
node.original_values.emplace(r);
node.original_values->fields = clone_record(r, *record);
}
continue;
}
if (field == "_is_new" || field == "_is_deleted" || field == "_is_recovered") {
if (auto* flag = std::get_if<bool>(&value.data); flag && *flag) {
node.operation = field == "_is_new" ? GraphOperation::Create
: field == "_is_recovered" ? GraphOperation::Recover
: GraphOperation::Remove;
}
continue;
}
const auto* relation = descriptor.value().get().relation_by_name(field);
if (!relation) {
node.values.insert_field(std::move(field_id), Value(r, std::move(value)));
continue;
}
auto invalid = [&](std::string_view expected, const Value& value) {
String message("relation ", r);
message += entity;
message += '.';
message += field;
message += expected;
message += debug_value(r, value);
return Output::err(r, RuntimeError::graph(r, message));
};
if (std::holds_alternative<std::monostate>(value.data)) {
node.relations.try_emplace(String(field, r));
} else if (auto* record = value.as_object()) {
MutationValues child_values(r);
child_values.fields = clone_record(r, *record);
auto child =
graph_node_from_values(context, relation->target_entity, std::move(child_values));
if (!child) {
return Output::err(r, std::move(child).error());
}
node.relations.try_emplace(String(field, r))
.first->second.emplace_back(std::move(child).value());
} else if (auto* list = value.as_list()) {
auto& children = node.relations.try_emplace(String(field, r)).first->second;
for (const auto& value : *list) {
auto* record = value.as_object();
if (!record) {
return invalid(" expects object children, got ", value);
}
MutationValues child_values(r);
child_values.fields = clone_record(r, *record);
auto child = graph_node_from_values(
context, relation->target_entity, std::move(child_values));
if (!child) {
return Output::err(r, std::move(child).error());
}
children.emplace_back(std::move(child).value());
}
} else {
return invalid(" expects object/list/null, got ", value);
}
}
return Output::ok(r, std::move(node));
}
static RuntimeHookResult preflight_graph_inner(const UserContext& context,
GraphNode& node,
const ObjectLocation& requested_location,
const EntityRuntimeState* root,
const LedgerObjectLocations& selected) {
auto r = context.memory_resource();
auto nr = node.memory_resource();
const ObjectLocation* current_location = &requested_location;
if (const auto* id = node.id()) {
EntityKey key(std::allocator_arg, Allocator(r), node.entity, *id);
if (auto found = selected.find(key); found != selected.end()) {
current_location = &found->second;
}
}
const auto& location = *current_location;
if (node.operation != GraphOperation::Remove && node.operation != GraphOperation::Reference) {
auto before = clone_in(nr, node.values);
mark_entity_status(node.values,
node.operation == GraphOperation::Create ? CheckObjectStatus::Create
: CheckObjectStatus::Update);
if (node.dirty_fields) {
Value::List fields(nr);
for (const auto& field : *node.dirty_fields) {
fields.emplace_back(field);
}
node.values.fields.insert_or_assign(String("_dirty_fields", nr), Value(nr, fields));
}
bool dynamic_changed = false;
if (root) {
if (const auto* id = node.id()) {
EntityKey key(std::allocator_arg, Allocator(nr), node.entity, *id);
dynamic_changed = EntityRuntimeAccess::has_dynamic_mutation_for(*root, key);
}
}
auto result = ContextCheckerAccess::check_storage_mutation(
context,
node.entity,
node.values,
location,
CheckMutationState{node.original_values ? &*node.original_values : nullptr,
node.dirty_fields ? &*node.dirty_fields : nullptr,
dynamic_changed});
clear_entity_status(node.values);
if (auto field = node.values.fields.find("_dirty_fields");
field != node.values.fields.end()) {
node.values.fields.erase(field);
}
if (!result) {
return result;
}
if (root) {
if (const auto* id = node.id()) {
EntityKey key(std::allocator_arg, Allocator(nr), node.entity, *id);
for (const auto& [field, value] : node.values.fields) {
auto old = before.fields.find(field);
if (old == before.fields.end() || old->second != value) {
auto descriptor = context.require_entity(node.entity);
if (!descriptor) {
return RuntimeHookResult::err(nr, std::move(descriptor).error());
}
auto canonical =
canonical_field_from_storage(nr, descriptor.value().get(), field);
if (!canonical) {
return RuntimeHookResult::err(nr, std::move(canonical).error());
}
// Retain an existing legacy setter's native spelling
// rather than introducing a second alias for its edit.
auto name = std::string_view(canonical.value());
if (name != field && root->get(key, field)) {
name = field;
}
root->set(key, name, value);
}
}
}
}
}
for (auto& [name, children] : node.relations) {
for (std::size_t index = 0; index < children.size(); ++index) {
auto path = clone_in(r, location);
path.member(name).element(index);
auto result = preflight_graph_inner(context, children[index], path, root, selected);
if (!result) {
return result;
}
}
}
return RuntimeHookResult::ok(r, {});
}
RuntimeHookResult preflight_graph(const UserContext& context,
GraphNode& node,
const ObjectLocation& location,
const EntityRuntimeState* root) {
checker_messages::detail::OriginActivation origin(context, node.entity);
auto admitted = validate_context_roots(context, node, location, root);
if (!admitted) {
return admitted;
}
auto r = context.memory_resource();
LedgerObjectLocations selected(r);
if (root && node.id()) {
EntityRuntimeState::Keys wanted(r);
for (const auto& [key, path] : ledger_object_locations(r, node)) {
wanted.emplace(key);
}
EntityKey origin_key(std::allocator_arg, Allocator(r), node.entity, *node.id());
auto loaded = EntityRuntimeAccess::loaded_object_locations(*root, origin_key, wanted);
for (const auto& [key, path] : loaded) {
auto resolved = clone_in(r, location);
for (const auto& segment : path.segments()) {
if (const auto* name = std::get_if<String>(&segment.data)) {
resolved.member(*name);
} else {
resolved.element(std::get<std::size_t>(segment.data));
}
}
selected.emplace(key, std::move(resolved));
}
}
return preflight_graph_inner(context, node, location, root, selected);
}
RuntimeHookResult preflight_pending_dynamic_entities(const UserContext& context,
const EntityRuntimeState& root,
const GraphNode& node) {
auto r = context.memory_resource();
auto changes = root.current_change_set();
const auto* dynamic = changes.dynamic_changes();
if (!dynamic) {
return RuntimeHookResult::ok(r, {});
}
auto locations = ledger_object_locations(r, node);
for (const auto& [key, fields] : *dynamic) {
if (locations.contains(key) || root.is_marked_as_delete(key)) {
continue;
}
GraphNode pending(std::allocator_arg, Allocator(r), key.entity);
pending.original_values = EntityRuntimeAccess::original_values_for(root, key);
if (pending.original_values) {
pending.values.fields = clone_record(r, pending.original_values->fields);
}
auto descriptor = context.require_entity(key.entity);
if (!descriptor) {
return RuntimeHookResult::err(r, std::move(descriptor).error());
}
if (auto values = changes.changes().find(key); values != changes.changes().end()) {
auto storage = storage_values_from_ledger(r, descriptor.value().get(), values->second);
if (!storage) {
return RuntimeHookResult::err(r, std::move(storage).error());
}
for (const auto& [field, value] : storage.value().named_entries()) {
pending.values.insert_named(field, Value(r, value));
}
}
pending.values.insert_named("id", Value(r, key.id));
Value::List loaded(r);
for (const auto& [field, value] : pending.values.named_entries()) {
if (!field.starts_with('_')) {
loaded.emplace_back(field);
}
}
pending.values.insert_named("_loaded_fields", Value(r, loaded));
auto dirty =
storage_fields_from_ledger(r, descriptor.value().get(), root.changed_field_names(key));
if (!dirty) {
return RuntimeHookResult::err(r, std::move(dirty).error());
}
pending.dirty_fields.emplace(std::move(dirty).value());
pending.operation = root.is_marked_as_recover(key) ? GraphOperation::Recover
: root.is_new(key) ? GraphOperation::Create
: GraphOperation::Upsert;
auto result = preflight_graph(context, pending, ObjectLocation::root(r), &root);
if (!result) {
return result;
}
}
return RuntimeHookResult::ok(r, {});
}
static RuntimeHookResult merge_graph_value(const EntityRuntimeState& root,
const EntityKey& key,
const EntityDescriptor& descriptor,
std::string_view field,
const Value& value) {
auto r = root.memory_resource();
auto canonical = canonical_field_from_storage(r, descriptor, field);
if (!canonical) {
return RuntimeHookResult::err(r, std::move(canonical).error());
}
if (canonical.value() != field) {
if (auto previous = root.get(key, field)) {
if (*previous == value) {
return RuntimeHookResult::ok(r, {});
}
String message("conflicting relation ledger aliases ", r);
message += descriptor.name;
message += '.';
message += field;
message += " and ";
message += descriptor.name;
message += '.';
message += canonical.value();
return RuntimeHookResult::err(r, RuntimeError::graph(r, message));
}
}
root.set(key, canonical.value(), value);
return RuntimeHookResult::ok(r, {});
}
RuntimeHookResult merge_relation_mutations_into_root(const UserContext& context,
const EntityRuntimeState& root,
const GraphNode& node) {
auto r = root.memory_resource();
for (const auto& [name, children] : node.relations) {
for (const auto& child : children) {
const auto* id = child.id();
if (!id) {
String message("related mutation ", r);
message += child.entity;
message += " is missing its id";
return RuntimeHookResult::err(r, RuntimeError::graph(r, message));
}
EntityKey key(std::allocator_arg, Allocator(r), child.entity, *id);
auto descriptor = context.require_entity(child.entity);
if (!descriptor) {
return RuntimeHookResult::err(r, std::move(descriptor).error());
}
if (child.operation == GraphOperation::Create) {
root.mark_as_new(key);
auto admitted = canonical_values_from_storage(
r, descriptor.value().get(), child.values.mutation_values());
if (!admitted) {
return RuntimeHookResult::err(r, std::move(admitted).error());
}
for (const auto& [field, value] : child.values.fields) {
auto merged =
merge_graph_value(root, key, descriptor.value().get(), field, value);
if (!merged) {
return merged;
}
}
} else if (child.operation == GraphOperation::Upsert ||
child.operation == GraphOperation::Recover) {
if (child.operation == GraphOperation::Recover) {
root.mark_as_recover(key);
}
if (child.dirty_fields) {
for (const auto& field : *child.dirty_fields) {
if (auto value = child.values.fields.find(field);
value != child.values.fields.end()) {
auto merged = merge_graph_value(
root, key, descriptor.value().get(), field, value->second);
if (!merged) {
return merged;
}
}
}
}
} else if (child.operation == GraphOperation::Remove) {
root.mark_as_delete(key);
}
if (child.original_values) {
if (auto version = child.original_values->fields.find("version");
version != child.original_values->fields.end()) {
if (auto value = version->second.try_i64()) {
root.set_original_version(key, *value);
}
}
}
auto result = merge_relation_mutations_into_root(context, root, child);
if (!result) {
return result;
}
}
}
return RuntimeHookResult::ok(r, {});
}
static RuntimeHookResult hydrate_ledger_relations_borrowed(const UserContext& context,
const LedgerPreflightView& view,
GraphNode& node,
EntityRuntimeState::Keys& visited) {
auto r = context.memory_resource();
auto nr = node.memory_resource();
auto descriptor = context.require_entity(node.entity);
if (!descriptor) {
return RuntimeHookResult::err(r, std::move(descriptor).error());
}
const auto& changes = view.changes();
for (const auto& relation : descriptor.value().get().relations) {
const auto* local = node.values.get(relation.local_key);
if (!local) {
continue;
}
auto local_value = Value(nr, *local);
auto& existing = node.relations.try_emplace(String(relation.name, nr)).first->second;
EntityRuntimeState::Keys existing_keys(nr);
for (const auto& child : existing) {
if (const auto* id = child.id()) {
existing_keys.emplace(child.entity, *id);
}
}
Vector<GraphNode> discovered(nr);
const auto pending = view.pending_keys();
for (const auto& key : pending) {
if (key.entity != relation.target_entity || existing_keys.contains(key)) {
continue;
}
const auto* original = view.original_values(key);
auto target = context.require_entity_key(key);
if (!target) {
return RuntimeHookResult::err(r, std::move(target).error());
}
EntityValues values(std::allocator_arg, Allocator(nr));
if (const auto* symbols = target.value().get().read().symbols) {
values.bind_symbols(*symbols);
}
if (original) {
for (const auto& [field, value] : original->fields) {
values.insert_named(field, Value(nr, value));
}
}
if (auto fields = changes.changes().find(key); fields != changes.changes().end()) {
auto storage = storage_values_from_ledger(nr, target.value().get(), fields->second);
if (!storage) {
return RuntimeHookResult::err(r, std::move(storage).error());
}
for (const auto& [field, value] : storage.value().named_entries()) {
values.insert_named(field, Value(nr, value));
}
}
const Value* foreign =
relation.foreign_key == "id" ? &key.id : values.get(relation.foreign_key);
if (std::holds_alternative<std::monostate>(local_value.data) || !foreign ||
EntityKey(std::allocator_arg, Allocator(nr), relation.target_entity, *foreign) !=
EntityKey(
std::allocator_arg, Allocator(nr), relation.target_entity, local_value) ||
!visited.emplace(key).second) {
continue;
}
GraphNode child(std::allocator_arg, Allocator(nr), key.entity);
child.values = std::move(values);
if (!child.values.contains_key("id")) {
child.values.insert_named("id", Value(nr, key.id));
}
child.operation = view.is_deleted(key) ? GraphOperation::Remove
: view.is_recovered(key) ? GraphOperation::Recover
: view.is_new(key) || !view.original_version(key)
? GraphOperation::Create
: GraphOperation::Upsert;
child.comment = clone_in(nr, view.comment(key));
if (original) {
child.original_values.emplace(std::allocator_arg, Allocator(nr), *original);
}
auto fields =
storage_fields_from_ledger(nr, target.value().get(), view.dirty_fields(key));
if (!fields) {
return RuntimeHookResult::err(r, std::move(fields).error());
}
child.dirty_fields.emplace(std::move(fields).value());
if (child.operation != GraphOperation::Remove) {
auto result = hydrate_ledger_relations_borrowed(context, view, child, visited);
if (!result) {
return result;
}
}
discovered.emplace_back(std::move(child));
}
for (auto& child : discovered) {
existing.emplace_back(std::move(child));
}
}
return RuntimeHookResult::ok(r, {});
}
RuntimeHookResult hydrate_ledger_relations(const UserContext& context,
const EntityRuntimeState& root,
GraphNode& node,
EntityRuntimeState::Keys& visited) {
std::optional<RuntimeHookResult> result;
auto hydrate = [&](const LedgerPreflightView& view) {
result.emplace(hydrate_ledger_relations_borrowed(context, view, node, visited));
};
EntityRuntimeAccess::with_preflight_view(root, hydrate);
return std::move(*result);
}
LedgerObjectLocations ledger_object_locations(Memory r, const GraphNode& node) {
LedgerObjectLocations locations(r);
auto visit = [&](auto&& self, const GraphNode& node, const ObjectLocation& path) -> void {
if (const auto* id = node.id()) {
locations.try_emplace(EntityKey(std::allocator_arg, Allocator(r), node.entity, *id),
path);
}
for (const auto& [name, children] : node.relations) {
for (std::size_t index = 0; index < children.size(); ++index) {
auto next = clone_in(r, path);
next.member(name).element(index);
self(self, children[index], next);
}
}
};
visit(visit, node, ObjectLocation::root(r));
return locations;
}
RuntimeHookResult reject_cancelled_new_root(const EntityRuntimeState& root, const EntityKey& key) {
auto r = root.memory_resource();
if (root.new_keys().contains(key) && root.deleted_keys().contains(key)) {
String message("cancelled new root ", r);
message += key.debug(r);
message += ": create-then-delete has no persisted entity to return";
return RuntimeHookResult::err(r, RuntimeError::graph(r, message));
}
return RuntimeHookResult::ok(r, {});
}
bool can_preserve_loaded_relations(const EntityRuntimeState& root,
const EntityKey& key,
const EntityDescriptor& descriptor) {
if (!root.new_keys().empty() || !root.deleted_keys().empty() ||
!root.recovered_keys().empty()) {
return false;
}
auto changes = root.current_change_set();
if (auto* dynamic = changes.dynamic_changes()) {
for (const auto& [changed, fields] : *dynamic) {
if (changed != key) {
return false;
}
}
}
for (const auto& [changed, fields] : changes.changes()) {
if (changed != key) {
return false;
}
for (const auto& [field, value] : fields.fields) {
for (const auto& relation : descriptor.relations) {
if (relation.local_key == field) {
return false;
}
}
}
}
return true;
}
}