Skip to content

Commit

Permalink
Allow adding CHGraphs to GraphHopperStorage after creation
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Dec 13, 2019
1 parent fa175ff commit 402028f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
5 changes: 4 additions & 1 deletion core/src/main/java/com/graphhopper/GraphHopper.java
Expand Up @@ -750,6 +750,8 @@ public boolean load(String graphHopperFolder) {
if (lmFactoryDecorator.isEnabled())
initLMAlgoFactoryDecorator();

ghStorage = new GraphHopperStorage(dir, encodingManager, hasElevation(), encodingManager.needsTurnCostsSupport(), defaultSegmentSize);

List<CHProfile> chProfiles;
if (chFactoryDecorator.isEnabled()) {
initCHAlgoFactoryDecorator();
Expand All @@ -758,7 +760,8 @@ public boolean load(String graphHopperFolder) {
chProfiles = Collections.emptyList();
}

ghStorage = new GraphHopperStorage(chProfiles, dir, encodingManager, hasElevation(), encodingManager.needsTurnCostsSupport(), defaultSegmentSize);
ghStorage.addCHGraphs(chProfiles);


if (!new File(graphHopperFolder).exists())
return false;
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/com/graphhopper/storage/GraphBuilder.java
Expand Up @@ -103,7 +103,9 @@ public GraphBuilder setSegmentSize(int segmentSize) {
* {@link #create} directly.
*/
public GraphHopperStorage build() {
return new GraphHopperStorage(chProfiles, dir, encodingManager, elevation, turnCosts, segmentSize);
GraphHopperStorage ghStorage = new GraphHopperStorage(dir, encodingManager, elevation, turnCosts, segmentSize);
ghStorage.addCHGraphs(chProfiles);
return ghStorage;
}

/**
Expand Down
40 changes: 21 additions & 19 deletions core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java
Expand Up @@ -24,7 +24,10 @@
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.shapes.BBox;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* This class manages all storage related methods and delegates the calls to the associated graphs.
Expand All @@ -43,34 +46,24 @@ public final class GraphHopperStorage implements GraphStorage, Graph {
private final BaseGraph baseGraph;
// same flush order etc
private final Collection<CHGraphImpl> chGraphs;
private final int segmentSize;

public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation) {
this(dir, encodingManager, withElevation, false);
}

public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts) {
this(Collections.<CHProfile>emptyList(), dir, encodingManager, withElevation, withTurnCosts);
this(dir, encodingManager, withElevation, withTurnCosts, -1);
}

public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingManager encodingManager, boolean withElevation) {
this(chProfiles, dir, encodingManager, withElevation, false);
}

public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts) {
this(chProfiles, dir, encodingManager, withElevation, withTurnCosts, -1);
}

public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts, int segmentSize) {
public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts, int segmentSize) {
if (encodingManager == null)
throw new IllegalArgumentException("EncodingManager needs to be non-null since 0.7. Create one using EncodingManager.create or EncodingManager.create(flagEncoderFactory, ghLocation)");

if (chProfiles.size() != new HashSet<>(chProfiles).size()) {
throw new IllegalArgumentException("Given CH profiles contain duplicates, given: " + chProfiles);
}

this.encodingManager = encodingManager;
this.dir = dir;
this.properties = new StorableProperties(dir);
this.segmentSize = segmentSize;
InternalGraphEventListener listener = new InternalGraphEventListener() {
@Override
public void initStorage() {
Expand All @@ -86,11 +79,21 @@ public void freeze() {
}
}
};

baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts, segmentSize);
this.chGraphs = new ArrayList<>(chProfiles.size());
chGraphs = new ArrayList<>();
}

public void addCHGraph(CHProfile chProfile) {
baseGraph.checkNotInitialized();
if (getCHProfiles().contains(chProfile)) {
throw new IllegalArgumentException("For the given CH profile a CHGraph already exists: " + chProfile);
}
chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph, segmentSize));
}

public void addCHGraphs(List<CHProfile> chProfiles) {
for (CHProfile chProfile : chProfiles) {
chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph, segmentSize));
addCHGraph(chProfile);
}
}

Expand Down Expand Up @@ -455,5 +458,4 @@ public boolean isAdjacentToNode(int edge, int node) {
public void flushAndCloseEarly() {
baseGraph.flushAndCloseGeometryAndNameStorage();
}

}

0 comments on commit 402028f

Please sign in to comment.