Full node pass: map every node to a region via its file; count members. * Files not seen in the callable pass join folder/misc regions here. */
| 740 | /* Full node pass: map every node to a region via its file; count members. |
| 741 | * Files not seen in the callable pass join folder/misc regions here. */ |
| 742 | static int rg_map_all_nodes(cbm_store_t *store, const char *project, CBMHashTable *file_ht, |
| 743 | rg_build_t *b) { |
| 744 | struct sqlite3 *db = cbm_store_get_db(store); |
| 745 | sqlite3_stmt *st = NULL; |
| 746 | if (!db || sqlite3_prepare_v2(db, "SELECT id, file_path FROM nodes WHERE project=?1", -1, &st, |
| 747 | NULL) != SQLITE_OK) |
| 748 | return -1; |
| 749 | sqlite3_bind_text(st, 1, project, -1, SQLITE_STATIC); |
| 750 | |
| 751 | /* folder key → region id for late files (folder regions + misc). */ |
| 752 | CBMHashTable *late_ht = cbm_ht_create(128); |
| 753 | for (int r = 0; r < b->region_count; r++) { |
| 754 | /* Folder regions are addressable by their name (the key). Leiden |
| 755 | * region names may collide with folder keys; folder regions were |
| 756 | * created later, so set them last — but only map names that ARE |
| 757 | * folder keys (why starts with "folder group"). */ |
| 758 | if (b->regions[r].why && strncmp(b->regions[r].why, "folder group", 12) == 0) |
| 759 | cbm_ht_set(late_ht, b->regions[r].name, (void *)(intptr_t)(r + 1)); |
| 760 | } |
| 761 | int misc_region = -1; |
| 762 | for (int r = 0; r < b->region_count; r++) |
| 763 | if (b->regions[r].name && strcmp(b->regions[r].name, "misc") == 0) |
| 764 | misc_region = r; |
| 765 | |
| 766 | long long cap = 0, n = 0; |
| 767 | rg_id_region_t *map = NULL; |
| 768 | int file_cap_local = b->file_count; /* interning may grow the arrays */ |
| 769 | while (sqlite3_step(st) == SQLITE_ROW) { |
| 770 | int64_t id = sqlite3_column_int64(st, 0); |
| 771 | const char *path = (const char *)sqlite3_column_text(st, 1); |
| 772 | int region = -1; |
| 773 | if (path && path[0]) { |
| 774 | file_rec_t *rec = cbm_ht_get(file_ht, path); |
| 775 | if (!rec) { |
| 776 | rec = rg_file_intern(file_ht, b, &file_cap_local, path); |
| 777 | if (rec) { |
| 778 | char key[CBM_SZ_256]; |
| 779 | rg_folder_key(path, key, sizeof(key)); |
| 780 | void *slot = cbm_ht_get(late_ht, key); |
| 781 | if (slot) |
| 782 | rec->region = (int)(intptr_t)slot - 1; |
| 783 | else if (misc_region >= 0) |
| 784 | rec->region = misc_region; |
| 785 | if (rec->region >= 0) |
| 786 | b->regions[rec->region].files++; |
| 787 | } |
| 788 | } |
| 789 | if (rec) |
| 790 | region = rec->region; |
| 791 | } |
| 792 | if (n >= cap) { |
| 793 | long long nc = cap ? cap * 2 : 65536; |
| 794 | rg_id_region_t *nm = |
| 795 | cbm_realloc(CBM_MEM_CLASS_OTHER, map, (size_t)nc * sizeof(rg_id_region_t)); |
| 796 | if (!nm) |
| 797 | break; |
| 798 | map = nm; |
| 799 | cap = nc; |
no test coverage detected