Skip to content

Re-index a node that is added to an RTree layer again - #495

Merged
Andy2003 merged 2 commits into
masterfrom
fix/idempotent-add-node
Nov 18, 2026
Merged

Andy2003 merged 2 commits into
masterfrom
fix/idempotent-add-node

Conversation

@Andy2003

Copy link
Copy Markdown
Collaborator

Adding a node that was already in an RTree layer created a second RTREE_REFERENCE. Queries then returned the node twice, spatial.getFeatureCount counted it twice, and spatial.removeNode / spatial.updateWKT failed with More than one relationship[RTREE_REFERENCE, INCOMING].

spatial.addNode and spatial.addNodes now re-index a node the layer already holds. Calling them again after changing a node's coordinates moves it in the index; a moved node has to be re-added to every layer that holds it. A re-added node whose envelope still fits its leaf is left untouched. Duplicates within one spatial.addNodes list are indexed once.

Removing or re-adding a node that is in several RTree layers no longer fails with More than one relationship. Duplicate references written by earlier releases are dropped when the node is added again.

Geohash, z-order and Hilbert layers were not affected.

Resolves #65
Resolves #254
Resolves #178
Resolves #229
Resolves #439

Adding an already indexed node created a second RTREE_REFERENCE: queries
returned it twice, the feature count grew, and removing or updating the
node failed with "More than one relationship". The RTree now drops
duplicates from the input and re-inserts a node it already holds only when
its envelope no longer fits its leaf, so a moved node can simply be added
again and an unchanged one costs no writes.
Removing or re-adding a node that is in more than one RTree layer failed
with "More than one relationship[RTREE_REFERENCE, INCOMING]". add, remove
and isNodeIndexed now act on every reference this tree holds to the node,
which also drops duplicate references written by earlier releases when
the node is added again. A moved node must be re-added to each layer.
@Andy2003
Andy2003 requested a lite review from Copilot November 18, 2026 15:16
@Andy2003 Andy2003 self-assigned this Nov 18, 2026
@Andy2003 Andy2003 added fix Bugfix java Pull requests that update java code labels Nov 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human validation is needed because the RTree delete/reinsert changes affect persisted structure and legacy duplicate references, while runtime tests are unavailable to verify tree invariants and cross-layer behavior.

No open issues 🎉

What changed in this PR

Updates RTree indexing so re-adding an existing node avoids duplicate references and can relocate nodes whose geometry changed.

Changes:

  • Deduplicates nodes in bulk additions and reindexes moved or previously duplicated RTree entries.
  • Removes matching references safely when removing nodes and handles membership across multiple layers.
  • Adds regression tests and updates procedure and API documentation.
File Description
server-plugin/​src/​test/​java/​org/​neo4j/​gis/​spatial/​procedures/​SpatialProceduresTest.java Adds regression coverage for repeated additions, moved nodes, duplicate legacy references, multiple layers, and WKT-backed layers.
server-plugin/​src/​main/​java/​org/​neo4j/​gis/​spatial/​rtree/​RTreeIndex.java Implements duplicate detection, reindexing of moved nodes, and safe removal of multiple references.
server-plugin/​src/​main/​java/​org/​neo4j/​gis/​spatial/​procedures/​SpatialProcedures.java Documents repeated-add and moved-node behavior for the spatial add procedures.
docs/​docs/​modules/​ROOT/​partials/​generated/​api/​spatial/​spatial.addNodes.adoc Updates the generated documentation for spatial.addNodes reindexing behavior.
docs/​docs/​modules/​ROOT/​partials/​generated/​api/​spatial/​spatial.addNode.adoc Updates the generated documentation for spatial.addNode reindexing behavior.
docs/​docs/​modules/​ROOT/​partials/​generated/​api/​index.adoc Reflects the updated spatial procedure descriptions in the generated API index.

Review effort: Lite

@github-actions

Copy link
Copy Markdown

Test Results

432 tests  +16   410 ✅ +16   2m 45s ⏱️ -12s
 33 suites ± 0    22 💤 ± 0 
 33 files   ± 0     0 ❌ ± 0 

Results for commit 96d286e. ± Comparison against base commit fb63cc4.

@Andy2003

Copy link
Copy Markdown
Collaborator Author

On the tree invariants: removing a reference runs the unchanged remove logic, only moved into removeReference. The new tests cover the new paths through search results, spatial.getFeatureCount and reference counts:

  • moving a node in one of two layers
  • removing two duplicate references from the same leaf, with the node unchanged and moved
  • re-adding an unchanged node, which must not write anything

A broken leaf bounding box would show up as a missed search hit. So no separate tree invariant test.

@Andy2003
Andy2003 merged commit 3c1272f into master Nov 18, 2026
9 checks passed
@Andy2003
Andy2003 deleted the fix/idempotent-add-node branch November 18, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bugfix java Pull requests that update java code

Projects

None yet

2 participants