Files
wehub-resource-sync c889a57b6b
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Waiting to run
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Waiting to run
Weighted Edges Tests / Code Quality for Weighted Edges (push) Waiting to run
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Waiting to run
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Waiting to run
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Waiting to run
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Waiting to run
Weighted Edges Tests / Test Weighted Edges Examples (push) Waiting to run
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

60 lines
2.6 KiB
Python

"""Group and apply distributed graph writes while preserving graph provenance.
Distributed graph writes arrive on ``add_nodes_and_edges_queue`` as
``(nodes, edges, source_ref_key, pipeline_run_id)`` items. The provenance stamp
(``source_ref_key`` + ``pipeline_run_id``) is per data item, so batches from
different items must not be merged into one write — a single fold key applied to
a merged batch would mis-attribute the other items' artifacts.
So the worker groups accumulated items by their provenance key and folds each
group independently, but still writes ALL nodes before ANY edge (across groups)
so a cross-item edge always finds its endpoint nodes already present. Items
without provenance (``source_ref_key is None`` — non-provenance backends or
non-attributed writes) group under ``(None, None)`` and are written unfolded,
exactly as before.
This module is import-light on purpose (no Modal) so the grouping/ordering logic
is unit-testable without the worker's Modal runtime.
"""
from typing import Awaitable, Callable, Dict, List, Optional, Tuple
# (source_ref_key, pipeline_run_id) -> (nodes, edges)
ProvenanceKey = Tuple[Optional[str], Optional[str]]
GraphWriteGroups = Dict[ProvenanceKey, Tuple[List, List]]
def group_graph_writes(items) -> GraphWriteGroups:
"""Group ``(nodes, edges, source_ref_key, pipeline_run_id)`` items by provenance key.
Insertion order is preserved (dict keeps first-seen order) so writes stay
deterministic across a batch.
"""
groups: GraphWriteGroups = {}
for nodes, edges, source_ref_key, pipeline_run_id in items:
key = (source_ref_key, pipeline_run_id)
if key not in groups:
groups[key] = ([], [])
groups[key][0].extend(nodes)
groups[key][1].extend(edges)
return groups
async def apply_grouped_graph_writes(
groups: GraphWriteGroups,
add_nodes: Callable[[List, Optional[str], Optional[str]], Awaitable[None]],
add_edges: Callable[[List, Optional[str], Optional[str]], Awaitable[None]],
) -> None:
"""Write every group's nodes, then every group's edges (nodes-before-edges).
``add_nodes`` / ``add_edges`` are async callables taking
``(batch, source_ref_key, pipeline_run_id)`` — the worker supplies its
deadlock-retrying, engine-bound writers.
"""
for (source_ref_key, pipeline_run_id), (nodes, _edges) in groups.items():
if nodes:
await add_nodes(nodes, source_ref_key, pipeline_run_id)
for (source_ref_key, pipeline_run_id), (_nodes, edges) in groups.items():
if edges:
await add_edges(edges, source_ref_key, pipeline_run_id)