| 501 | /* ── Public API ───────────────────────────────────────────────── */ |
| 502 | |
| 503 | cbm_layout_result_t *cbm_layout_compute(cbm_store_t *store, const char *project, |
| 504 | cbm_layout_level_t level, const char *center_node, |
| 505 | int radius, int max_nodes) { |
| 506 | if (!store || !project) |
| 507 | return NULL; |
| 508 | max_nodes = clamp_max_nodes(max_nodes); |
| 509 | (void)center_node; |
| 510 | (void)radius; |
| 511 | (void)level; |
| 512 | |
| 513 | /* 1. Query nodes, then hand the structs to the fetch-agnostic core (the |
| 514 | * region scope feeds cbm_layout_from_nodes its own node set). Strings in |
| 515 | * the copied structs stay borrowed from search_out until the core has |
| 516 | * duplicated them into the result. */ |
| 517 | cbm_search_params_t params; |
| 518 | memset(¶ms, 0, sizeof(params)); |
| 519 | params.project = project; |
| 520 | params.limit = max_nodes; |
| 521 | params.min_degree = -1; |
| 522 | params.max_degree = -1; |
| 523 | |
| 524 | cbm_search_output_t search_out; |
| 525 | memset(&search_out, 0, sizeof(search_out)); |
| 526 | if (cbm_store_search(store, ¶ms, &search_out) != CBM_STORE_OK) |
| 527 | return cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(cbm_layout_result_t)); |
| 528 | |
| 529 | int n = search_out.count, total_count = search_out.total; |
| 530 | cbm_node_t *node_arr = NULL; |
| 531 | if (n > 0) { |
| 532 | node_arr = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)n * sizeof(cbm_node_t)); |
| 533 | if (!node_arr) { |
| 534 | cbm_store_search_free(&search_out); |
| 535 | cbm_layout_result_t *r = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*r)); |
| 536 | if (r) |
| 537 | r->total_nodes = total_count; |
| 538 | return r; |
| 539 | } |
| 540 | for (int i = 0; i < n; i++) |
| 541 | node_arr[i] = search_out.results[i].node; |
| 542 | } |
| 543 | cbm_layout_result_t *result = cbm_layout_from_nodes(store, project, node_arr, n, total_count); |
| 544 | cbm_free(CBM_MEM_CLASS_OTHER, node_arr); |
| 545 | cbm_store_search_free(&search_out); |
| 546 | return result; |
| 547 | } |
| 548 | |
| 549 | cbm_layout_result_t *cbm_layout_from_nodes(cbm_store_t *store, const char *project, |
| 550 | const cbm_node_t *nodes, int n, int total_nodes) { |