Skip to content

Commit deda28e

Browse files
Peterb3nn0
Peter
authored andcommitted
EncodingManager cannot be null anylonger as we need to detect turn support before
1 parent e7aefc0 commit deda28e

File tree

6 files changed

+95
-26
lines changed

6 files changed

+95
-26
lines changed

core/files/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
moving the boolean parameter of GraphHopper.setInMemory into a separate method setStoreOnFlush
1010
renaming of GraphHopper.setCHShortcuts to setCHWeighting, as well as the property prepare.chShortcuts to prepare.chWeighting
1111
jsonp is disabled by default. You need to enable it in the config.properties, see the config-example.properties
12+
EncodingManager cannot be null in GraphHopperStorage since 0.4. If you need to parse EncodingManager configuration from existing graph use EncodingManager.create
1213
no reflection done in EncodingManager which improves portability and makes configuration of encoders
1314
removed dijkstraNativebi as no performance advantage but maintenance disadvantage and similar to oneToManyDijkstra
1415
osmreader.bytesForFlags changed to graph.bytesForFlags

core/src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ public GraphHopper init( CmdArgs args )
546546
private void printInfo()
547547
{
548548
logger.info("version " + Constants.VERSION + "|" + Constants.BUILD_DATE + " (" + Constants.getVersions() + ")");
549-
logger.info("graph " + graph.toString() + ", details:" + graph.toDetailsString());
549+
if (graph != null)
550+
logger.info("graph " + graph.toString() + ", details:" + graph.toDetailsString());
550551
}
551552

552553
/**
@@ -614,9 +615,6 @@ protected DataReader importData() throws IOException
614615
throw new IllegalStateException("Couldn't load from existing folder: " + ghLocation
615616
+ " but also cannot import from OSM file as it wasn't specified!");
616617

617-
if (encodingManager == null)
618-
throw new IllegalStateException("Missing encoding manager");
619-
620618
encodingManager.setEnableInstructions(enableInstructions);
621619
DataReader reader = createReader(graph);
622620
logger.info("using " + graph.toString() + ", memory:" + Helper.getMemInfo());
@@ -683,10 +681,13 @@ public boolean load( String graphHopperFolder )
683681
}
684682
}
685683
}
684+
686685
setGraphHopperLocation(graphHopperFolder);
687686

688-
GHDirectory dir = new GHDirectory(ghLocation, dataAccessType);
687+
if (encodingManager == null)
688+
encodingManager = EncodingManager.create(ghLocation);
689689

690+
GHDirectory dir = new GHDirectory(ghLocation, dataAccessType);
690691
if (chEnabled)
691692
graph = new LevelGraphStorage(dir, encodingManager, hasElevation());
692693
else if (encodingManager.needsTurnCostsSupport())

core/src/main/java/com/graphhopper/routing/util/EncodingManager.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import com.graphhopper.reader.OSMTurnRelation;
3131
import com.graphhopper.reader.OSMTurnRelation.TurnCostTableEntry;
3232
import com.graphhopper.reader.OSMWay;
33+
import com.graphhopper.storage.Directory;
34+
import com.graphhopper.storage.RAMDirectory;
35+
import com.graphhopper.storage.StorableProperties;
3336
import com.graphhopper.util.EdgeIteratorState;
3437
import com.graphhopper.util.Helper;
3538
import java.util.*;
@@ -138,11 +141,12 @@ static List<FlagEncoder> parseEncoderString( String encoderList )
138141
continue;
139142

140143
String entryVal = "";
141-
if(entry.contains("|")) {
144+
if (entry.contains("|"))
145+
{
142146
entryVal = entry;
143-
entry = entry.split("\\|")[0];
147+
entry = entry.split("\\|")[0];
144148
}
145-
149+
146150
AbstractFlagEncoder fe;
147151
if (entry.equals(CAR))
148152
fe = new CarFlagEncoder(entryVal);
@@ -466,4 +470,30 @@ public boolean needsTurnCostsSupport()
466470
}
467471
return false;
468472
}
473+
474+
/**
475+
* Create the EncodingManager from the provided GraphHopper location. Throws an
476+
* IllegalStateException if it fails.
477+
*/
478+
public static EncodingManager create( String ghLoc )
479+
{
480+
Directory dir = new RAMDirectory(ghLoc, true);
481+
StorableProperties properties = new StorableProperties(dir);
482+
if (!properties.loadExisting())
483+
throw new IllegalStateException("Cannot load properties to fetch EncodingManager configuration at: "
484+
+ dir.getLocation());
485+
486+
// check encoding for compatiblity
487+
properties.checkVersions(false);
488+
String acceptStr = properties.get("osmreader.acceptWay");
489+
490+
if (acceptStr.isEmpty())
491+
throw new IllegalStateException("EncodingManager was not configured. And no one was found in the graph: "
492+
+ dir.getLocation());
493+
494+
int bytesForFlags = 4;
495+
if ("8".equals(properties.get("graph.bytesForFlags")))
496+
bytesForFlags = 8;
497+
return new EncodingManager(acceptStr, bytesForFlags);
498+
}
469499
}

core/src/main/java/com/graphhopper/routing/util/RoutingAlgorithmSpecialAreaTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static Collection<Entry<AlgorithmPreparation, LocationIndex>> createAlgos
134134
LocationIndex idxCH = new LocationIndexTreeSC(graphCH, new RAMDirectory()).prepareIndex();
135135
prepare.add(new ME(prepareCH, idxCH));
136136

137-
PrepareContractionHierarchies prepareCHAStar = new PrepareContractionHierarchies(encoder, weighting, edgeBased)
137+
PrepareContractionHierarchies prepareCHAStar = new PrepareContractionHierarchies(encoder, weighting, tMode)
138138
{
139139
@Override
140140
public RoutingAlgorithm createAlgo()

core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ public GraphHopperStorage( Directory dir, EncodingManager encodingManager, boole
9999
public GraphHopperStorage( Directory dir, EncodingManager encodingManager, boolean withElevation,
100100
ExtendedStorage extendedStorage )
101101
{
102-
// here encoding manager can be null e.g. if we want to load existing graph
102+
if (encodingManager == null)
103+
throw new IllegalArgumentException("EncodingManager cannot be null in GraphHopperStorage since 0.4. "
104+
+ "If you need to parse EncodingManager configuration from existing graph use EncodingManager.create");
105+
103106
this.encodingManager = encodingManager;
104107
this.extStorage = extendedStorage;
105108
this.dir = dir;
@@ -1360,17 +1363,10 @@ private static boolean isTestingEnabled()
13601363
public boolean loadExisting()
13611364
{
13621365
checkInit();
1363-
if (edges.loadExisting())
1366+
if (nodes.loadExisting())
13641367
{
1365-
// edges loaded properly so the other storages have to load or the file is corrupt.
1366-
if (!nodes.loadExisting())
1367-
throw new IllegalStateException("cannot load nodes. corrupt file or directory? " + dir);
1368-
1369-
if (!wayGeometry.loadExisting())
1370-
throw new IllegalStateException("cannot load geometry. corrupt file or directory? " + dir);
1371-
1372-
if (!nameIndex.loadExisting())
1373-
throw new IllegalStateException("cannot load name index. corrupt file or directory? " + dir);
1368+
if (!properties.loadExisting())
1369+
throw new IllegalStateException("Cannot load properties. Corrupt file or directory? " + dir);
13741370

13751371
if (!extStorage.loadExisting())
13761372
{
@@ -1410,6 +1406,18 @@ public boolean loadExisting()
14101406
if (!byteOrder.equalsIgnoreCase("" + dir.getByteOrder()))
14111407
throw new IllegalStateException("Configured byteOrder (" + dim + ") is not equal to byteOrder of loaded graph (" + dir.getByteOrder() + ")");
14121408

1409+
if (!edges.loadExisting())
1410+
throw new IllegalStateException("Cannot load nodes. corrupt file or directory? " + dir);
1411+
1412+
if (!wayGeometry.loadExisting())
1413+
throw new IllegalStateException("Cannot load geometry. corrupt file or directory? " + dir);
1414+
1415+
if (!nameIndex.loadExisting())
1416+
throw new IllegalStateException("Cannot load name index. corrupt file or directory? " + dir);
1417+
1418+
if (!extStorage.loadExisting())
1419+
throw new IllegalStateException("Cannot load extended storage. corrupt file or directory? " + dir);
1420+
14131421
// first define header indices of this storage
14141422
initStorage();
14151423

core/src/test/java/com/graphhopper/GraphHopperTest.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.File;
2929
import java.io.IOException;
3030
import java.util.concurrent.CountDownLatch;
31+
import java.util.concurrent.TimeUnit;
3132
import java.util.concurrent.atomic.AtomicReference;
3233
import org.junit.After;
3334
import org.junit.Test;
@@ -98,6 +99,30 @@ public void testLoadOSM()
9899
}
99100
}
100101

102+
@Test
103+
public void testLoadOSMNoCH()
104+
{
105+
GraphHopper gh = new GraphHopper().setStoreOnFlush(true).
106+
setCHEnable(false).
107+
setEncodingManager(new EncodingManager("CAR")).
108+
setGraphHopperLocation(ghLoc).
109+
setOSMFile(testOsm);
110+
gh.importOrLoad();
111+
GHResponse ph = gh.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
112+
assertTrue(ph.isFound());
113+
assertEquals(3, ph.getPoints().getSize());
114+
115+
gh.close();
116+
gh = new GraphHopper().setStoreOnFlush(true).
117+
setCHEnable(false);
118+
assertTrue(gh.load(ghLoc));
119+
ph = gh.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
120+
assertTrue(ph.isFound());
121+
assertEquals(3, ph.getPoints().getSize());
122+
123+
gh.close();
124+
}
125+
101126
@Test
102127
public void testAllowMultipleReadingInstances()
103128
{
@@ -135,7 +160,7 @@ protected DataReader importData() throws IOException
135160
try
136161
{
137162
latch2.countDown();
138-
latch1.await();
163+
latch1.await(3, TimeUnit.SECONDS);
139164
} catch (InterruptedException ex)
140165
{
141166
}
@@ -168,7 +193,7 @@ public void run()
168193
try
169194
{
170195
// let thread reach the CountDownLatch
171-
latch2.await();
196+
latch2.await(3, TimeUnit.SECONDS);
172197
// now importOrLoad should have create a lock which this load call does not like
173198
instance2.load(ghLoc);
174199
assertTrue(false);
@@ -309,10 +334,12 @@ public void testFailsForWrongConfig() throws IOException
309334
}
310335

311336
@Test
312-
public void testNoNPE_ifOnlyLoad()
337+
public void testNoNPE_ifLoadNotSuccessful()
313338
{
314339
// missing import of graph
315-
instance = new GraphHopper().setStoreOnFlush(true);
340+
instance = new GraphHopper().
341+
setStoreOnFlush(true).
342+
setEncodingManager(new EncodingManager("CAR"));
316343
try
317344
{
318345
assertFalse(instance.load(ghLoc));
@@ -352,6 +379,7 @@ public void testFailsForMissingParameters() throws IOException
352379
// missing OSM file to import
353380
instance = new GraphHopper().
354381
setStoreOnFlush(true).
382+
setEncodingManager(new EncodingManager("CAR")).
355383
setGraphHopperLocation(ghLoc);
356384
try
357385
{
@@ -374,12 +402,13 @@ public void testFailsForMissingParameters() throws IOException
374402
assertTrue(false);
375403
} catch (IllegalStateException ex)
376404
{
377-
assertEquals("Missing encoding manager", ex.getMessage());
405+
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Cannot load properties to fetch EncodingManager"));
378406
}
379407

380-
// Import is possible even if no storeOnFlush but missing OSM file
408+
// Import is possible even if no storeOnFlush is specified BUT here we miss the OSM file
381409
instance = new GraphHopper().
382410
setStoreOnFlush(false).
411+
setEncodingManager(new EncodingManager("CAR")).
383412
setGraphHopperLocation(ghLoc);
384413
try
385414
{

0 commit comments

Comments
 (0)