| 9425 | } |
| 9426 | |
| 9427 | static int arch_packages(cbm_store_t *s, const char *project, const char *path, |
| 9428 | cbm_architecture_info_t *out) { |
| 9429 | char norm[CBM_SZ_512]; |
| 9430 | char like[CBM_SZ_512]; |
| 9431 | bool scoped = arch_path_prepare(path, norm, sizeof(norm), like, sizeof(like)); |
| 9432 | char sqlbuf[ST_SQL_BUF]; |
| 9433 | /* GROUP BY +n.name, not n.name: with a bare column the planner walks |
| 9434 | * idx_nodes_name over EVERY node of the project to get groups in name order, |
| 9435 | * with a table lookup per node to test the label (6.3 s on the 8.5M-node |
| 9436 | * kernel graph, for 5 Package nodes). The unary plus keeps the value and its |
| 9437 | * BINARY collation but not the index, so the rows come from the |
| 9438 | * idx_nodes_label seek. The ORDER BY sorter keeps tied counts in group |
| 9439 | * emission order, which was name order from the index; a sorted GROUP BY |
| 9440 | * follows the ORDER BY direction instead, so the name tie-break is explicit. */ |
| 9441 | const char *base = "SELECT n.name, COUNT(*) as cnt FROM nodes n " |
| 9442 | "WHERE n.project=?1 AND n.label='Package'"; |
| 9443 | if (scoped) { |
| 9444 | snprintf(sqlbuf, sizeof(sqlbuf), |
| 9445 | "%s AND (n.file_path = ?2 OR n.file_path LIKE ?3) " |
| 9446 | "GROUP BY +n.name ORDER BY cnt DESC, +n.name LIMIT 15", |
| 9447 | base); |
| 9448 | } else { |
| 9449 | snprintf(sqlbuf, sizeof(sqlbuf), "%s GROUP BY +n.name ORDER BY cnt DESC, +n.name LIMIT 15", |
| 9450 | base); |
| 9451 | } |
| 9452 | sqlite3_stmt *stmt = NULL; |
| 9453 | if (sqlite3_prepare_v2(s->db, sqlbuf, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) { |
| 9454 | store_set_error_sqlite(s, "arch_packages"); |
| 9455 | return CBM_STORE_ERR; |
| 9456 | } |
| 9457 | bind_text(stmt, SKIP_ONE, project); |
| 9458 | if (scoped) { |
| 9459 | arch_bind_path_scope(stmt, ST_COL_2, ST_COL_3, norm, like); |
| 9460 | } |
| 9461 | |
| 9462 | int cap = ST_INIT_CAP_16; |
| 9463 | int n = 0; |
| 9464 | cbm_package_summary_t *arr = calloc(cap, sizeof(cbm_package_summary_t)); |
| 9465 | int scan_rc27; |
| 9466 | while ((scan_rc27 = sqlite3_step(stmt)) == SQLITE_ROW) { |
| 9467 | if (n >= cap) { |
| 9468 | cap *= ST_GROWTH; |
| 9469 | arr = safe_realloc(arr, cap * sizeof(cbm_package_summary_t)); |
| 9470 | } |
| 9471 | arr[n].name = heap_strdup((const char *)sqlite3_column_text(stmt, 0)); |
| 9472 | arr[n].node_count = sqlite3_column_int(stmt, SKIP_ONE); |
| 9473 | n++; |
| 9474 | } |
| 9475 | if (scan_rc27 != SQLITE_DONE) { /* SCANCHK:27:stmt */ |
| 9476 | store_set_error_sqlite(s, "row scan aborted"); |
| 9477 | sqlite3_finalize(stmt); |
| 9478 | out->packages = arr; |
| 9479 | out->package_count = n; |
| 9480 | return CBM_STORE_ERR; |
| 9481 | } |
| 9482 | sqlite3_finalize(stmt); |
| 9483 | |
| 9484 | /* Fallback: group by QN segment if no Package nodes */ |
no test coverage detected