Refac: Avoid duplicate edge processing in adelete_by_doc_id

This commit is contained in:
yangdx 2025-06-23 18:39:36 +08:00
parent dffe659388
commit a215939c41

View file

@ -1775,12 +1775,21 @@ class LightRAG:
) )
if node_edges: if node_edges:
for src, tgt in node_edges: for src, tgt in node_edges:
# To avoid processing the same edge twice in an undirected graph
if (
(tgt, src) in relationships_to_delete
or (tgt, src) in relationships_to_rebuild
):
continue
edge_data = await self.chunk_entity_relation_graph.get_edge( edge_data = await self.chunk_entity_relation_graph.get_edge(
src, tgt src, tgt
) )
if edge_data and "source_id" in edge_data: if edge_data and "source_id" in edge_data:
# Split source_id using GRAPH_FIELD_SEP # Split source_id using GRAPH_FIELD_SEP
sources = set(edge_data["source_id"].split(GRAPH_FIELD_SEP)) sources = set(
edge_data["source_id"].split(GRAPH_FIELD_SEP)
)
remaining_sources = sources - chunk_ids remaining_sources = sources - chunk_ids
if not remaining_sources: if not remaining_sources:
@ -1790,7 +1799,9 @@ class LightRAG:
) )
elif remaining_sources != sources: elif remaining_sources != sources:
# Relationship needs to be rebuilt from remaining chunks # Relationship needs to be rebuilt from remaining chunks
relationships_to_rebuild[(src, tgt)] = remaining_sources relationships_to_rebuild[
(src, tgt)
] = remaining_sources
logger.debug( logger.debug(
f"Relationship {src}-{tgt} will be rebuilt from {len(remaining_sources)} remaining chunks" f"Relationship {src}-{tgt} will be rebuilt from {len(remaining_sources)} remaining chunks"
) )