Skip to content

Commit

Permalink
Rename BaseGraph#checkInit, remove GraphHopperStorage#getAllCHGraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
easbar committed Dec 13, 2019
1 parent 0f32226 commit fa175ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
17 changes: 12 additions & 5 deletions core/src/main/java/com/graphhopper/storage/BaseGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import com.graphhopper.coll.GHBitSet;
import com.graphhopper.coll.GHBitSetImpl;
import com.graphhopper.coll.SparseIntIntArray;
import com.graphhopper.routing.profiles.*;
import com.graphhopper.routing.profiles.BooleanEncodedValue;
import com.graphhopper.routing.profiles.DecimalEncodedValue;
import com.graphhopper.routing.profiles.EnumEncodedValue;
import com.graphhopper.routing.profiles.IntEncodedValue;
import com.graphhopper.routing.util.AllEdgesIterator;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.routing.util.EncodingManager;
Expand Down Expand Up @@ -178,12 +181,17 @@ public Graph getBaseGraph() {
return this;
}

void checkInit() {
void checkNotInitialized() {
if (initialized)
throw new IllegalStateException("You cannot configure this GraphStorage "
+ "after calling create or loadExisting. Calling one of the methods twice is also not allowed.");
}

void checkInitialized() {
if (!initialized)
throw new IllegalStateException("The graph has not yet been initialized.");
}

protected int loadNodesHeader() {
nodeEntryBytes = nodes.getHeader(1 * 4);
nodeCount = nodes.getHeader(2 * 4);
Expand Down Expand Up @@ -313,8 +321,7 @@ protected final void initNodeAndEdgeEntrySize() {
* extend byte capacity
*/
final void ensureNodeIndex(int nodeIndex) {
if (!initialized)
throw new AssertionError("The graph has not yet been initialized.");
checkInitialized();

if (nodeIndex < nodeCount)
return;
Expand Down Expand Up @@ -354,7 +361,7 @@ public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirecti
}

private void setSegmentSize(int bytes) {
checkInit();
checkNotInitialized();
nodes.setSegmentSize(bytes);
edges.setSegmentSize(bytes);
wayGeometry.setSegmentSize(bytes);
Expand Down
28 changes: 11 additions & 17 deletions core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public GraphHopperStorage(List<CHProfile> chProfiles, Directory dir, EncodingMan
InternalGraphEventListener listener = new InternalGraphEventListener() {
@Override
public void initStorage() {
for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
cg.initStorage();
}
}

@Override
public void freeze() {
for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
cg._prepareForContraction();
}
}
Expand All @@ -95,7 +95,6 @@ public void freeze() {
}

public CHGraph getCHGraph() {
Collection<CHGraphImpl> chGraphs = getAllCHGraphs();
if (chGraphs.isEmpty()) {
throw new IllegalStateException("There is no CHGraph");
} else if (chGraphs.size() > 1) {
Expand All @@ -109,7 +108,6 @@ public CHGraph getCHGraph() {
* @return the {@link CHGraph} for the specified {@link CHProfile}
*/
public CHGraph getCHGraph(CHProfile profile) {
Collection<CHGraphImpl> chGraphs = getAllCHGraphs();
if (chGraphs.isEmpty())
throw new IllegalStateException("There is no CHGraph");

Expand All @@ -127,7 +125,7 @@ public CHGraph getCHGraph(CHProfile profile) {
}

public boolean isCHPossible() {
return !getAllCHGraphs().isEmpty();
return !chGraphs.isEmpty();
}

public List<CHProfile> getCHProfiles() {
Expand Down Expand Up @@ -162,7 +160,7 @@ public Directory getDirectory() {
*/
@Override
public GraphHopperStorage create(long byteCount) {
baseGraph.checkInit();
baseGraph.checkNotInitialized();
if (encodingManager == null)
throw new IllegalStateException("EncodingManager can only be null if you call loadExisting");

Expand All @@ -179,7 +177,7 @@ public GraphHopperStorage create(long byteCount) {

baseGraph.create(initSize);

for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
cg.create(byteCount);
}

Expand Down Expand Up @@ -226,7 +224,7 @@ public void optimize() {

@Override
public boolean loadExisting() {
baseGraph.checkInit();
baseGraph.checkNotInitialized();
if (properties.loadExisting()) {
properties.checkVersions(false);
// check encoding for compatibility
Expand Down Expand Up @@ -256,7 +254,7 @@ public boolean loadExisting() {

checkIfConfiguredAndLoadedWeightingsCompatible();

for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
if (!cg.loadExisting())
throw new IllegalStateException("Cannot load " + cg);
}
Expand Down Expand Up @@ -304,7 +302,7 @@ private List<String> parseList(String listStr) {

@Override
public void flush() {
for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
if (!cg.isClosed())
cg.flush();
}
Expand All @@ -318,7 +316,7 @@ public void close() {
properties.close();
baseGraph.close();

for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
if (!cg.isClosed())
cg.close();
}
Expand All @@ -333,7 +331,7 @@ public boolean isClosed() {
public long getCapacity() {
long cnt = baseGraph.getCapacity() + properties.getCapacity();

for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
cnt += cg.getCapacity();
}
return cnt;
Expand All @@ -355,7 +353,7 @@ boolean isFrozen() {
@Override
public String toDetailsString() {
String str = baseGraph.toDetailsString();
for (CHGraphImpl cg : getAllCHGraphs()) {
for (CHGraphImpl cg : chGraphs) {
str += ", " + cg.toDetailsString();
}

Expand Down Expand Up @@ -451,10 +449,6 @@ public boolean isAdjacentToNode(int edge, int node) {
return baseGraph.isAdjacentToNode(edge, node);
}

private Collection<CHGraphImpl> getAllCHGraphs() {
return chGraphs;
}

/**
* Flush and close resources like wayGeometry that are not needed for CH preparation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ protected GraphHopperStorage newGHStorage(Directory dir, boolean enabled3D, int
public void testNoCreateCalled() {
try (GraphHopperStorage gs = GraphBuilder.start(encodingManager).build()) {
((BaseGraph) gs.getBaseGraph()).ensureNodeIndex(123);
fail("AssertionError should be raised");
} catch (AssertionError err) {
fail("IllegalStateException should be raised");
} catch (IllegalStateException err) {
// ok
} catch (Exception ex) {
fail("AssertionError should be raised, but was " + ex.toString());
fail("IllegalStateException should be raised, but was " + ex.toString());
}
}

Expand Down

0 comments on commit fa175ff

Please sign in to comment.