Repository navigation
Read the stored geometry count instead of recounting the RTree on load - #497
Merged
Merged
Conversation
Every layer load traversed the whole tree to count its geometries, which made queries and writes on large layers slow. The count stored on the metadata node is now used; the tree is only counted when it is missing or contradicts the tree (0 for a non-empty tree, or non-zero for an empty one). Removing the last node resets the count to 0. A direct DETACH DELETE of an indexed node no longer gets corrected by that recount. The docs now say to call spatial.removeNode first (#496).
There was a problem hiding this comment.
🟡 Changes recommended
A critical active issue remains in RTreeIndex.isEmpty(tx), which can accept stale positive feature counts after deleting all geometries from a multi-level tree.
1 open issue
What changed in this PR
This pull request improves R-tree layer loading by reusing the persisted geometry count instead of traversing the full tree on every load, while retaining fallback recounting for legacy or clearly inconsistent metadata.
Changes:
- Stores, validates, and maintains
totalGeometryCountduring R-tree operations. - Recounts legacy or inconsistent trees and resets the count when the final indexed node is removed.
- Adds regression and upgrade-compatibility coverage for stored counts and documents the required procedure for modifying indexed nodes.
| File | Description |
|---|---|
| server-plugin/src/test/java/org/neo4j/gis/spatial/LayersTest.java | Adds regression tests for stored geometry counts, legacy fallback recounting, stale counts, and final-node removal behavior. |
| server-plugin/src/main/java/org/neo4j/gis/spatial/rtree/RTreeIndex.java | Loads and maintains persisted geometry counts with fallback recounting for missing or inconsistent metadata. |
| README.md | Documents the requirement to use spatial procedures when modifying indexed nodes. |
| docs/docs/modules/ROOT/pages/overview/introduction.adoc | Adds user-facing guidance about keeping spatial indexes synchronized with graph changes. |
| compat-tests/src/test/java/org/neo4j/spatial/compat/UpgradeCompatibilityTest.java | Verifies stored geometry counts across compatibility and upgrade fixtures. |
Review effort: Lite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Every time an RTree layer was loaded (each
spatial.*procedure call), the whole tree was traversed to count its geometries. On large layers this made every query and write slow.Loading now uses the
totalGeometryCountstored on the metadata node. The tree is only counted when the value is missing (stores written by 5.x releases), or when it is 0 although the root has entries, or non-zero although the root has none.addandremovekeep the stored count current, and removing the last node resets it to 0.Behaviour change
The recount used to correct the count after indexed nodes were deleted directly with
DETACH DELETE. It no longer does. The docs now say to callspatial.removeNodebefore deleting an indexed node. Automatic syncing is tracked in #496.Resolves #313
Resolves #145