Skip to content

Commit

Permalink
Moves segment size setting of graphs into constructor (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Nov 14, 2019
1 parent d3ebf2e commit 9f71833
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 107 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/com/graphhopper/GraphHopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,7 @@ public boolean load(String graphHopperFolder) {
chProfiles = Collections.emptyList();
}

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

if (!new File(graphHopperFolder).exists())
return false;
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/com/graphhopper/storage/BaseGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BaseGraph implements Graph {
private boolean frozen = false;

public BaseGraph(Directory dir, final EncodingManager encodingManager, boolean withElevation,
InternalGraphEventListener listener, boolean withTurnCosts) {
InternalGraphEventListener listener, boolean withTurnCosts, int segmentSize) {
this.dir = dir;
this.encodingManager = encodingManager;
this.intsForFlags = encodingManager.getIntsForFlags();
Expand Down Expand Up @@ -151,6 +151,9 @@ public String toString() {
} else {
turnCostExtension = null;
}
if (segmentSize >= 0) {
setSegmentSize(segmentSize);
}
}

private static boolean isTestingEnabled() {
Expand Down Expand Up @@ -339,7 +342,7 @@ public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirecti
return edge(a, b).setDistance(distance).setFlags(encodingManager.flagsDefault(true, bothDirection));
}

void setSegmentSize(int bytes) {
private void setSegmentSize(int bytes) {
checkInit();
nodes.setSegmentSize(bytes);
edges.setSegmentSize(bytes);
Expand Down
11 changes: 5 additions & 6 deletions core/src/main/java/com/graphhopper/storage/CHGraphImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class CHGraphImpl implements CHGraph, Storable<CHGraph> {
private int shortcutCount = 0;
private boolean isReadyForContraction;

CHGraphImpl(CHProfile chProfile, Directory dir, final BaseGraph baseGraph) {
CHGraphImpl(CHProfile chProfile, Directory dir, final BaseGraph baseGraph, int segmentSize) {
if (chProfile.getWeighting() == null)
throw new IllegalStateException("Weighting for CHGraph cannot be null");
this.chProfile = chProfile;
Expand All @@ -72,6 +72,10 @@ public class CHGraphImpl implements CHGraph, Storable<CHGraph> {
this.nodesCH = dir.find("nodes_ch_" + name, DAType.getPreferredInt(dir.getDefaultType()));
this.shortcuts = dir.find("shortcuts_" + name, DAType.getPreferredInt(dir.getDefaultType()));
this.chEdgeAccess = new CHEdgeAccess(name);
if (segmentSize >= 0) {
nodesCH.setSegmentSize(segmentSize);
shortcuts.setSegmentSize(segmentSize);
}
}

@Override
Expand Down Expand Up @@ -342,11 +346,6 @@ void initStorage() {
nodeCHEntryBytes = N_CH_REF + 4;
}

void setSegmentSize(int bytes) {
nodesCH.setSegmentSize(bytes);
shortcuts.setSegmentSize(bytes);
}

@Override
public CHGraph create(long bytes) {
nodesCH.create(bytes);
Expand Down
85 changes: 33 additions & 52 deletions core/src/main/java/com/graphhopper/storage/GraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,35 @@

import com.graphhopper.routing.util.EncodingManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
* For now this is just a helper class to quickly create a {@link GraphHopperStorage}
* <p>
* Used to build {@link GraphHopperStorage}
*
* @author Peter Karich
* @author easbar
*/
public class GraphBuilder {
private final EncodingManager encodingManager;
private String location;
private boolean mmap;
private boolean store;
private Directory dir = new RAMDirectory();
private boolean elevation;
private boolean turnCosts;
private long byteCapacity = 100;
private List<CHProfile> chProfiles = Collections.emptyList();
private long bytes = 100;
private int segmentSize = -1;
private List<CHProfile> chProfiles = new ArrayList<>();

public static GraphBuilder start(EncodingManager encodingManager) {
return new GraphBuilder(encodingManager);
}

public GraphBuilder(EncodingManager encodingManager) {
this.encodingManager = encodingManager;
this.turnCosts = encodingManager.needsTurnCostsSupport();
}

/**
* This method enables creating CHGraphs with the specified CHProfiles
*/
public GraphBuilder setCHProfiles(List<CHProfile> chProfiles) {
if (chProfiles.size() != new HashSet<>(chProfiles).size()) {
throw new IllegalArgumentException("Given CH profiles contain duplicates, given: " + chProfiles);
}
this.chProfiles = chProfiles;
return this;
}
Expand All @@ -59,23 +56,29 @@ public GraphBuilder setCHProfiles(CHProfile... chProfiles) {
return setCHProfiles(Arrays.asList(chProfiles));
}

public GraphBuilder setLocation(String location) {
this.location = location;
public GraphBuilder setDir(Directory dir) {
this.dir = dir;
return this;
}

public GraphBuilder setStore(boolean store) {
this.store = store;
return this;
public GraphBuilder setMMap(String location) {
return setDir(new MMapDirectory(location));
}

public GraphBuilder setMmap(boolean mmap) {
this.mmap = mmap;
return this;
public GraphBuilder setRAM() {
return setDir(new RAMDirectory());
}

public GraphBuilder setRAM(String location) {
return setDir(new RAMDirectory(location));
}

public GraphBuilder setRAM(String location, boolean store) {
return setDir(new RAMDirectory(location, store));
}

public GraphBuilder setExpectedSize(byte cap) {
this.byteCapacity = cap;
public GraphBuilder setBytes(long bytes) {
this.bytes = bytes;
return this;
}

Expand All @@ -89,15 +92,9 @@ public GraphBuilder withTurnCosts(boolean turnCosts) {
return this;
}

public boolean hasElevation() {
return elevation;
}

/**
* Creates a CHGraph
*/
public CHGraph chGraphCreate(CHProfile chProfile) {
return setCHProfiles(chProfile).create().getCHGraph();
public GraphBuilder setSegmentSize(int segmentSize) {
this.segmentSize = segmentSize;
return this;
}

/**
Expand All @@ -106,29 +103,13 @@ public CHGraph chGraphCreate(CHProfile chProfile) {
* {@link #create} directly.
*/
public GraphHopperStorage build() {
Directory dir = mmap ?
new MMapDirectory(location) :
new RAMDirectory(location, store);

boolean withTurnCosts = encodingManager.needsTurnCostsSupport() || turnCosts;
return new GraphHopperStorage(chProfiles, dir, encodingManager, elevation, withTurnCosts);
return new GraphHopperStorage(chProfiles, dir, encodingManager, elevation, turnCosts, segmentSize);
}

/**
* Default graph is a {@link GraphHopperStorage} with an in memory directory and disabled storing on flush.
*/
public GraphHopperStorage create() {
return build().create(byteCapacity);
}

/**
* @throws IllegalStateException if not loadable.
*/
public GraphHopperStorage load() {
GraphHopperStorage gs = build();
if (!gs.loadExisting()) {
throw new IllegalStateException("Cannot load graph " + location);
}
return gs;
return build().create(bytes);
}
}
26 changes: 11 additions & 15 deletions core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.shapes.BBox;

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

/**
* This class manages all storage related methods and delegates the calls to the associated graphs.
Expand Down Expand Up @@ -60,9 +57,17 @@ public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingMan
}

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) {
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);
Expand All @@ -82,10 +87,10 @@ public void freeze() {
}
};

baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts);
baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts, segmentSize);
this.chGraphs = new ArrayList<>(chProfiles.size());
for (CHProfile chProfile : chProfiles) {
chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph));
chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph, segmentSize));
}
}

Expand Down Expand Up @@ -152,15 +157,6 @@ public Directory getDirectory() {
return dir;
}

@Override
public void setSegmentSize(int bytes) {
baseGraph.setSegmentSize(bytes);

for (CHGraphImpl cg : getAllCHGraphs()) {
cg.setSegmentSize(bytes);
}
}

/**
* After configuring this storage you need to create it explicitly.
*/
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/com/graphhopper/storage/GraphStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public interface GraphStorage extends Storable<GraphStorage> {

EncodingManager getEncodingManager();

void setSegmentSize(int bytes);

String toDetailsString();

StorableProperties getProperties();
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/com/graphhopper/GraphHopperAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void testDoNotInterpolateTwice1645() {
String loc = "./target/issue1645";
Helper.removeDir(new File(loc));
EncodingManager em = new EncodingManager.Builder().add(new OSMRoadEnvironmentParser()).add(new CarFlagEncoder()).build();
GraphHopperStorage graph = new GraphBuilder(em).setLocation(loc).set3D(true).setStore(true).create();
GraphHopperStorage graph = GraphBuilder.start(em).setRAM(loc, true).set3D(true).create();

// we need elevation
NodeAccess na = graph.getNodeAccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ protected GraphHopperStorage createGHStorage() {

abstract GraphHopperStorage createGHStorage(String location, boolean is3D);

protected final GraphHopperStorage newRAMGHStorage() {
return new GraphHopperStorage(new RAMDirectory(), encodingManager, false);
}

@Before
public void setUp() {
Helper.removeDir(new File(locationParent));
Expand Down Expand Up @@ -316,9 +312,7 @@ public void testGetLocations() {
public void testCopyTo() {
graph = createGHStorage();
initExampleGraph(graph);
GraphHopperStorage gs = newRAMGHStorage();
gs.setSegmentSize(8000);
gs.create(10);
GraphHopperStorage gs = GraphBuilder.start(encodingManager).setSegmentSize(8000).setBytes(10).create();
graph.copyTo(gs);
checkExampleGraph(gs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,22 @@ protected CHGraph getGraph(GraphHopperStorage ghStorage) {
}

@Override
public GraphHopperStorage newGHStorage(Directory dir, boolean is3D) {
return newGHStorage(dir, is3D, false);
protected GraphHopperStorage newGHStorage(Directory dir, boolean enabled3D) {
return newGHStorage(dir, enabled3D, -1);
}

@Override
public GraphHopperStorage newGHStorage(Directory dir, boolean is3D, int segmentSize) {
return newGHStorage(dir, is3D, false, segmentSize);
}

private GraphHopperStorage newGHStorage(boolean is3D, boolean forEdgeBasedTraversal) {
return newGHStorage(new RAMDirectory(defaultGraphLoc, true), is3D, forEdgeBasedTraversal).create(defaultSize);
return newGHStorage(new RAMDirectory(defaultGraphLoc, true), is3D, forEdgeBasedTraversal, -1).create(defaultSize);
}

private GraphHopperStorage newGHStorage(Directory dir, boolean is3D, boolean forEdgeBasedTraversal) {
private GraphHopperStorage newGHStorage(Directory dir, boolean is3D, boolean forEdgeBasedTraversal, int segmentSize) {
CHProfile chProfile = new CHProfile(new FastestWeighting(carEncoder), forEdgeBasedTraversal, INFINITE_U_TURN_COSTS);
return new GraphHopperStorage(Collections.singletonList(chProfile), dir, encodingManager, is3D);
return new GraphHopperStorage(Collections.singletonList(chProfile), dir, encodingManager, is3D, false, segmentSize);
}

@Test
Expand All @@ -68,7 +73,7 @@ public void testCannotBeLoadedWithNormalGraphHopperStorageClass() {
graph.flush();
graph.close();

graph = new GraphBuilder(encodingManager).setLocation(defaultGraphLoc).setMmap(false).setStore(true).create();
graph = GraphBuilder.start(encodingManager).setRAM(defaultGraphLoc, true).create();
try {
graph.loadExisting();
fail();
Expand Down Expand Up @@ -521,7 +526,7 @@ public void testLoadingWithWrongWeighting_edge_throws() {

private void testLoadingWithWrongWeighting_throws(boolean edgeBased) {
// we start with one weighting
GraphHopperStorage ghStorage = newGHStorage(new GHDirectory(defaultGraphLoc, DAType.RAM_STORE), false, edgeBased);
GraphHopperStorage ghStorage = newGHStorage(new GHDirectory(defaultGraphLoc, DAType.RAM_STORE), false, edgeBased, -1);
ghStorage.create(defaultSize);
ghStorage.flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void tearDown() {

@Test
public void testStorageProperties() {
graph = new GraphBuilder(encodingManager).setStore(true).setLocation(defaultGraphLoc).create();
graph = new GraphBuilder(encodingManager).setRAM(defaultGraphLoc, true).create();

// 0-1
ReaderWay way_0_1 = new ReaderWay(27l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public void testLoad() {
Helper.removeDir(new File(defaultGraphLoc));
CarFlagEncoder carFlagEncoder = new CarFlagEncoder();
EncodingManager encodingManager = EncodingManager.create(carFlagEncoder);
GraphHopperStorage graph = new GraphBuilder(encodingManager).setStore(true).
setLocation(defaultGraphLoc).create();
GraphHopperStorage graph = GraphBuilder.start(encodingManager).setRAM(defaultGraphLoc, true).create();

// 0-1
ReaderWay way_0_1 = new ReaderWay(27l);
Expand Down
Loading

0 comments on commit 9f71833

Please sign in to comment.