Skip to content

Commit 9f71833

Browse files
authored
Moves segment size setting of graphs into constructor (graphhopper#1794)
1 parent d3ebf2e commit 9f71833

14 files changed

+86
-107
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,7 @@ public boolean load(String graphHopperFolder) {
755755
chProfiles = Collections.emptyList();
756756
}
757757

758-
ghStorage = new GraphHopperStorage(chProfiles, dir, encodingManager, hasElevation(), encodingManager.needsTurnCostsSupport());
759-
ghStorage.setSegmentSize(defaultSegmentSize);
758+
ghStorage = new GraphHopperStorage(chProfiles, dir, encodingManager, hasElevation(), encodingManager.needsTurnCostsSupport(), defaultSegmentSize);
760759

761760
if (!new File(graphHopperFolder).exists())
762761
return false;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class BaseGraph implements Graph {
9797
private boolean frozen = false;
9898

9999
public BaseGraph(Directory dir, final EncodingManager encodingManager, boolean withElevation,
100-
InternalGraphEventListener listener, boolean withTurnCosts) {
100+
InternalGraphEventListener listener, boolean withTurnCosts, int segmentSize) {
101101
this.dir = dir;
102102
this.encodingManager = encodingManager;
103103
this.intsForFlags = encodingManager.getIntsForFlags();
@@ -151,6 +151,9 @@ public String toString() {
151151
} else {
152152
turnCostExtension = null;
153153
}
154+
if (segmentSize >= 0) {
155+
setSegmentSize(segmentSize);
156+
}
154157
}
155158

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

342-
void setSegmentSize(int bytes) {
345+
private void setSegmentSize(int bytes) {
343346
checkInit();
344347
nodes.setSegmentSize(bytes);
345348
edges.setSegmentSize(bytes);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class CHGraphImpl implements CHGraph, Storable<CHGraph> {
6363
private int shortcutCount = 0;
6464
private boolean isReadyForContraction;
6565

66-
CHGraphImpl(CHProfile chProfile, Directory dir, final BaseGraph baseGraph) {
66+
CHGraphImpl(CHProfile chProfile, Directory dir, final BaseGraph baseGraph, int segmentSize) {
6767
if (chProfile.getWeighting() == null)
6868
throw new IllegalStateException("Weighting for CHGraph cannot be null");
6969
this.chProfile = chProfile;
@@ -72,6 +72,10 @@ public class CHGraphImpl implements CHGraph, Storable<CHGraph> {
7272
this.nodesCH = dir.find("nodes_ch_" + name, DAType.getPreferredInt(dir.getDefaultType()));
7373
this.shortcuts = dir.find("shortcuts_" + name, DAType.getPreferredInt(dir.getDefaultType()));
7474
this.chEdgeAccess = new CHEdgeAccess(name);
75+
if (segmentSize >= 0) {
76+
nodesCH.setSegmentSize(segmentSize);
77+
shortcuts.setSegmentSize(segmentSize);
78+
}
7579
}
7680

7781
@Override
@@ -342,11 +346,6 @@ void initStorage() {
342346
nodeCHEntryBytes = N_CH_REF + 4;
343347
}
344348

345-
void setSegmentSize(int bytes) {
346-
nodesCH.setSegmentSize(bytes);
347-
shortcuts.setSegmentSize(bytes);
348-
}
349-
350349
@Override
351350
public CHGraph create(long bytes) {
352351
nodesCH.create(bytes);

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

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,35 @@
1919

2020
import com.graphhopper.routing.util.EncodingManager;
2121

22+
import java.util.ArrayList;
2223
import java.util.Arrays;
23-
import java.util.Collections;
24-
import java.util.HashSet;
2524
import java.util.List;
2625

2726
/**
28-
* For now this is just a helper class to quickly create a {@link GraphHopperStorage}
29-
* <p>
27+
* Used to build {@link GraphHopperStorage}
3028
*
3129
* @author Peter Karich
30+
* @author easbar
3231
*/
3332
public class GraphBuilder {
3433
private final EncodingManager encodingManager;
35-
private String location;
36-
private boolean mmap;
37-
private boolean store;
34+
private Directory dir = new RAMDirectory();
3835
private boolean elevation;
3936
private boolean turnCosts;
40-
private long byteCapacity = 100;
41-
private List<CHProfile> chProfiles = Collections.emptyList();
37+
private long bytes = 100;
38+
private int segmentSize = -1;
39+
private List<CHProfile> chProfiles = new ArrayList<>();
40+
41+
public static GraphBuilder start(EncodingManager encodingManager) {
42+
return new GraphBuilder(encodingManager);
43+
}
4244

4345
public GraphBuilder(EncodingManager encodingManager) {
4446
this.encodingManager = encodingManager;
47+
this.turnCosts = encodingManager.needsTurnCostsSupport();
4548
}
4649

47-
/**
48-
* This method enables creating CHGraphs with the specified CHProfiles
49-
*/
5050
public GraphBuilder setCHProfiles(List<CHProfile> chProfiles) {
51-
if (chProfiles.size() != new HashSet<>(chProfiles).size()) {
52-
throw new IllegalArgumentException("Given CH profiles contain duplicates, given: " + chProfiles);
53-
}
5451
this.chProfiles = chProfiles;
5552
return this;
5653
}
@@ -59,23 +56,29 @@ public GraphBuilder setCHProfiles(CHProfile... chProfiles) {
5956
return setCHProfiles(Arrays.asList(chProfiles));
6057
}
6158

62-
public GraphBuilder setLocation(String location) {
63-
this.location = location;
59+
public GraphBuilder setDir(Directory dir) {
60+
this.dir = dir;
6461
return this;
6562
}
6663

67-
public GraphBuilder setStore(boolean store) {
68-
this.store = store;
69-
return this;
64+
public GraphBuilder setMMap(String location) {
65+
return setDir(new MMapDirectory(location));
7066
}
7167

72-
public GraphBuilder setMmap(boolean mmap) {
73-
this.mmap = mmap;
74-
return this;
68+
public GraphBuilder setRAM() {
69+
return setDir(new RAMDirectory());
70+
}
71+
72+
public GraphBuilder setRAM(String location) {
73+
return setDir(new RAMDirectory(location));
74+
}
75+
76+
public GraphBuilder setRAM(String location, boolean store) {
77+
return setDir(new RAMDirectory(location, store));
7578
}
7679

77-
public GraphBuilder setExpectedSize(byte cap) {
78-
this.byteCapacity = cap;
80+
public GraphBuilder setBytes(long bytes) {
81+
this.bytes = bytes;
7982
return this;
8083
}
8184

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

92-
public boolean hasElevation() {
93-
return elevation;
94-
}
95-
96-
/**
97-
* Creates a CHGraph
98-
*/
99-
public CHGraph chGraphCreate(CHProfile chProfile) {
100-
return setCHProfiles(chProfile).create().getCHGraph();
95+
public GraphBuilder setSegmentSize(int segmentSize) {
96+
this.segmentSize = segmentSize;
97+
return this;
10198
}
10299

103100
/**
@@ -106,29 +103,13 @@ public CHGraph chGraphCreate(CHProfile chProfile) {
106103
* {@link #create} directly.
107104
*/
108105
public GraphHopperStorage build() {
109-
Directory dir = mmap ?
110-
new MMapDirectory(location) :
111-
new RAMDirectory(location, store);
112-
113-
boolean withTurnCosts = encodingManager.needsTurnCostsSupport() || turnCosts;
114-
return new GraphHopperStorage(chProfiles, dir, encodingManager, elevation, withTurnCosts);
106+
return new GraphHopperStorage(chProfiles, dir, encodingManager, elevation, turnCosts, segmentSize);
115107
}
116108

117109
/**
118110
* Default graph is a {@link GraphHopperStorage} with an in memory directory and disabled storing on flush.
119111
*/
120112
public GraphHopperStorage create() {
121-
return build().create(byteCapacity);
122-
}
123-
124-
/**
125-
* @throws IllegalStateException if not loadable.
126-
*/
127-
public GraphHopperStorage load() {
128-
GraphHopperStorage gs = build();
129-
if (!gs.loadExisting()) {
130-
throw new IllegalStateException("Cannot load graph " + location);
131-
}
132-
return gs;
113+
return build().create(bytes);
133114
}
134115
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
import com.graphhopper.util.EdgeIteratorState;
2525
import com.graphhopper.util.shapes.BBox;
2626

27-
import java.util.ArrayList;
28-
import java.util.Collection;
29-
import java.util.Collections;
30-
import java.util.List;
27+
import java.util.*;
3128

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

6259
public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts) {
60+
this(chProfiles, dir, encodingManager, withElevation, withTurnCosts, -1);
61+
}
62+
63+
public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts, int segmentSize) {
6364
if (encodingManager == null)
6465
throw new IllegalArgumentException("EncodingManager needs to be non-null since 0.7. Create one using EncodingManager.create or EncodingManager.create(flagEncoderFactory, ghLocation)");
6566

67+
if (chProfiles.size() != new HashSet<>(chProfiles).size()) {
68+
throw new IllegalArgumentException("Given CH profiles contain duplicates, given: " + chProfiles);
69+
}
70+
6671
this.encodingManager = encodingManager;
6772
this.dir = dir;
6873
this.properties = new StorableProperties(dir);
@@ -82,10 +87,10 @@ public void freeze() {
8287
}
8388
};
8489

85-
baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts);
90+
baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts, segmentSize);
8691
this.chGraphs = new ArrayList<>(chProfiles.size());
8792
for (CHProfile chProfile : chProfiles) {
88-
chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph));
93+
chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph, segmentSize));
8994
}
9095
}
9196

@@ -152,15 +157,6 @@ public Directory getDirectory() {
152157
return dir;
153158
}
154159

155-
@Override
156-
public void setSegmentSize(int bytes) {
157-
baseGraph.setSegmentSize(bytes);
158-
159-
for (CHGraphImpl cg : getAllCHGraphs()) {
160-
cg.setSegmentSize(bytes);
161-
}
162-
}
163-
164160
/**
165161
* After configuring this storage you need to create it explicitly.
166162
*/

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public interface GraphStorage extends Storable<GraphStorage> {
2424

2525
EncodingManager getEncodingManager();
2626

27-
void setSegmentSize(int bytes);
28-
2927
String toDetailsString();
3028

3129
StorableProperties getProperties();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void testDoNotInterpolateTwice1645() {
116116
String loc = "./target/issue1645";
117117
Helper.removeDir(new File(loc));
118118
EncodingManager em = new EncodingManager.Builder().add(new OSMRoadEnvironmentParser()).add(new CarFlagEncoder()).build();
119-
GraphHopperStorage graph = new GraphBuilder(em).setLocation(loc).set3D(true).setStore(true).create();
119+
GraphHopperStorage graph = GraphBuilder.start(em).setRAM(loc, true).set3D(true).create();
120120

121121
// we need elevation
122122
NodeAccess na = graph.getNodeAccess();

core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ protected GraphHopperStorage createGHStorage() {
100100

101101
abstract GraphHopperStorage createGHStorage(String location, boolean is3D);
102102

103-
protected final GraphHopperStorage newRAMGHStorage() {
104-
return new GraphHopperStorage(new RAMDirectory(), encodingManager, false);
105-
}
106-
107103
@Before
108104
public void setUp() {
109105
Helper.removeDir(new File(locationParent));
@@ -316,9 +312,7 @@ public void testGetLocations() {
316312
public void testCopyTo() {
317313
graph = createGHStorage();
318314
initExampleGraph(graph);
319-
GraphHopperStorage gs = newRAMGHStorage();
320-
gs.setSegmentSize(8000);
321-
gs.create(10);
315+
GraphHopperStorage gs = GraphBuilder.start(encodingManager).setSegmentSize(8000).setBytes(10).create();
322316
graph.copyTo(gs);
323317
checkExampleGraph(gs);
324318

core/src/test/java/com/graphhopper/storage/GraphHopperStorageCHTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,22 @@ protected CHGraph getGraph(GraphHopperStorage ghStorage) {
4949
}
5050

5151
@Override
52-
public GraphHopperStorage newGHStorage(Directory dir, boolean is3D) {
53-
return newGHStorage(dir, is3D, false);
52+
protected GraphHopperStorage newGHStorage(Directory dir, boolean enabled3D) {
53+
return newGHStorage(dir, enabled3D, -1);
54+
}
55+
56+
@Override
57+
public GraphHopperStorage newGHStorage(Directory dir, boolean is3D, int segmentSize) {
58+
return newGHStorage(dir, is3D, false, segmentSize);
5459
}
5560

5661
private GraphHopperStorage newGHStorage(boolean is3D, boolean forEdgeBasedTraversal) {
57-
return newGHStorage(new RAMDirectory(defaultGraphLoc, true), is3D, forEdgeBasedTraversal).create(defaultSize);
62+
return newGHStorage(new RAMDirectory(defaultGraphLoc, true), is3D, forEdgeBasedTraversal, -1).create(defaultSize);
5863
}
5964

60-
private GraphHopperStorage newGHStorage(Directory dir, boolean is3D, boolean forEdgeBasedTraversal) {
65+
private GraphHopperStorage newGHStorage(Directory dir, boolean is3D, boolean forEdgeBasedTraversal, int segmentSize) {
6166
CHProfile chProfile = new CHProfile(new FastestWeighting(carEncoder), forEdgeBasedTraversal, INFINITE_U_TURN_COSTS);
62-
return new GraphHopperStorage(Collections.singletonList(chProfile), dir, encodingManager, is3D);
67+
return new GraphHopperStorage(Collections.singletonList(chProfile), dir, encodingManager, is3D, false, segmentSize);
6368
}
6469

6570
@Test
@@ -68,7 +73,7 @@ public void testCannotBeLoadedWithNormalGraphHopperStorageClass() {
6873
graph.flush();
6974
graph.close();
7075

71-
graph = new GraphBuilder(encodingManager).setLocation(defaultGraphLoc).setMmap(false).setStore(true).create();
76+
graph = GraphBuilder.start(encodingManager).setRAM(defaultGraphLoc, true).create();
7277
try {
7378
graph.loadExisting();
7479
fail();
@@ -521,7 +526,7 @@ public void testLoadingWithWrongWeighting_edge_throws() {
521526

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

core/src/test/java/com/graphhopper/storage/GraphHopperStorageForDataFlagEncoderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void tearDown() {
5353

5454
@Test
5555
public void testStorageProperties() {
56-
graph = new GraphBuilder(encodingManager).setStore(true).setLocation(defaultGraphLoc).create();
56+
graph = new GraphBuilder(encodingManager).setRAM(defaultGraphLoc, true).create();
5757

5858
// 0-1
5959
ReaderWay way_0_1 = new ReaderWay(27l);

core/src/test/java/com/graphhopper/storage/GraphHopperStorageLMTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public void testLoad() {
2222
Helper.removeDir(new File(defaultGraphLoc));
2323
CarFlagEncoder carFlagEncoder = new CarFlagEncoder();
2424
EncodingManager encodingManager = EncodingManager.create(carFlagEncoder);
25-
GraphHopperStorage graph = new GraphBuilder(encodingManager).setStore(true).
26-
setLocation(defaultGraphLoc).create();
25+
GraphHopperStorage graph = GraphBuilder.start(encodingManager).setRAM(defaultGraphLoc, true).create();
2726

2827
// 0-1
2928
ReaderWay way_0_1 = new ReaderWay(27l);

0 commit comments

Comments
 (0)