| 330 | } |
| 331 | |
| 332 | JSONBuilder::ItemPtr QueryPlan::explainPlan(const ExplainPlanOptions & options) const |
| 333 | { |
| 334 | checkInitialized(); |
| 335 | |
| 336 | struct Frame |
| 337 | { |
| 338 | Node * node = {}; |
| 339 | size_t next_child = 0; |
| 340 | std::unique_ptr<JSONBuilder::JSONMap> node_map = {}; |
| 341 | std::unique_ptr<JSONBuilder::JSONArray> children_array = {}; |
| 342 | }; |
| 343 | |
| 344 | std::stack<Frame> stack; |
| 345 | stack.push(Frame{.node = root}); |
| 346 | |
| 347 | std::unique_ptr<JSONBuilder::JSONMap> tree; |
| 348 | |
| 349 | while (!stack.empty()) |
| 350 | { |
| 351 | auto & frame = stack.top(); |
| 352 | |
| 353 | if (frame.next_child == 0) |
| 354 | { |
| 355 | if (!frame.node->children.empty()) |
| 356 | frame.children_array = std::make_unique<JSONBuilder::JSONArray>(); |
| 357 | |
| 358 | frame.node_map = std::make_unique<JSONBuilder::JSONMap>(); |
| 359 | explainStep(*frame.node->step, *frame.node_map, options); |
| 360 | } |
| 361 | |
| 362 | if (frame.next_child < frame.node->children.size()) |
| 363 | { |
| 364 | stack.push(Frame{frame.node->children[frame.next_child]}); |
| 365 | ++frame.next_child; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | auto child_plans = frame.node->step->getChildPlans(); |
| 370 | |
| 371 | if (!frame.children_array && !child_plans.empty()) |
| 372 | frame.children_array = std::make_unique<JSONBuilder::JSONArray>(); |
| 373 | |
| 374 | for (const auto & child_plan : child_plans) |
| 375 | frame.children_array->add(child_plan->explainPlan(options)); |
| 376 | |
| 377 | if (frame.children_array) |
| 378 | frame.node_map->add("Plans", std::move(frame.children_array)); |
| 379 | |
| 380 | tree.swap(frame.node_map); |
| 381 | stack.pop(); |
| 382 | |
| 383 | if (!stack.empty()) |
| 384 | stack.top().children_array->add(std::move(tree)); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return tree; |
| 389 | } |
no test coverage detected