Skip to content

Commit 2842968

Browse files
committed
fixed code (list->queue) and tests after commit for graphhopper#916
1 parent 3f9cdf5 commit 2842968

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testEnsureCapacity() throws IOException {
9393
graph.create(100); // 100 is the minimum size
9494

9595
// assert that turnCostStorage can hold 104 turn cost entries at the beginning
96-
assertEquals(104, turnCostStorage.getCapacity() / 16);
96+
assertEquals(128, turnCostStorage.getCapacity());
9797

9898
Random r = new Random();
9999

graphhopper.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ NAME="${FILE%.*}"
174174

175175
if [ "$FILE" == "-" ]; then
176176
OSM_FILE=
177-
elif [ ${FILE: -4} == ".osm" ]; then
178-
OSM_FILE="$FILE"
179-
elif [ ${FILE: -4} == ".pbf" ]; then
177+
elif [ ${FILE: -4} == ".osm" ] || [ ${FILE: -4} == ".xml" ] || [ ${FILE: -4} == ".pbf" ]; then
180178
OSM_FILE="$FILE"
181179
elif [ ${FILE: -7} == ".osm.gz" ]; then
182180
OSM_FILE="$FILE"

reader-osm/src/main/java/com/graphhopper/reader/osm/OSMInputFile.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class OSMInputFile implements Sink, Closeable {
5454

5555
public OSMInputFile(File file) throws IOException {
5656
bis = decode(file);
57-
itemQueue = new LinkedBlockingQueue<ReaderElement>(50000);
57+
itemQueue = new LinkedBlockingQueue<>(50_000);
5858
}
5959

6060
public OSMInputFile open() throws XMLStreamException {
@@ -251,9 +251,10 @@ public void process(ReaderElement item) {
251251
} catch (InterruptedException ex) {
252252
throw new RuntimeException(ex);
253253
}
254+
}
254255

255-
// throw exception if full
256-
// itemQueue.add(item);
256+
public int getUnprocessedElements() {
257+
return itemQueue.size();
257258
}
258259

259260
@Override

reader-osm/src/main/java/com/graphhopper/reader/osm/OSMReader.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class OSMReader implements DataReader {
9393
private long locations;
9494
private long skippedLocations;
9595
private EncodingManager encodingManager = null;
96-
private int workerThreads = -1;
96+
private int workerThreads = 2;
9797
// Using the correct Map<Long, Integer> is hard. We need a memory efficient and fast solution for big data sets!
9898
//
9999
// very slow: new SparseLongLongArray
@@ -176,7 +176,7 @@ void preProcess(File osmFile) {
176176
prepareHighwayNode(wayNodes.get(index));
177177
}
178178

179-
if (++tmpWayCounter % 5000000 == 0) {
179+
if (++tmpWayCounter % 10_000_000 == 0) {
180180
LOGGER.info(nf(tmpWayCounter) + " (preprocess), osmIdMap:" + nf(getNodeMap().getSize()) + " ("
181181
+ getNodeMap().getMemoryUsage() + "MB) " + Helper.getMemInfo());
182182
}
@@ -189,7 +189,7 @@ void preProcess(File osmFile) {
189189
if (relation.hasTag("type", "restriction"))
190190
prepareRestrictionRelation(relation);
191191

192-
if (++tmpRelationCounter % 50000 == 0) {
192+
if (++tmpRelationCounter % 100_000 == 0) {
193193
LOGGER.info(nf(tmpRelationCounter) + " (preprocess), osmWayMap:" + nf(getRelFlagsMap().size())
194194
+ " " + Helper.getMemInfo());
195195
}
@@ -248,7 +248,7 @@ boolean filterWay(ReaderWay item) {
248248
}
249249

250250
/**
251-
* Creates the edges and nodes files from the specified osm file.
251+
* Creates the graph with edges and nodes from the specified osm file.
252252
*/
253253
private void writeOsm2Graph(File osmFile) {
254254
int tmp = (int) Math.max(getNodeMap().getSize() / 50, 100);
@@ -290,11 +290,14 @@ private void writeOsm2Graph(File osmFile) {
290290
default:
291291
throw new IllegalStateException("Unknown type " + item.getType());
292292
}
293-
if (++counter % 100000000 == 0) {
293+
if (++counter % 200_000_000 == 0) {
294294
LOGGER.info(nf(counter) + ", locs:" + nf(locations) + " (" + skippedLocations + ") " + Helper.getMemInfo());
295295
}
296296
}
297297

298+
if (in.getUnprocessedElements() > 0)
299+
throw new IllegalStateException("Still unprocessed elements in reader queue " + in.getUnprocessedElements());
300+
298301
// logger.info("storage nodes:" + storage.nodes() + " vs. graph nodes:" + storage.getGraph().nodes());
299302
} catch (Exception ex) {
300303
throw new RuntimeException("Couldn't process file " + osmFile + ", error: " + ex.getMessage(), ex);
@@ -304,7 +307,7 @@ private void writeOsm2Graph(File osmFile) {
304307

305308
finishedReading();
306309
if (graph.getNodes() == 0)
307-
throw new IllegalStateException("osm must not be empty. read " + counter + " lines and " + locations + " locations");
310+
throw new RuntimeException("Graph after reading OSM must not be empty. Read " + counter + " items and " + locations + " locations");
308311
}
309312

310313
/**

reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfDecoder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.LinkedList;
88
import java.util.List;
99
import java.util.Queue;
10+
import java.util.concurrent.ArrayBlockingQueue;
1011
import java.util.concurrent.ExecutorService;
1112
import java.util.concurrent.locks.Condition;
1213
import java.util.concurrent.locks.Lock;
@@ -26,7 +27,7 @@ public class PbfDecoder implements Runnable {
2627
private final Sink sink;
2728
private final Lock lock;
2829
private final Condition dataWaitCondition;
29-
private final List<PbfBlobResult> blobResults;
30+
private final Queue<PbfBlobResult> blobResults;
3031

3132
/**
3233
* Creates a new instance.
@@ -49,7 +50,8 @@ public PbfDecoder(PbfStreamSplitter streamSplitter, ExecutorService executorServ
4950
dataWaitCondition = lock.newCondition();
5051

5152
// Create the queue of blobs being decoded.
52-
blobResults = new ArrayList<>();
53+
// TODO #916 blobResults = new ArrayBlockingQueue<>(1000);
54+
blobResults = new LinkedList<>();
5355
}
5456

5557
/**
@@ -75,7 +77,7 @@ private void signalUpdate() {
7577
private void sendResultsToSink(int targetQueueSize) {
7678
while (blobResults.size() > targetQueueSize) {
7779
// Get the next result from the queue and wait for it to complete.
78-
PbfBlobResult blobResult = blobResults.remove(blobResults.size() - 1);
80+
PbfBlobResult blobResult = blobResults.remove();
7981
while (!blobResult.isComplete()) {
8082
// The thread hasn't finished processing yet so wait for an
8183
// update from another thread before checking again.

reader-osm/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ public void testRelation() {
421421
OSMReader reader = new OSMReader(ghStorage).
422422
setEncodingManager(manager);
423423
ReaderRelation osmRel = new ReaderRelation(1);
424-
osmRel.getMembers().add(new ReaderRelation.Member(ReaderRelation.WAY, 1, ""));
425-
osmRel.getMembers().add(new ReaderRelation.Member(ReaderRelation.WAY, 2, ""));
424+
osmRel.add(new ReaderRelation.Member(ReaderRelation.WAY, 1, ""));
425+
osmRel.add(new ReaderRelation.Member(ReaderRelation.WAY, 2, ""));
426426

427427
osmRel.setTag("route", "bicycle");
428428
osmRel.setTag("network", "lcn");
@@ -723,7 +723,7 @@ public void testPreferredLanguage() {
723723

724724
@Test
725725
public void testDataDateWithinPBF() {
726-
GraphHopper hopper = new GraphHopperTest(file6).importOrLoad();
726+
GraphHopper hopper = new GraphHopperTest("test-osm6.pbf").importOrLoad();
727727
GraphHopperStorage graph = hopper.getGraphHopperStorage();
728728

729729
assertEquals("2014-01-02T00:10:14Z", graph.getProperties().get("datareader.data.date"));
@@ -745,14 +745,15 @@ public void testCrossBoundary_issue667() {
745745
assertEquals(56, qr.getClosestEdge().getDistance() / 1000, 1);
746746
}
747747

748+
@Test
748749
public void testRoutingRequestFails_issue665() {
749750
GraphHopper hopper = new GraphHopperOSM()
750-
.setDataReaderFile("src/test/resources/com/graphhopper/reader/" + file7)
751+
.setDataReaderFile(getClass().getResource(file7).getFile())
751752
.setEncodingManager(new EncodingManager("car,motorcycle"))
752753
.setGraphHopperLocation(dir);
753754
hopper.getCHFactoryDecorator().setEnabled(false);
754755
hopper.importOrLoad();
755-
GHRequest req = new GHRequest(48.97725592769741, 8.256896138191223, 48.978875552977684, 8.25486302375793).
756+
GHRequest req = new GHRequest(48.977277, 8.256896, 48.978876, 8.254884).
756757
setWeighting("curvature").
757758
setVehicle("motorcycle");
758759

0 commit comments

Comments
 (0)