Skip to content

Commit 4dbcdcd

Browse files
committed
去掉了通过迭代器获取所有顶点
原代码用迭代器的目的是不把所有顶点一次性加入内存,但重构树时必须全加入,所以迭代器除了降低代码可读性毫无意义
1 parent e1f1b9e commit 4dbcdcd

File tree

1 file changed

+22
-98
lines changed

1 file changed

+22
-98
lines changed

src/main/java/org/wowtools/neo4j/rtree/spatial/RTreeIndex.java

Lines changed: 22 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*/
2020
package org.wowtools.neo4j.rtree.spatial;
2121

22+
import org.locationtech.jts.geom.Geometry;
23+
import org.locationtech.jts.io.ParseException;
24+
import org.locationtech.jts.io.WKBReader;
2225
import org.neo4j.graphdb.*;
2326
import org.neo4j.graphdb.traversal.Evaluation;
2427
import org.neo4j.graphdb.traversal.Evaluator;
@@ -719,52 +722,29 @@ public void warmUp() {
719722
}
720723
}
721724

722-
public Iterable<Node> getAllIndexInternalNodes(Transaction tx) {
723-
TraversalDescription td = tx.traversalDescription()
724-
.breadthFirst()
725-
.relationships(RTreeRelationshipTypes.RTREE_CHILD, Direction.OUTGOING)
726-
.evaluator(Evaluators.all());
727-
return td.traverse(getIndexRoot(tx)).nodes();
728-
}
729-
730-
731-
public Iterable<Node> getAllIndexedNodes(Transaction tx) {
732-
return new IndexNodeToGeometryNodeIterable(getAllIndexInternalNodes(tx));
733-
}
734-
735-
private class SearchEvaluator implements Evaluator {
736-
private SearchFilter filter;
737725

738-
public SearchEvaluator(SearchFilter filter) {
739-
this.filter = filter;
740-
}
741-
742-
743-
public Evaluation evaluate(Path path) {
744-
Relationship rel = path.lastRelationship();
745-
Node node = path.endNode();
746-
if (rel == null) {
747-
return Evaluation.EXCLUDE_AND_CONTINUE;
748-
} else if (rel.isType(RTreeRelationshipTypes.RTREE_CHILD)) {
749-
boolean shouldContinue;
750-
try (Transaction tx = database.beginTx()) {
751-
shouldContinue = filter.needsToVisit(getIndexNodeEnvelope(node, tx));
726+
public List<Node> getAllIndexedNodes(Transaction tx) {
727+
Node rtreeNode = getIndexRoot(tx);
728+
Deque<Node> stack = new ArrayDeque<>();//辅助遍历的栈
729+
List<Node> list = new LinkedList<>();
730+
stack.push(rtreeNode);
731+
while (!stack.isEmpty()) {
732+
rtreeNode = stack.pop();
733+
if (rtreeNode.hasRelationship(Direction.OUTGOING, Constant.Relationship.RTREE_CHILD)) {
734+
//若有下级索引节点,下级索引节点入栈
735+
for (Relationship relationship : rtreeNode.getRelationships(Direction.OUTGOING, Constant.Relationship.RTREE_CHILD)) {
736+
Node child = relationship.getEndNode();
737+
stack.push(child);
738+
}
739+
} else if (rtreeNode.hasRelationship(Direction.OUTGOING, Constant.Relationship.RTREE_REFERENCE)) {
740+
//若有下级对象节点,返回结果
741+
for (Relationship relationship : rtreeNode.getRelationships(Direction.OUTGOING, Constant.Relationship.RTREE_REFERENCE)) {
742+
Node objNode = relationship.getEndNode();
743+
list.add(objNode);
752744
}
753-
if (shouldContinue) monitor.matchedTreeNode(path.length(), node);
754-
monitor.addCase(shouldContinue ? "Index Matches" : "Index Does NOT Match");
755-
return shouldContinue ?
756-
Evaluation.EXCLUDE_AND_CONTINUE :
757-
Evaluation.EXCLUDE_AND_PRUNE;
758-
} else if (rel.isType(RTreeRelationshipTypes.RTREE_REFERENCE)) {
759-
boolean found = filter.geometryMatches(node);
760-
monitor.addCase(found ? "Geometry Matches" : "Geometry Does NOT Match");
761-
if (found) monitor.setHeight(path.length());
762-
return found ?
763-
Evaluation.INCLUDE_AND_PRUNE :
764-
Evaluation.EXCLUDE_AND_PRUNE;
765745
}
766-
return null;
767746
}
747+
return list;
768748
}
769749

770750

@@ -1452,62 +1432,6 @@ public void onIndexReference(Node geomNode) {
14521432
}
14531433
}
14541434

1455-
/**
1456-
* In order to wrap one iterable or iterator in another that converts
1457-
* the objects from one type to another without loading all into memory,
1458-
* we need to use this ugly java-magic. Man, I miss Ruby right now!
1459-
*
1460-
* @author Craig
1461-
*/
1462-
private class IndexNodeToGeometryNodeIterable implements Iterable<Node> {
1463-
1464-
private Iterator<Node> allIndexNodeIterator;
1465-
1466-
private class GeometryNodeIterator implements Iterator<Node> {
1467-
1468-
private Transaction tx;
1469-
1470-
public GeometryNodeIterator(Transaction tx) {
1471-
this.tx = tx;
1472-
}
1473-
1474-
Iterator<Node> geometryNodeIterator = null;
1475-
1476-
public boolean hasNext() {
1477-
checkGeometryNodeIterator(tx);
1478-
return geometryNodeIterator != null && geometryNodeIterator.hasNext();
1479-
}
1480-
1481-
public Node next() {
1482-
checkGeometryNodeIterator(tx);
1483-
return geometryNodeIterator == null ? null : geometryNodeIterator.next();
1484-
}
1485-
1486-
private void checkGeometryNodeIterator(Transaction tx) {
1487-
TraversalDescription td = tx.traversalDescription()
1488-
.depthFirst()
1489-
.relationships(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.OUTGOING)
1490-
.evaluator(Evaluators.excludeStartPosition())
1491-
.evaluator(Evaluators.toDepth(1));
1492-
while ((geometryNodeIterator == null || !geometryNodeIterator.hasNext()) &&
1493-
allIndexNodeIterator.hasNext()) {
1494-
geometryNodeIterator = td.traverse(allIndexNodeIterator.next()).nodes().iterator();
1495-
}
1496-
}
1497-
1498-
public void remove() {
1499-
}
1500-
}
1501-
1502-
public IndexNodeToGeometryNodeIterable(Iterable<Node> allIndexNodes) {
1503-
this.allIndexNodeIterator = allIndexNodes.iterator();
1504-
}
1505-
1506-
public Iterator<Node> iterator() {
1507-
return new GeometryNodeIterator(database.beginTx());
1508-
}
1509-
}
1510-
15111435
private class IndexNodeAreaComparator implements Comparator<NodeWithEnvelope> {
15121436

15131437

0 commit comments

Comments
 (0)