Repository navigation
Expand file tree
/
Copy pathrelation_installation.cpp
More file actions
428 lines (414 loc) · 19.7 KB
/
Copy pathrelation_installation.cpp
File metadata and controls
428 lines (414 loc) · 19.7 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
#include <teaql/runtime/detail/data_service/relation_installation.hpp>
namespace teaql::detail {
void validate_relation_domain(const UserContext& context,
std::span<const CompactRow> rows,
const EntityRuntimeState* root,
const EntityGraphBuilder* graph) {
ContextEntityAccess::ensure_open(context);
auto r = context.memory_resource();
if ((root && root->memory_resource() != r) || (graph && graph->memory_resource() != r)) {
throw std::invalid_argument("Relation graph and root must belong to the query Context");
}
for (const auto& row : rows) {
if (row.memory_resource() != r) {
throw std::invalid_argument("Mutable relation rows must belong to the query Context");
}
}
}
namespace {
const RelationDescriptor* inverse(const UserContext& context, const RelationLoadPlan& plan) {
if (auto* entity = context.entity(plan.target_entity)) {
for (const auto& relation : entity->relations) {
if (relation.target_entity == plan.parent_entity &&
relation.local_key == plan.foreign_key && relation.foreign_key == plan.local_key) {
return &relation;
}
}
}
return nullptr;
}
using Buckets = std::pmr::map<String, Vector<CompactRow>, std::less<>>;
Buckets buckets(Memory r, const RelationLoadPlan& plan, Vector<CompactRow> rows) {
Buckets out(r);
for (auto& row : rows) {
if (auto* key = row.get(plan.foreign_key)) {
out.try_emplace(graph_identity_key(r, *key)).first->second.emplace_back(std::move(row));
}
}
return out;
}
Vector<CompactRow> take_related(Memory r, Buckets& buckets, const Value& key, bool consume) {
auto it = buckets.find(graph_identity_key(r, key));
if (it == buckets.end()) {
return Vector<CompactRow>(r);
}
if (!consume) {
return Vector<CompactRow>(it->second, r);
}
auto rows = std::move(it->second);
buckets.erase(it);
return rows;
}
EntityGraphDecodeResult
install_reverse(const UserContext& context,
const CompactRow& parent,
const RelationLoadPlan& plan,
Vector<CompactRow> rows,
const EntityRuntimeState& root,
EntityGraphBuilder& graph,
const EntityGraphBuilder::RelationFacets* facets = nullptr) {
auto r = context.memory_resource();
auto* id = parent.get("id");
auto owner = id ? id->try_u64() : std::nullopt;
if (!owner) {
return EntityGraphDecodeResult::err(
r,
EntityError(std::allocator_arg,
Allocator(r),
plan.parent_entity,
"loaded reverse relation owner is missing its u64 id"));
}
if (plan.many) {
SmartList<CompactRow> list(std::allocator_arg, Allocator(r), std::move(rows));
if (facets) {
for (const auto& [name, values] : *facets) {
list.facets.emplace(name, values);
}
}
return ContextEntityAccess::decode_entity_graph_compact_smart_list(context,
plan.target_entity,
std::move(list),
root,
graph,
plan.parent_entity,
*owner,
plan.relation_name);
}
return ContextEntityAccess::decode_entity_graph_compact_option(context,
plan.target_entity,
std::move(rows),
root,
graph,
plan.parent_entity,
*owner,
plan.relation_name);
}
EntityGraphDecodeResult install_unfetched_forward(const UserContext& context,
const CompactRow& parent,
const RelationLoadPlan& plan,
const EntityRuntimeState& root,
EntityGraphBuilder& graph) {
auto r = context.memory_resource();
const auto* id = parent.get("id");
auto owner = id ? id->try_u64() : std::nullopt;
if (!owner) {
return EntityGraphDecodeResult::err(
r,
EntityError(std::allocator_arg,
Allocator(r),
plan.parent_entity,
"forward relation owner is missing its u64 id"));
}
Vector<CompactRow> rows(r);
if (const auto* key = parent.get(plan.local_key); key && !key->is_null()) {
Record identity(r);
identity.emplace(String(plan.foreign_key, r), Value(r, *key));
rows.emplace_back(std::move(identity));
}
return ContextEntityAccess::decode_entity_graph_compact_option(context,
plan.target_entity,
std::move(rows),
root,
graph,
plan.parent_entity,
*owner,
plan.relation_name);
}
}
void attach_relation_rows(const UserContext& context,
std::span<CompactRow> parents,
const RelationLoadPlan& plan,
Vector<CompactRow> children,
std::span<const std::optional<Value>> attachment_keys) {
attach_relation_rows(context, parents, parents, plan, std::move(children), attachment_keys);
}
void attach_relation_rows(const UserContext& context,
std::span<CompactRow> parents,
std::span<const CompactRow> parent_keys,
const RelationLoadPlan& plan,
Vector<CompactRow> children,
std::span<const std::optional<Value>> attachment_keys) {
validate_relation_domain(context, parents);
validate_relation_domain(context, parent_keys);
if (parents.size() != parent_keys.size()) {
throw std::invalid_argument("Relation key snapshots must correspond to parent rows");
}
auto r = context.memory_resource();
auto* back = inverse(context, plan);
if (back && std::ranges::any_of(plan.children, [&](const auto& child) {
return child.relation_name == back->name;
})) {
back = nullptr;
}
Buckets grouped(r);
if (!attachment_keys.empty() && attachment_keys.size() != children.size()) {
throw std::invalid_argument("Relation attachment keys must match the child batch");
}
for (std::size_t i = 0; i < children.size(); ++i) {
const Value* key = attachment_keys.empty()
? children[i].get(plan.foreign_key)
: (attachment_keys[i] ? &*attachment_keys[i] : nullptr);
if (key) {
grouped.try_emplace(graph_identity_key(r, *key))
.first->second.emplace_back(std::move(children[i]));
}
}
for (std::size_t index = 0; index < parents.size(); ++index) {
auto& parent = parents[index];
auto* key = parent_keys[index].get(plan.local_key);
Vector<CompactRow> related(r);
if (auto it = key ? grouped.find(graph_identity_key(r, *key)) : grouped.end();
it != grouped.end()) {
for (const auto& row : it->second) {
related.emplace_back(row);
}
}
if (back) {
auto parent_object = clone_in(r, parent);
parent_object.remove(plan.relation_name);
for (auto& row : related) {
auto parent_value = Value(r, parent_object.into_map(r));
if (!back->many) {
row.insert(back->name, parent_value);
} else {
if (!row.contains_key(back->name)) {
row.insert(back->name, Value(r, Value::List(r)));
}
if (auto* values = std::get_if<Value::List>(&row.get_mut(back->name)->data)) {
values->emplace_back(parent_value);
}
}
}
}
if (!plan.many && related.empty() && key && !key->is_null()) {
Record identity(r);
identity.emplace(String(plan.foreign_key, r), Value(r, *key));
related.emplace_back(identity);
}
if ((plan.query && !plan.query->facets.empty()) ||
std::ranges::any_of(related, [](const auto& row) {
return row.has_loaded_relations();
})) {
SmartList<CompactRow> list(std::allocator_arg, Allocator(r));
if (plan.many) {
for (const auto& row : related) {
list.data.emplace_back(row);
}
} else if (!related.empty()) {
list.data.emplace_back(related.front());
}
list.loaded();
parent.set_loaded_relation(plan.relation_name, list);
}
if (plan.many) {
Value::List values(r);
for (const auto& row : related) {
values.emplace_back(row.into_map(r));
}
parent.insert(plan.relation_name, Value(r, values));
} else {
parent.insert(plan.relation_name,
related.empty() ? Value(r) : Value(r, related.front().into_map(r)));
}
}
}
EntityGraphDecodeResult install_flat_relation(const UserContext& context,
std::span<CompactRow> parents,
const RelationLoadPlan& plan,
Vector<CompactRow> children,
const EntityRuntimeState& root,
EntityGraphBuilder& graph) {
return install_flat_relation(context, parents, plan, std::move(children), root, graph, nullptr);
}
EntityGraphDecodeResult install_flat_relation(const UserContext& context,
std::span<CompactRow> parents,
const RelationLoadPlan& plan,
Vector<CompactRow> children,
const EntityRuntimeState& root,
EntityGraphBuilder& graph,
const EntityGraphBuilder::RelationFacets* facets) {
validate_relation_domain(context, parents, &root, &graph);
auto r = context.memory_resource();
auto* back = inverse(context, plan);
if (back && std::ranges::any_of(plan.children, [&](const auto& child) {
return child.relation_name == back->name;
})) {
back = nullptr;
}
auto grouped = buckets(r, plan, std::move(children));
for (auto& parent : parents) {
auto* key = parent.get(plan.local_key);
// The source's ordinary flat path publishes graph edges without adding
// compact-row availability markers. CompactFlat marks those separately.
auto related =
key ? take_related(r, grouped, *key, plan.local_key == "id") : Vector<CompactRow>(r);
if (back) {
for (const auto& child : related) {
auto* value = child.get("id");
auto id = value ? value->try_u64() : std::nullopt;
if (!id) {
continue;
}
Vector<CompactRow> rows(r);
rows.emplace_back(parent);
auto result =
back->many
? ContextEntityAccess::decode_entity_graph_compact_list(context,
plan.parent_entity,
std::move(rows),
root,
graph,
plan.target_entity,
*id,
back->name)
: ContextEntityAccess::decode_entity_graph_compact_option(
context,
plan.parent_entity,
std::move(rows),
root,
graph,
plan.target_entity,
*id,
back->name);
if (!result) {
return result;
}
}
}
if (plan.many || plan.local_key == "id") {
auto result =
install_reverse(context, parent, plan, std::move(related), root, graph, facets);
if (!result) {
return result;
}
} else if (related.empty()) {
auto result = install_unfetched_forward(context, parent, plan, root, graph);
if (!result) {
return result;
}
} else {
for (auto& child : related) {
auto result = ContextEntityAccess::decode_entity_graph_compact(
context, plan.target_entity, std::move(child), root, graph);
if (!result) {
return result;
}
}
}
}
return EntityGraphDecodeResult::ok(r, {});
}
EntityGraphDecodeResult install_compact_flat_relation(const UserContext& context,
std::span<CompactRow> parents,
const RelationLoadPlan& plan,
Vector<CompactRow> children,
const EntityRuntimeState& root,
EntityGraphBuilder& graph) {
return install_compact_flat_relation(
context, parents, plan, std::move(children), root, graph, nullptr);
}
EntityGraphDecodeResult
install_compact_flat_relation(const UserContext& context,
std::span<CompactRow> parents,
const RelationLoadPlan& plan,
Vector<CompactRow> children,
const EntityRuntimeState& root,
EntityGraphBuilder& graph,
const EntityGraphBuilder::RelationFacets* facets) {
validate_relation_domain(context, parents, &root, &graph);
auto r = context.memory_resource();
RelationShapeCache shapes(std::allocator_arg, Allocator(r));
if (!plan.many && plan.local_key != "id") {
std::pmr::set<String> fetched(r);
for (const auto& child : children) {
if (const auto* key = child.get(plan.foreign_key)) {
fetched.emplace(graph_identity_key(r, *key));
}
}
auto result = ContextEntityAccess::decode_entity_graph_compact_batch(
context, plan.target_entity, std::move(children), root, graph);
if (!result) {
return result;
}
for (auto& parent : parents) {
const auto* key = parent.get(plan.local_key);
const bool matched = key && fetched.contains(graph_identity_key(r, *key));
if (!matched) {
auto detail = install_unfetched_forward(context, parent, plan, root, graph);
if (!detail) {
return detail;
}
}
if (matched || (key && key->is_null())) {
parent.mark_relation_loaded(plan.relation_name, shapes);
}
}
return EntityGraphDecodeResult::ok(r, {});
}
auto grouped = buckets(r, plan, std::move(children));
for (auto& parent : parents) {
parent.mark_relation_loaded(plan.relation_name, shapes);
auto* key = parent.get(plan.local_key);
auto related =
key ? take_related(r, grouped, *key, plan.local_key == "id") : Vector<CompactRow>(r);
auto result =
install_reverse(context, parent, plan, std::move(related), root, graph, facets);
if (!result) {
return result;
}
}
return EntityGraphDecodeResult::ok(r, {});
}
EntityGraphDecodeResult install_compact_forward_views(const UserContext& context,
std::span<const CompactRow> parents,
const RelationLoadPlan& plan,
std::span<const CompactRow> children,
const EntityRuntimeState& root,
EntityGraphBuilder& graph) {
validate_relation_domain(context, parents, &root, &graph);
validate_relation_domain(context, children, &root, &graph);
auto r = context.memory_resource();
for (const auto& parent : parents) {
const auto* id = parent.get("id");
auto owner = id ? id->try_u64() : std::nullopt;
if (!owner) {
return EntityGraphDecodeResult::err(
r,
EntityError(std::allocator_arg,
Allocator(r),
plan.parent_entity,
"forward relation owner is missing its u64 id"));
}
Vector<CompactRow> rows(r);
const auto* key = parent.get(plan.local_key);
for (const auto& child : children) {
const auto* foreign = child.get(plan.foreign_key);
if ((!key && !foreign) || (key && foreign && *key == *foreign)) {
rows.emplace_back(child);
}
}
auto result = ContextEntityAccess::decode_entity_graph_compact_option(context,
plan.target_entity,
std::move(rows),
root,
graph,
plan.parent_entity,
*owner,
plan.relation_name);
if (!result) {
return result;
}
}
return EntityGraphDecodeResult::ok(r, {});
}
}