| 2185 | } |
| 2186 | |
| 2187 | int cbm_gbuf_merge(cbm_gbuf_t *dst, cbm_gbuf_t *src) { |
| 2188 | if (!dst || !src) { |
| 2189 | return CBM_NOT_FOUND; |
| 2190 | } |
| 2191 | if (src->nodes.count == 0 && src->edges.count == 0) { |
| 2192 | return 0; |
| 2193 | } |
| 2194 | |
| 2195 | /* ID remap for QN-colliding nodes: "src_id" → (int64_t*) dst_id. |
| 2196 | * Only populated when a src node's QN already exists in dst. */ |
| 2197 | CBMHashTable *remap = NULL; |
| 2198 | |
| 2199 | for (int i = 0; i < src->nodes.count; i++) { |
| 2200 | cbm_gbuf_node_t *sn = src->nodes.items[i]; |
| 2201 | if (!sn->qualified_name) { |
| 2202 | continue; |
| 2203 | } |
| 2204 | |
| 2205 | /* Skip nodes deleted from QN index */ |
| 2206 | if (!cbm_ht_get(src->node_by_qn, sn->qualified_name)) { |
| 2207 | continue; |
| 2208 | } |
| 2209 | |
| 2210 | /* The spans src gathered for this node travel with it; they are |
| 2211 | * written once, when dst is exported. */ |
| 2212 | const gb_span_vec_t *spans = |
| 2213 | src->variant_spans ? cbm_ht_get(src->variant_spans, sn->qualified_name) : NULL; |
| 2214 | cbm_gbuf_node_t *existing = cbm_ht_get(dst->node_by_qn, sn->qualified_name); |
| 2215 | if (existing) { |
| 2216 | merge_update_existing(dst, existing, sn, spans, &remap); |
| 2217 | } else { |
| 2218 | merge_copy_new_node(dst, sn); |
| 2219 | gb_adopt_spans(dst, cbm_ht_get(dst->node_by_qn, sn->qualified_name), spans); |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | /* Merge edges with optional ID remapping */ |
| 2224 | merge_remap_edges(dst, src, remap); |
| 2225 | |
| 2226 | if (remap) { |
| 2227 | cbm_ht_foreach(remap, free_remap_entry, NULL); |
| 2228 | cbm_ht_free(remap); |
| 2229 | } |
| 2230 | |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | /* ── Dump / Flush ────────────────────────────────────────────────── */ |
| 2235 |
no test coverage detected