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
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,12 @@ public SearchRecords search(Transaction tx, SearchFilter filter) {
return new SearchRecords(layer, searchIndex(tx, filter));
}

@Override
public void add(Transaction tx, Node geomNode) {
index.add(geomNode, getIndexValueFor(tx, geomNode));
}

protected abstract E getIndexValueFor(Transaction tx, Node geomNode);

@Override
public void add(Transaction tx, List<Node> geomNodes) {
for (Node node : geomNodes) {
add(tx, node);
index.add(node, getIndexValueFor(tx, node));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

public interface SpatialIndexWriter extends SpatialIndexReader {

void add(Transaction tx, Node geomNode);
default void add(Transaction tx, Node geomNode) {
add(tx, List.of(geomNode));
}

void add(Transaction tx, List<Node> geomNodes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -170,15 +172,6 @@ public void configure(Map<String, Object> config) {
});
}

@Override
public void add(Transaction tx, Node geomNode) {
// initialize the search with root
Node parent = getIndexRoot(tx);
addBelow(tx, parent, geomNode);
countSaved = false;
totalGeometryCount++;
}

/**
* This method will add the node somewhere below the parent.
*/
Expand Down Expand Up @@ -229,29 +222,34 @@ private void insertIndexNodeOnParent(Transaction tx, Node parent, Node child) {
*/
@Override
public void add(Transaction tx, List<Node> geomNodes) {
Node indexRoot = getIndexRoot(tx);

//If the insertion is large relative to the size of the tree, simply rebuild the whole tree.
if (geomNodes.size() > totalGeometryCount * 0.4) {
List<Node> nodesToAdd = new ArrayList<>(geomNodes.size() + totalGeometryCount);
for (Node n : getAllIndexedNodes(tx)) {
nodesToAdd.add(n);
}
nodesToAdd.addAll(geomNodes);
detachGeometryNodes(tx, false, getIndexRoot(tx), new NullListener());
deleteTreeBelow(getIndexRoot(tx));
buildRtreeFromScratch(tx, getIndexRoot(tx), decodeGeometryNodeEnvelopes(nodesToAdd), 0.7);
if (totalGeometryCount > 0
&& geomNodes.size() > 1
&& geomNodes.size() > totalGeometryCount * 0.4
) {
Set<Node> uniqueNodes
= new LinkedHashSet<>(geomNodes.size() + totalGeometryCount);
getAllIndexedNodes(tx).forEach(uniqueNodes::add);
uniqueNodes.addAll(geomNodes);

List<Node> nodesToAdd = new ArrayList<>(uniqueNodes);
detachGeometryNodes(tx, false, indexRoot, new NullListener());
deleteTreeBelow(indexRoot);
buildRtreeFromScratch(tx, indexRoot, decodeGeometryNodeEnvelopes(nodesToAdd), 0.7);
countSaved = false;
totalGeometryCount = nodesToAdd.size();
monitor.addNbrRebuilt(this, tx);
} else {

List<NodeWithEnvelope> outliers = bulkInsertion(tx, getIndexRoot(tx), getHeight(getIndexRoot(tx), 0),
List<NodeWithEnvelope> outliers = bulkInsertion(tx, indexRoot, getHeight(indexRoot, 0),
decodeGeometryNodeEnvelopes(geomNodes), 0.7);
countSaved = false;
totalGeometryCount = totalGeometryCount + (geomNodes.size() - outliers.size());
for (NodeWithEnvelope n : outliers) {
add(tx, n.node);
addBelow(tx, indexRoot, n.node);
}
totalGeometryCount += geomNodes.size();
countSaved = false;
}
}

Expand Down Expand Up @@ -499,7 +497,11 @@ protected void mergeTwoSubtrees(Transaction tx, NodeWithEnvelope parent, List<No
disconnectedChildren.forEach(t -> t.node.delete());

for (NodeWithEnvelope n : right) {
n.node.getSingleRelationship(RTreeRelationshipTypes.RTREE_CHILD, Direction.INCOMING);
Relationship previousParent = n.node.getSingleRelationship(RTreeRelationshipTypes.RTREE_CHILD,
Direction.INCOMING);
if (previousParent != null) {
previousParent.delete();
}
parent.node.createRelationshipTo(n.node, RTreeRelationshipTypes.RTREE_CHILD);
parent.envelope.expandToInclude(n.envelope);
}
Expand Down Expand Up @@ -681,7 +683,13 @@ public boolean needsToVisit(Envelope indexNodeEnvelope) {

@Override
public void onIndexReference(Node geomNode) {
geomNode.getSingleRelationship(referenceRelationshipType, Direction.INCOMING).delete();
try (var relationships = geomNode.getRelationships(Direction.INCOMING, referenceRelationshipType)) {
for (Relationship rel : relationships) {
if (rel.getStartNode().equals(indexRoot)) {
rel.delete();
}
}
}
if (deleteGeomNodes) {
deleteNode(geomNode);
}
Expand Down