Repository navigation
Conversation
# Conflicts: # core/src/main/java/com/graphhopper/GraphHopper.java
# Conflicts: # core/src/main/resources/com/graphhopper/util/zh_TW.txt
|
I updated this to current master. I also extracted a Extracting the |
| * This method calculates the alternative path list using the low level Path objects. | ||
| */ | ||
| public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) { | ||
| return createGraphHopperRouter().route(request, ghRsp); |
There was a problem hiding this comment.
I basically took this code and put it into a new class...
| private final CHPreparationHandler chPreparationHandler; | ||
| private final LMPreparationHandler lmPreparationHandler; |
There was a problem hiding this comment.
Similar to GraphHopper these PreparationHandlers are concerned with both the 'import' and the 'algorithm creation' (when routing), but they are mostly about preparation and I definitely want to get rid of them here. -> They are gone now :)
…e.getCHGraph(String)
…arationHandler dependencies from GHRouter
…tRoutingConfig instead
easbar
left a comment
There was a problem hiding this comment.
I took this a bit further now:
-
CH/LMPreprarationHandlercontained thedisablingAllowedflags, but other than this the preparation handlers were only concerned with preparing the landmarks and CH graphs. So I moved the flags intoRoutingConfig.RoutingConfignow contains all (and only) the properties that can be changed between imports. -
I removed all the setters in
GraphHopperthat only delegated to setters ofRoutingConfig. Instead the same can be done likehopper.getRoutingConfig().setXYZ()now.GraphHoppernow is only concerned with importing, loading and storing stuff. Everything related to routing is delegated toGraphHopperRouterwhich assumes fully loaded data structures. The only things that are still left are the translation map and the path details builder factory (I am not sure yet what to do with these...).
| Map<String, CHGraph> chGraphs = new LinkedHashMap<>(); | ||
| for (CHProfile chProfile : chPreparationHandler.getCHProfiles()) { | ||
| String chGraphName = chPreparationHandler.getPreparation(chProfile.getProfile()).getCHConfig().getName(); | ||
| chGraphs.put(chProfile.getProfile(), ghStorage.getCHGraph(chGraphName)); |
There was a problem hiding this comment.
See my comment about the GraphHopperRouter constructor.
| // todo: these should not be necessary anymore as soon as GraphHopperStorage (or something that replaces) it acts | ||
| // like a 'graph database' | ||
| private final Map<String, CHGraph> chGraphs; | ||
| private final Map<String, LandmarkStorage> landmarks; |
There was a problem hiding this comment.
GraphHopperRouter needs a way to obtain the CH/LM helper structures for a given profile. Once we have it, some kind of 'graph-database' could replace this, but for now I am simply using these maps.
| public final boolean isDisablingAllowed() { | ||
| return disablingAllowed || !isEnabled(); | ||
| } |
There was a problem hiding this comment.
This was a bit weird. The default for LM disabling allowed was false, but still this returned true whenever there were some LM profiles. -> I changed the default for lmDisablingAllowed to true in RouterConfig now. Is this ok?
There was a problem hiding this comment.
Or should we set it to false now? Seems a bit more simple to me to have both set to false :) (We would probably have to adjust this in a few tests, but what about production?)
There was a problem hiding this comment.
Hm I'll leave it as it is for now we can change the default still later if we think its better.
There was a problem hiding this comment.
Yes, this could work too. The (hopefully ;)) only requirement for production is that the behaviour of ch.disable=true does not depend on if there are LM profiles or not (as we have clusters with CH and LM but also with CH and without LM). An additional assumption there is that no one uses lm.disable=true and only ch.disable=true/false.
There was a problem hiding this comment.
Ok I think ch.disable=true does not depend on the existence of LM profiles, but anyway for now it seems easier to not change it :)
| if (!hopper.getCHPreparationHandler().getNodeBasedCHConfigs().isEmpty()) { | ||
| CHConfig chConfig = hopper.getCHPreparationHandler().getNodeBasedCHConfigs().get(0); | ||
| CHGraph lg = g.getCHGraph(chConfig); | ||
| CHGraph lg = g.getCHGraph(chConfig.getName()); |
There was a problem hiding this comment.
I changed these, because getting a CH graph should be done by passing its name, not the entire CHConfig (which I want to get rid of anyway).
| this.chGraph = ghStorage.getCHGraph(chConfig); | ||
| chGraph = ghStorage.getCHGraph(chConfig.getName()); | ||
| if (chGraph == null) | ||
| throw new IllegalArgumentException("There is no CH graph '" + chConfig.getName() + "', existing: " + ghStorage.getCHGraphNames()); |
There was a problem hiding this comment.
Can we throw this inside ghStorage.getCHGraph?
There was a problem hiding this comment.
Good question :) Previously there was an IllegalStateException inside getCHGraph(), but what about the case where we get some profile via the web UI and use this to get the CH graph? In this case we do not want a 500 error, so we would have to catch the IllegalStateException and convert it into a (web api friendly(ier)) IllegalArgumentException, but this very ugly. So we could change this into an IllegalArgumentException instead, but this only means that the concerns of the web layer further propagate into the core (especially if the error message contains web layer stuff). I thought returning null is a cleaner (layer independent) way to communicate that the corresponding CHGraph does not exist. I would even use a (checked) CHGraphDoesNotExistException to make this very explicit, but I know you don't like it and we had this discussion before :) Or we use a dedicated but unchecked exception...
To summarize:
- The
IllegalStateExceptionused before seems very wrong to me, because it does not even allow askingGraphHopperStoragefor a wrongCHGraphname. You have to know it before calling the method, but if this shall turn into something like a 'graph-database' there should be a clean way to find out something does not exist. - Using
IllegalArgumentExceptionis slightly better, especially because we use this quite frequently so far. But what about the error message? Its hard to make this independent from the layer the method is used in. And catching and converting anIllegalArgumentExceptionalso seems wrong. In this case it would be better to use a dedicated exception (does not matter much if its checked or not). - Using
nullleaves it up to the calling code how to handle the exceptional case (this is what I decided to do here)
There was a problem hiding this comment.
Ah. Thanks for these insights. So we fetch getCHGraph to know if a ch graph exists? Couldn't we do this with the profile map or when is it required?
but I know you don't like it and we had this discussion before :)
Haha. Yeah. But every rule can have its exceptions ;)
There was a problem hiding this comment.
So we fetch getCHGraph to know if a ch graph exists?
No, we need the actual graph (here and in other places).
Haha. Yeah. But every rule can have its exceptions ;)
No I think there is no need for this. Its a matter of style and since we aren't using checked exceptions there is no real need to start doing it. Sorry mentioning checked/unchecked was a bit distracting here. This is more about error handling between different parts of the code base. And I guess the question was whether we should use return values like null, or standard exceptions (like IllegalArgument) or dedicated exceptions (like CHGraphDoesNotExist) to indicate such exceptional conditions.
There was a problem hiding this comment.
Using IllegalArgumentException is slightly better, especially because we use this quite frequently so far. But what about the error message? Its hard to make this independent from the layer the method is used in.
Are there currently different exception handling cases for ghStorage.getCHGraph==null where we have different messages?
(Because if there is a bug then the now unchecked cases like in createRouter will throw a NPE)
There was a problem hiding this comment.
Are there currently different exception handling cases for ghStorage.getCHGraph==null where we have different messages?
We don't know what GraphHopperStorage might be used for so we cannot know what a good error message would be. Actually I think we do not want to know this because GraphHopperStorage is not meant to be used for one particular use-case. This is kind of my point here, it should be up to the caller of the method what to do if the CH graph does not exist. In case the CH graph does not exist and we use the result we get an NPE yes, but before we got an IllegalStateException (I do not think either one is better). Using null forces the caller to handle it unless she is sure this case cannot happen (compared to a checked exceptions where we also have to handle it (and the compiler tells us) if we are sure we do not need to). Ok, the NPE will not produce a useful error message, so this would be an advantage of using a dedicated exception.
There was a problem hiding this comment.
like in createRouter
In createRouter there cannot be an NPE, because we use a profile name that we got from GraphHopperStorage (but yes this is hard to see and not so easy to reason about).
There was a problem hiding this comment.
Ah but maybe we are talking about two different things... Have a look at Router#getAlgorithmFactory
CHGraph chGraph = chGraphs.get(profile);
if (chGraph == null)
throw new IllegalArgumentException("Cannot find CH preparation for the requested profile: '" + profile + "'" +
"\nYou can try disabling CH using " + Parameters.CH.DISABLE + "=true" +
"\navailable CH profiles: " + chGraphs.keySet());Here we get a profile name from the user and throw an error if this profile name does not work with CH. The same error message would not make sense in GraphHopperStorage#getCHGraph. But to make this even more complicated: This code no longer even takes the CH graph from GraphHopperStorage (but it did before), because now I have this Map<String, CHGraph> (see the comment in the Router constructor if you wonder why).
So I could also revert the change in getCHGraph and throw an exception there again, but still my point is that it should be possible to find out a CH graph does not exist in a clean way (without catching IllegalStateException).
There was a problem hiding this comment.
But I can see why you want to prevent NPE, so my next suggestion would be an (unchecked) CHGraphDoesNotExistException.
There was a problem hiding this comment.
So I could also revert the change in getCHGraph and throw an exception there again
ok
But I can see why you want to prevent NPE, so my next suggestion would be an (unchecked) CHGraphDoesNotExistException
Let's keep it like it is :) (returning null)
Work towards #1895.
The goal here is extracting everything that is related to processing routing requests from
GraphHopperso it can be used (and tested) standalone. The idea is that we first need to load the GH data (GraphHopperStoragemostly, but alsoLocationIndexandLandmarkStoragewhich are currently not managed byGraphHopperStorage). But this loading has nothing to do with the actual routing. The newGraphHopperRouterhere instead uses the loaded data and only deals with performing the routing. There are a few todos left in the code, but I think you already get the idea.