Per-node query fields, memoized on the graph object. Cached exactly like `_trigram_index` / `_idf_cache`, so a hot reload that swaps in a fresh graph object drops it automatically. The node-count guard keeps a graph that grew after the cache was built from serving stale rows (serve
(G: nx.Graph)
| 577 | |
| 578 | |
| 579 | def _node_fields(G: nx.Graph) -> dict[str, _NodeFields]: |
| 580 | """Per-node query fields, memoized on the graph object. |
| 581 | |
| 582 | Cached exactly like `_trigram_index` / `_idf_cache`, so a hot reload that |
| 583 | swaps in a fresh graph object drops it automatically. The node-count guard |
| 584 | keeps a graph that grew after the cache was built from serving stale rows |
| 585 | (serve never mutates a loaded graph, but tests and embedders can). |
| 586 | """ |
| 587 | cached = G.graph.get("_node_fields_cache") |
| 588 | n = G.number_of_nodes() |
| 589 | if cached is not None and cached[0] == n: |
| 590 | return cached[1] |
| 591 | fields: dict[str, _NodeFields] = {} |
| 592 | for nid, data in G.nodes(data=True): |
| 593 | raw_label = data.get("label") or "" |
| 594 | label = raw_label if isinstance(raw_label, str) else str(raw_label) |
| 595 | norm_label = data.get("norm_label") or _strip_diacritics(label).lower() |
| 596 | raw_source = data.get("source_file") or "" |
| 597 | source_file = raw_source if isinstance(raw_source, str) else str(raw_source) |
| 598 | nid_str = nid if isinstance(nid, str) else str(nid) |
| 599 | nid_lower = nid_str.lower() |
| 600 | attr_norm, attr_tokens = _node_attributes_text(data) |
| 601 | fields[nid] = _NodeFields( |
| 602 | norm_label=norm_label, |
| 603 | bare_label=norm_label.rstrip("()"), |
| 604 | label_tokens=" ".join(_search_tokens(label)), |
| 605 | source=source_file.lower(), |
| 606 | source_tokens=" ".join(_search_tokens(source_file)), |
| 607 | nid_lower=nid_lower, |
| 608 | nid_norm=nid_lower if nid_str.isascii() else _strip_diacritics(nid_str).lower(), |
| 609 | rationale=_node_rationale_text(data), |
| 610 | attr_norm=attr_norm, |
| 611 | attr_tokens=attr_tokens, |
| 612 | label_len=len(label or nid_str), |
| 613 | ) |
| 614 | G.graph["_node_fields_cache"] = (n, fields) |
| 615 | return fields |
| 616 | |
| 617 | |
| 618 | _QUALIFIED_UNSET = object() |
no test coverage detected