Repository: graphhopper/graphhopper
Pull request #2196 (2020-11-30 12:46:26)

Minor refactoring for LocationIndexTree
#2185


Patch:
diff --git a/core/src/main/java/com/graphhopper/GraphHopper.java b/core/src/main/java/com/graphhopper/GraphHopper.java
index 2bb510c54..6b02470d0 100644
--- a/core/src/main/java/com/graphhopper/GraphHopper.java
+++ b/core/src/main/java/com/graphhopper/GraphHopper.java
@@ -1071,7 +1071,6 @@ public class GraphHopper implements GraphHopperAPI {
             ensureWriteAccess();
 
             if (closeEarly) {
-                locationIndex.flush();
                 locationIndex.close();
                 ghStorage.flushAndCloseEarly();
             }
diff --git a/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java b/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java
index d507e7793..43e5c2b95 100644
--- a/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java
+++ b/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java
@@ -25,6 +25,7 @@ import com.graphhopper.coll.GHBitSet;
 import com.graphhopper.coll.GHIntHashSet;
 import com.graphhopper.coll.GHTBitSet;
 import com.graphhopper.geohash.SpatialKeyAlgo;
+import com.graphhopper.routing.util.AllEdgesIterator;
 import com.graphhopper.routing.util.EdgeFilter;
 import com.graphhopper.storage.*;
 import com.graphhopper.util.*;
@@ -214,9 +215,9 @@ public class LocationIndexTree implements LocationIndex {
         return bm;
     }
 
-    InMemConstructionIndex getPrepareInMemIndex() {
+    InMemConstructionIndex getPrepareInMemIndex(EdgeFilter edgeFilter) {
         InMemConstructionIndex memIndex = new InMemConstructionIndex(entries[0]);
-        memIndex.prepare();
+        memIndex.prepare(edgeFilter);
         return memIndex;
     }
 
@@ -276,13 +277,17 @@ public class LocationIndexTree implements LocationIndex {
 
     @Override
     public LocationIndex prepareIndex() {
+        return prepareIndex(EdgeFilter.ALL_EDGES);
+    }
+
+    public LocationIndex prepareIndex(EdgeFilter edgeFilter) {
         if (initialized)
             throw new IllegalStateException("Call prepareIndex only once");
 
         StopWatch sw = new StopWatch().start();
         prepareAlgo();
         // in-memory preparation
-        InMemConstructionIndex inMem = getPrepareInMemIndex();
+        InMemConstructionIndex inMem = getPrepareInMemIndex(edgeFilter);
 
         // compact & store to dataAccess
         dataAccess.create(64 * 1024);
@@ -307,9 +312,7 @@ public class LocationIndexTree implements LocationIndex {
     }
 
     int calcChecksum() {
-        // do not include the edges as we could get problem with CHGraph due to shortcuts
-        // ^ graph.getAllEdges().count();
-        return graph.getNodes();
+        return graph.getNodes() ^ graph.getAllEdges().length();
     }
 
     @Override
@@ -841,10 +844,12 @@ public class LocationIndexTree implements LocationIndex {
             root = new InMemTreeEntry(noOfSubEntries);
         }
 
-        void prepare() {
-            final EdgeIterator allIter = graph.getAllEdges();
+        void prepare(EdgeFilter edgeFilter) {
+            AllEdgesIterator allIter = graph.getAllEdges();
             try {
                 while (allIter.next()) {
+                    if (!edgeFilter.accept(allIter))
+                        continue;
                     int nodeA = allIter.getBaseNode();
                     int nodeB = allIter.getAdjNode();
                     double lat1 = nodeAccess.getLatitude(nodeA);


Test patch:
diff --git a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java
index 6ea4bdcb9..55db922a1 100644
--- a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java
+++ b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java
@@ -125,7 +125,7 @@ public class LocationIndexTreeTest extends AbstractLocationIndexTester {
         Graph graph = createTestGraph(encodingManager);
         LocationIndexTree index = createIndexNoPrepare(graph, 50000);
         index.prepareAlgo();
-        LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex();
+        LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex(EdgeFilter.ALL_EDGES);
         assertEquals(IntArrayList.from(new int[]{4, 4}), index.getEntries());
 
         assertEquals(4, inMemIndex.getEntriesOf(0).size());
@@ -172,7 +172,7 @@ public class LocationIndexTreeTest extends AbstractLocationIndexTester {
         Graph graph = createTestGraph2();
         LocationIndexTree index = createIndexNoPrepare(graph, 500);
         index.prepareAlgo();
-        LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex();
+        LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex(EdgeFilter.ALL_EDGES);
         assertEquals(IntArrayList.from(new int[]{4, 4}), index.getEntries());
         assertEquals(3, inMemIndex.getEntriesOf(0).size());
         assertEquals(5, inMemIndex.getEntriesOf(1).size());
@@ -209,9 +209,10 @@ public class LocationIndexTreeTest extends AbstractLocationIndexTester {
 
     @Test
     public void testInMemIndex3() {
-        LocationIndexTree index = createIndexNoPrepare(createTestGraph(encodingManager), 10000);
+        Graph graph = createTestGraph(encodingManager);
+        LocationIndexTree index = createIndexNoPrepare(graph, 10000);
         index.prepareAlgo();
-        LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex();
+        LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex(EdgeFilter.ALL_EDGES);
         assertEquals(IntArrayList.from(new int[]{16, 4, 4}), index.getEntries());
 
         assertEquals(13, inMemIndex.getEntriesOf(0).size());