Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/modules/ROOT/partials/generated/api/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ Adds a new native point layer with z-order curve based index, returns the layer
|label:procedure[]
|xref:api/spatial/spatial.addNode.adoc[spatial.addNode icon:book[]]

Adds the given node to the layer, returns the geometry-node
Adds the given node to the layer, returns the geometry-node. Adding a node that is already in the layer re-indexes it, e.g. after its location changed. Re-add a moved node to every layer that holds it
|label:procedure[]
|xref:api/spatial/spatial.addNodes.adoc[spatial.addNodes icon:book[]]

Adds the given nodes list to the layer, returns the count
Adds the given nodes list to the layer, returns the count. Adding a node that is already in the layer re-indexes it, e.g. after its location changed. Re-add a moved node to every layer that holds it
|label:procedure[]
|xref:api/spatial/spatial.addPointLayer.adoc[spatial.addPointLayer icon:book[]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
label:procedure[]

[.emphasis]
Adds the given node to the layer, returns the geometry-node
Adds the given node to the layer, returns the geometry-node. Adding a node that is already in the layer re-indexes it, e.g. after its location changed. Re-add a moved node to every layer that holds it

== Signature

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
label:procedure[]

[.emphasis]
Adds the given nodes list to the layer, returns the count
Adds the given nodes list to the layer, returns the count. Adding a node that is already in the layer re-indexes it, e.g. after its location changed. Re-add a moved node to every layer that holds it

== Signature

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ public void removeLayer(@Name(value = "name", description = DOC_LAYER_NAME) Stri
}

@Procedure(value = "spatial.addNode", mode = WRITE)
@Description("Adds the given node to the layer, returns the geometry-node")
@Description("Adds the given node to the layer, returns the geometry-node. Adding a node that is already in the layer re-indexes it, e.g. after its location changed. Re-add a moved node to every layer that holds it")
public Stream<NodeResult> addNodeToLayer(
@Name(value = "layerName", description = DOC_LAYER_NAME) String name,
@Name(value = "node", description = "the node to be added to the index") Node node) {
Expand All @@ -740,7 +740,7 @@ public Stream<NodeResult> addNodeToLayer(
}

@Procedure(value = "spatial.addNodes", mode = WRITE)
@Description("Adds the given nodes list to the layer, returns the count")
@Description("Adds the given nodes list to the layer, returns the count. Adding a node that is already in the layer re-indexes it, e.g. after its location changed. Re-add a moved node to every layer that holds it")
public Stream<CountResult> addNodesToLayer(
@Name(value = "layerName", description = DOC_LAYER_NAME) String name,
@Name(value = "nodes", description = "the nodes to be added to the index") List<Node> nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,23 @@ private void insertIndexNodeOnParent(Transaction tx, Node parent, Node child) {
* In practice, it should be fine for most uses.
*/
@Override
public void add(Transaction tx, List<Node> geomNodes) {
public void add(Transaction tx, List<Node> nodes) {
checkWritable();
List<Node> geomNodes = new ArrayList<>();
for (Node geomNode : new LinkedHashSet<>(nodes)) {
List<Relationship> references = findReferencesInThisIndex(tx, geomNode);
if (references.isEmpty()) {
geomNodes.add(geomNode);
} else if (references.size() > 1 || !getIndexNodeEnvelope(references.get(0).getStartNode())
.covers(envelopeDecoder.decodeEnvelope(geomNode))) {
// moved out of its leaf, or referenced twice by older versions: re-insert with a single reference
references.forEach(this::removeReference);
geomNodes.add(geomNode);
}
}
if (geomNodes.isEmpty()) {
return;
}
Node indexRoot = getIndexRoot(tx);

//If the insertion is large relative to the size of the tree, simply rebuild the whole tree.
Expand Down Expand Up @@ -645,41 +660,38 @@ public void remove(Transaction tx, String geomNodeId, boolean deleteGeomNode, bo
throw nfe;
}
}
if (geomNode != null && isGeometryNodeIndexed(geomNode)) {

Node indexNode = findLeafContainingGeometryNode(geomNode);

// be sure geomNode is inside this RTree
if (isIndexNodeInThisIndex(tx, indexNode)) {
if (geomNode == null) {
return;
}
List<Relationship> references = findReferencesInThisIndex(tx, geomNode);
if (!references.isEmpty()) {
references.forEach(this::removeReference);
if (deleteGeomNode) {
deleteNode(geomNode);
}
} else if (throwExceptionIfNotFound) {
throw new RuntimeException(isGeometryNodeIndexed(geomNode)
? "GeometryNode not indexed in this RTree: " + geomNodeId
: "GeometryNode not indexed with an RTree: " + geomNodeId);
}
}

// remove the entry
final Relationship geometryRtreeReference = geomNode.getSingleRelationship(
referenceRelationshipType, Direction.INCOMING);
if (geometryRtreeReference != null) {
geometryRtreeReference.delete();
}
if (deleteGeomNode) {
deleteNode(geomNode);
}
private void removeReference(Relationship reference) {
Node indexNode = reference.getStartNode();
reference.delete();

// reorganize the tree if needed
if (countChildren(indexNode, referenceRelationshipType) == 0) {
indexNode = deleteEmptyTreeNodes(indexNode, referenceRelationshipType);
adjustParentBoundingBox(indexNode, RTreeRelationshipTypes.RTREE_CHILD);
} else {
adjustParentBoundingBox(indexNode, referenceRelationshipType);
}
// reorganize the tree if needed
if (countChildren(indexNode, referenceRelationshipType) == 0) {
indexNode = deleteEmptyTreeNodes(indexNode, referenceRelationshipType);
adjustParentBoundingBox(indexNode, RTreeRelationshipTypes.RTREE_CHILD);
} else {
adjustParentBoundingBox(indexNode, referenceRelationshipType);
}

adjustPathBoundingBox(indexNode);
adjustPathBoundingBox(indexNode);

countSaved = false;
totalGeometryCount--;
} else if (throwExceptionIfNotFound) {
throw new RuntimeException("GeometryNode not indexed in this RTree: " + geomNodeId);
}
} else if (throwExceptionIfNotFound) {
throw new RuntimeException("GeometryNode not indexed with an RTree: " + geomNodeId);
}
countSaved = false;
totalGeometryCount--;
}

private static Node deleteEmptyTreeNodes(Node indexNode, RelationshipType relType) {
Expand Down Expand Up @@ -783,8 +795,7 @@ public boolean isEmpty(Transaction tx) {
public boolean isNodeIndexed(Transaction tx, String geomNodeId) {
Node geomNode = tx.getNodeByElementId(geomNodeId);
// be sure geomNode is inside this RTree
return geomNode != null && isGeometryNodeIndexed(geomNode) && isIndexNodeInThisIndex(tx,
findLeafContainingGeometryNode(geomNode));
return geomNode != null && !findReferencesInThisIndex(tx, geomNode).isEmpty();
}

public void warmUp(Transaction tx) {
Expand Down Expand Up @@ -1491,13 +1502,21 @@ private void deleteRecursivelySubtree(Node node, Relationship incoming) {
node.delete();
}

protected boolean isGeometryNodeIndexed(Node geomNode) {
return geomNode.hasRelationship(Direction.INCOMING, referenceRelationshipType);
// a node may also be referenced by other RTrees using the same relationship type
private List<Relationship> findReferencesInThisIndex(Transaction tx, Node geomNode) {
List<Relationship> references = new ArrayList<>();
try (var relationships = geomNode.getRelationships(Direction.INCOMING, referenceRelationshipType)) {
for (Relationship rel : relationships) {
if (isIndexNodeInThisIndex(tx, rel.getStartNode())) {
references.add(rel);
}
}
}
return references;
}

protected Node findLeafContainingGeometryNode(Node geomNode) {
return geomNode.getSingleRelationship(referenceRelationshipType, Direction.INCOMING)
.getStartNode();
protected boolean isGeometryNodeIndexed(Node geomNode) {
return geomNode.hasRelationship(Direction.INCOMING, referenceRelationshipType);
}

protected boolean isIndexNodeInThisIndex(Transaction tx, Node indexNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -272,6 +273,142 @@ WHERE size(labels(g)) = 0 AND COUNT { (g)--() } = 1
testCall(db, "MATCH (n) RETURN count(n) AS count", r -> assertEquals(2L, r.get("count")));
}

// https://github.com/neo4j-contrib/spatial/issues/254, #65, #178
@ParameterizedTest
@ValueSource(strings = {
"spatial.addPointLayer('geom')",
"spatial.addPointLayerGeohash('geom')",
"spatial.addPointLayerZOrder('geom')",
"spatial.addPointLayerHilbert('geom')",
"spatial.addWKTLayer('geom','wkt')",
})
public void add_node_twice_indexes_it_once(String createLayer) {
execute("CALL " + createLayer);
execute("CREATE (:Loc {name:'a', latitude:60.1, longitude:15.2, wkt:'POINT(15.2 60.1)'})");
execute("MATCH (n:Loc) CALL spatial.addNode('geom', n) YIELD node RETURN node");
execute("MATCH (n:Loc) CALL spatial.addNode('geom', n) YIELD node RETURN node");
assertIndexedOnce();
execute("MATCH (n:Loc) CALL spatial.addNodes('geom', [n]) YIELD count RETURN count");
assertIndexedOnce();
execute("MATCH (n:Loc) CALL spatial.addNodes('geom', [n, n]) YIELD count RETURN count");
assertIndexedOnce();
execute("MATCH (n:Loc) CALL spatial.removeNode('geom', n) YIELD nodeId RETURN nodeId");
testCall(db, "CALL spatial.getFeatureCount('geom') YIELD count", r -> assertEquals(0L, r.get("count")));
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:15.2,lat:60.1},10)", null, 0);
}

// https://github.com/neo4j-contrib/spatial/issues/254: both adds in one transaction
@Test
public void add_node_twice_in_one_transaction_indexes_it_once() {
execute("CALL spatial.addPointLayer('geom')");
execute("""
CREATE (n:Loc {name:'a', latitude:60.1, longitude:15.2})
WITH n CALL spatial.addNode('geom', n) YIELD node
WITH node CALL spatial.addNode('geom', node) YIELD node AS again
RETURN again""");
assertIndexedOnce();
}

// an unchanged node added again keeps its reference: no remove and re-insert
@Test
public void add_unchanged_node_again_leaves_the_tree_untouched() {
execute("CALL spatial.addPointLayer('geom')");
execute("""
CREATE (n:Loc {latitude:60.1, longitude:15.2})
WITH n CALL spatial.addNode('geom', n) YIELD node RETURN node""");
String refId = "MATCH (:Loc)<-[r:RTREE_REFERENCE]-() RETURN elementId(r) AS id";
String[] before = new String[1];
testCall(db, refId, r -> before[0] = (String) r.get("id"));
execute("MATCH (n:Loc) CALL spatial.addNode('geom', n) YIELD node RETURN node");
testCall(db, refId, r -> assertEquals(before[0], r.get("id")));
}

// https://github.com/neo4j-contrib/spatial/issues/229
@ParameterizedTest
@ValueSource(strings = {
"spatial.addPointLayer('geom')",
"spatial.addPointLayerGeohash('geom')",
"spatial.addPointLayerZOrder('geom')",
"spatial.addPointLayerHilbert('geom')",
"spatial.addWKTLayer('geom','wkt')",
})
public void add_moved_node_again_updates_its_location(String createLayer) {
execute("CALL " + createLayer);
execute("""
CREATE (n:Loc {name:'a', latitude:60.1, longitude:15.2, wkt:'POINT(15.2 60.1)'})
WITH n CALL spatial.addNode('geom', n) YIELD node RETURN node""");
execute("""
MATCH (n:Loc) SET n.latitude = 10.1, n.longitude = 5.2, n.wkt = 'POINT(5.2 10.1)'
WITH n CALL spatial.addNode('geom', n) YIELD node RETURN node""");
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:15.2,lat:60.1},10)", null, 0);
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:5.2,lat:10.1},10)", null, 1);
testCall(db, "CALL spatial.getFeatureCount('geom') YIELD count", r -> assertEquals(1L, r.get("count")));
}

// the node carries an RTREE_REFERENCE from each layer
@Test
public void add_moved_node_again_to_one_of_two_layers() {
execute("CALL spatial.addPointLayer('geom')");
execute("CALL spatial.addPointLayer('other')");
execute("""
CREATE (n:Loc {latitude:60.1, longitude:15.2})
WITH n CALL spatial.addNode('geom', n) YIELD node
WITH n CALL spatial.addNode('other', n) YIELD node RETURN node""");
execute("""
MATCH (n:Loc) SET n.latitude = 10.1, n.longitude = 5.2
WITH n CALL spatial.addNode('geom', n) YIELD node RETURN node""");
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:15.2,lat:60.1},10)", null, 0);
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:5.2,lat:10.1},10)", null, 1);
testCall(db, "CALL spatial.getFeatureCount('other') YIELD count", r -> assertEquals(1L, r.get("count")));
execute("MATCH (n:Loc) CALL spatial.removeNode('other', n) YIELD nodeId RETURN nodeId");
testCall(db, "CALL spatial.getFeatureCount('other') YIELD count", r -> assertEquals(0L, r.get("count")));
testCall(db, "CALL spatial.getFeatureCount('geom') YIELD count", r -> assertEquals(1L, r.get("count")));
}

// https://github.com/neo4j-contrib/spatial/issues/439
@Test
public void remove_node_from_one_of_two_wkt_layers() {
execute("CALL spatial.addWKTLayer('Layer1', 'area')");
execute("CALL spatial.addWKTLayer('Layer2', 'area')");
execute("""
CREATE (site:Site {area: 'POLYGON((-63.183 -17.784, -63.181 -17.784, -63.181 -17.7825, -63.183 -17.7825, -63.183 -17.784))'})
WITH site CALL spatial.addNode('Layer1', site) YIELD node
WITH site CALL spatial.addNode('Layer2', site) YIELD node RETURN node""");
execute("MATCH (site:Site) CALL spatial.removeNode('Layer1', site) YIELD nodeId RETURN nodeId");
String point = "{lon:-63.182, lat:-17.783}";
testCallCount(db, "CALL spatial.intersects('Layer1', " + point + ")", null, 0);
testCallCount(db, "CALL spatial.intersects('Layer2', " + point + ")", null, 1);
execute("MATCH (site:Site) CALL spatial.removeNode('Layer2', site) YIELD nodeId RETURN nodeId");
testCall(db, "MATCH (site:Site) RETURN COUNT { (site)<-[:RTREE_REFERENCE]-() } AS refs",
r -> assertEquals(0L, r.get("refs")));
}

// older versions added a second reference on each repeated add
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void add_node_again_drops_duplicate_reference_of_older_versions(boolean moved) {
execute("CALL spatial.addPointLayer('geom')");
execute("""
CREATE (n:Loc {latitude:60.1, longitude:15.2})
WITH n CALL spatial.addNode('geom', n) YIELD node RETURN node""");
execute("MATCH (leaf)-[:RTREE_REFERENCE]->(n:Loc) CREATE (leaf)-[:RTREE_REFERENCE]->(n)");
if (moved) {
execute("MATCH (n:Loc) SET n.latitude = 10.1, n.longitude = 5.2");
}
execute("MATCH (n:Loc) CALL spatial.addNode('geom', n) YIELD node RETURN node");
testCall(db, "MATCH (n:Loc) RETURN COUNT { (n)<-[:RTREE_REFERENCE]-() } AS refs",
r -> assertEquals(1L, r.get("refs")));
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:15.2,lat:60.1},10)", null, moved ? 0 : 1);
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:5.2,lat:10.1},10)", null, moved ? 1 : 0);
}

private void assertIndexedOnce() {
testCall(db, "MATCH (n:Loc) RETURN COUNT { (n)<-[:RTREE_REFERENCE]-() } AS refs", r -> assertThat(
(Long) r.get("refs"), lessThanOrEqualTo(1L)));
testCall(db, "CALL spatial.getFeatureCount('geom') YIELD count", r -> assertEquals(1L, r.get("count")));
testCallCount(db, "CALL spatial.withinDistance('geom',{lon:15.2,lat:60.1},10)", null, 1);
}

// This tests issue https://github.com/neo4j-contrib/spatial/issues/298
@ParameterizedTest
@ValueSource(strings = {"addPointLayer", "addPointLayerGeohash"})
Expand Down
Loading