MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _node_fields

Function _node_fields

graphify/serve.py:579–615  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

577
578
579def _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()

Callers 4

_compute_idfFunction · 0.85
_get_trigram_indexFunction · 0.85
_score_queryFunction · 0.85
_find_node_tiersFunction · 0.85

Calls 7

_node_attributes_textFunction · 0.85
_NodeFieldsClass · 0.85
_search_tokensFunction · 0.85
_node_rationale_textFunction · 0.85
_strip_diacriticsFunction · 0.70
getMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected