Repository navigation
feat(export): stream graph.json into the DB push — kill the load-time memory wall - #9
Conversation
… memory wall A --push never needs the graph object: it only iterates nodes, then edges. Yet the CLI json.loads-ed the ENTIRE graph.json and rebuilt a full NetworkX graph before push_to_falkordb/neo4j walked it. On a real 1.87GB graph.json that load was OOM-killed on a 7.6GB box (unit capped 6G RAM + 5G swap) before a single row went out; an earlier run measured the pusher at ~5.3GB peak. The --push paths (falkordb + neo4j) now stream-parse the file with a stdlib-only incremental reader for the known node-link shape (ijson is not a dependency): one fast offset scan, then one row pass per array, each with a fixed 64KiB buffer, feeding batches straight to the existing batched UNWIND writers without constructing NetworkX. Measured on a 30.5MB / 50k-node synthetic file: 0.66MB tracemalloc peak vs 135.4MB for the old path (~204x, ~2% of file size, flat in graph size). Wire parity is pinned by tests against the in-memory push: identical Cypher text, identical row payloads, per-label/relation row order and chunking preserved, nodes fully precede edges (even when "links" physically precedes "nodes" in the file - offsets are scanned first), legacy "edges" key honoured, MERGE upserts idempotent, --batch-size and URI/auth parsing unchanged. Only cross-label batch interleaving differs, which MERGE makes irrelevant. The GRAPHIFY_MAX_GRAPH_BYTES cap still guards the push path - now bounding disk read, not RAM. The no-push cypher.txt route keeps the old in-memory code on purpose. Co-Authored-By: Claude Fable 5 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
graphify/graphify/exporters/graphdb.py
Line 313 in 8a27fcb
For graphify's default directed: false output, the old path rebuilt an nx.Graph, whose G.edges() emits each pair according to node insertion order rather than the stored source/target order. This line now sends the raw endpoints instead, so a stored link such as z -> a can change from the old pushed a -> z to z -> a; rerunning against an existing database then creates the opposite relationship while leaving the previous one in place. Either preserve the old orientation or explicitly migrate/remove the stale relationships before claiming wire parity.
graphify/graphify/exporters/graphdb.py
Line 325 in 8a27fcb
When a graph contains many distinct sanitized labels or relationship types, each group can remain below batch_size, so pending retains one or more rows for every group until EOF. With unique relations per edge, this again holds the entire edge set in memory and can reproduce the OOM that the streaming path is intended to eliminate; impose a global pending-row bound or flush partial buckets as that bound is reached.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The OOM evidence
graphify export falkordb --push --batch-size 500on a real 1.87GB graph.json was OOM-killed during LOADING on a 7.6GB box (unit capped at 6G RAM + 5G swap). An earlier run measured the pusher at ~5.3GB peak. Root cause: the CLIjson.loads-ed the entire file and rebuilt a full NetworkX graph beforepush_to_falkordb/push_to_neo4jever iterated it — but a push never needs the graph object; it only walks nodes, then edges.The fix
For the
--pushpaths (falkordb + neo4j),graph.jsonis now stream-parsed by a stdlib-only incremental reader for the known node-link shape (ijsonis not a dependency, so none was added): one fast offset scan locating the top-level"nodes"and"links"/legacy"edges"arrays (plus themultigraphflag), then one row pass per array — each with a fixed 64KiB buffer — feeding batches straight to the existing batched UNWIND writers. No NetworkX graph is ever constructed on a push.Before / after memory
~204x less. Extrapolated to the production 1.87GB file, the old path's ~4.4x factor reproduces the measured ~5.3GB peak; the streaming path stays at batch scale regardless of file size.
Parity kept (pinned by tests against the old path)
"links"physically precedes"nodes"in the file (offsets are scanned first)--batch-size, URI/auth parsing, upsert (MERGE) semantics, returned counts: unchangedGRAPHIFY_MAX_GRAPH_BYTESstill caps the push path — now guarding disk-read volume, not RAM (noted in code)--pushcypher.txt route keeps the old in-memory code on purposeTests
tests/test_streaming_push.py(12 new): end-state parity vs the in-memory push with fake drivers (rows, order, chunking, Cypher text, counts, communities-sidecar override), nodes-before-edges ordering with a links-first file, legacy"edges"key, empty graph, tracemalloc assertion on a 50k-node file (peak < 8MB and < file/4; measured 0.66MB), malformed-file error tone (library + CLI), CLI streams without touchingnode_link_graph,--passwordstill required for neo4j.Full sweep:
test_streaming_push+test_batched_push+test_extract_cli+test_cli_export+test_falkordb_integration+test_export→ 159 passed, 1 skipped (live-server integration test).ruffandpy_compileclean.🤖 Generated with Claude Code