Skip to content

Add bulk_create_has_child store primitive for the batched edge sweep (sync #345) - #1707

Merged
mstomar125 merged 2 commits into
NodeSpaceAI:mainfrom
mstomar125:issue-345-bulk-has-child
Jul 20, 2026
Merged

mstomar125 merged 2 commits into
NodeSpaceAI:mainfrom
mstomar125:issue-345-bulk-has-child

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Foundation for nodespace-sync#345 (batch the reconnect edge sweep). The pull's node apply is already batched (sync #338); the has_child edge apply is still per-row (create_parent_edge → move_node, one write each) and is now the dominant cost of a large cold pull (~133 edges/s in a 33k-edge tenant).

Change

  • SqliteStore::bulk_create_has_child(&[(parent, child, order)]) — insert many has_child edges in ONE transaction. Direction matches move_node/get_children (in_node = parent, out_node = child), order stored at properties.$.order (which get_children sorts by ASC), so a fresh parent's children reproduce the sender's sibling order exactly. Idempotent: a child that already has a parent is skipped (a node has at most one parent) — it never re-parents.
  • NodeService::bulk_create_has_child_edges(&[(parent, child, order)]) — thin wrapper that emits one RelationshipCreated per created edge (matching the per-row path), coalesced by the caller's begin_batch_emit guard.

Intended for the from-scratch (cold) sweep where every parent is fresh, so the sender's order values are the final sibling order. Repositioning / non-fresh parents stay on the per-row create_parent_edge.

Tests

bulk_create_has_child_edges_reproduces_order_and_is_idempotent: attach children out of order (b=1, a=2, c=3) → get_children returns [b,a,c]; a re-run skips already-parented children (0 created, order unchanged). Full nodespace-core lib suite (962) green; clippy + fmt clean; pre-push gate passed.

The sync-side integration (batch the cold rel sweep, per-row fallback) follows in nodespace-sync#345.

🤖 Generated with Claude Code

The reconnect relationship sweep applies has_child edges one at a time
(create_parent_edge → move_node, per-row), which is the dominant cost of a large
cold pull (~133 edges/s in a 33k-edge tenant) once the node apply is batched.

Add the store primitive `SqliteStore::bulk_create_has_child(&[(parent, child, order)])`
— one transaction, idempotent (skips a child that already has a parent, so it never
re-parents), direction `in_node = parent, out_node = child` matching move_node /
get_children, order stored at `properties.$.order` (get_children sorts by it, so a
fresh parent's children reproduce the sender's sibling order). Plus a NodeService
wrapper `bulk_create_has_child_edges` that emits one RelationshipCreated per created
edge (matching the per-row path), coalesced by the caller's begin_batch_emit guard.

This is intended for the from-scratch (cold) sweep where every parent is fresh;
repositioning / non-fresh parents stay on the per-row path. Test proves sibling
order is reproduced and re-runs are idempotent (no re-parent).

🤖 Generated with [Claude Code](https://claude.com/claude-code)
…iew)

Review notes: (1) the wrapper doc claimed RelationshipCreated events are coalesced by
begin_batch_emit — they aren't (relationship events are not node-keyed, so they
broadcast immediately, one per edge, exactly as the per-row create_parent_edge does;
the batching win is the single transaction). Corrected the comment. (2) Added a test
proving the store's read-your-writes skip-check prevents a second parent edge when the
same child appears under two parents within ONE batch (attaches to the first only).

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@mstomar125

Copy link
Copy Markdown
Contributor Author

Adversarial review + resolution

Verified all seven correctness points against move_node/get_children/get_parent, the schema, and the event machinery:

  1. Direction — in_node=parent, out_node=child matches move_node's insert, get_children (JOIN ON out_node=n.id WHERE in_node=?), and get_parent (WHERE out_node=child). No flip.
  2. Idempotency / no second parent — the WHERE out_node=child AND type='has_child' skip-check is correctly BROADER than the UNIQUE(in_node,out_node,type) index (which alone would NOT catch a same-child-different-parent duplicate). Read-your-writes inside the tx makes it hold within a single batch too.
  3. Order fidelity — {"order": f64} sorted by json_extract(...'$.order') ASC; integer-valued f64 compares fine.
  4. Atomicity — one tx, created built in memory and returned only after commit; any error rolls back the whole batch and emits nothing → clean per-row fallback.
  5. FK fallback — in_node/out_node are NOT NULL REFERENCES node(id) with foreign_keys=ON, so a missing endpoint rolls back the batch → Err → per-row fallback (the intended trigger).
  6. Event parity — same RelationshipEvent::new constructor/id-shape/from-to as create_parent_edge; byte-identical events.
  7. Insert-only — skips already-parented children, never deletes/repositions → can't corrupt existing structure.

Addressed the two non-blocking notes (commit): corrected the doc comment (relationship events broadcast immediately, NOT coalesced by begin_batch_emit — matches the per-row path), and added a within-batch idempotency test (same child under two parents → attaches to the first only). APPROVE.

@mstomar125
mstomar125 merged commit 775e08a into NodeSpaceAI:main Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant