Skip to content

Commit 9dd22e4

Browse files
author
Peter
committed
fixed the main problem in graphhopper#112
1 parent 2a21fad commit 9dd22e4

File tree

8 files changed

+741
-30
lines changed

8 files changed

+741
-30
lines changed

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

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public static void main( String[] strs ) throws Exception
7272
private boolean sortGraph = false;
7373
boolean removeZipped = true;
7474
private boolean elevation = false;
75+
private LockFactory lockFactory = new NativeFSLockFactory();
76+
private final String fileLockName = "write.lock";
7577
// for routing
7678
private boolean simplifyRequest = true;
7779
// for index
@@ -495,6 +497,27 @@ public GraphHopper init( CmdArgs args )
495497
sortGraph = args.getBool("graph.doSort", sortGraph);
496498
removeZipped = args.getBool("graph.removeZipped", removeZipped);
497499
turnCosts = args.getBool("graph.turnCosts", turnCosts);
500+
if (args.get("graph.locktype", "native").equals("simple"))
501+
lockFactory = new SimpleFSLockFactory();
502+
else
503+
lockFactory = new NativeFSLockFactory();
504+
505+
// elevation
506+
String eleProviderStr = args.get("graph.elevation.provider", "noop").toLowerCase();
507+
String cacheDirStr = args.get("graph.elevation.cachedir", "");
508+
String baseURL = args.get("graph.elevation.baseurl", "");
509+
DAType elevationDAType = DAType.fromString(args.get("graph.elevation.dataaccess", "MMAP"));
510+
ElevationProvider tmpProvider = ElevationProvider.NOOP;
511+
if (eleProviderStr.equalsIgnoreCase("srtm"))
512+
tmpProvider = new SRTMProvider();
513+
// later:
514+
// else if(eleProviderStr.startsWith("cgiar:"))
515+
// eleProvider = new CGIARProvider().setCacheDir(new File());
516+
517+
tmpProvider.setCacheDir(new File(cacheDirStr));
518+
tmpProvider.setBaseURL(baseURL);
519+
tmpProvider.setInMemory(elevationDAType.isInMemory());
520+
setElevationProvider(tmpProvider);
498521

499522
// optimizable prepare
500523
minNetworkSize = args.getInt("prepare.minNetworkSize", minNetworkSize);
@@ -519,23 +542,6 @@ public GraphHopper init( CmdArgs args )
519542
workerThreads = args.getInt("osmreader.workerThreads", workerThreads);
520543
enableInstructions = args.getBool("osmreader.instructions", enableInstructions);
521544

522-
// elevation
523-
String eleProviderStr = args.get("graph.elevation.provider", "noop").toLowerCase();
524-
String cacheDirStr = args.get("graph.elevation.cachedir", "");
525-
String baseURL = args.get("graph.elevation.baseurl", "");
526-
DAType elevationDAType = DAType.fromString(args.get("graph.elevation.dataaccess", "MMAP"));
527-
ElevationProvider tmpProvider = ElevationProvider.NOOP;
528-
if (eleProviderStr.equalsIgnoreCase("srtm"))
529-
tmpProvider = new SRTMProvider();
530-
// later:
531-
// else if(eleProviderStr.startsWith("cgiar:"))
532-
// eleProvider = new CGIARProvider().setCacheDir(new File());
533-
534-
tmpProvider.setCacheDir(new File(cacheDirStr));
535-
tmpProvider.setBaseURL(baseURL);
536-
tmpProvider.setInMemory(elevationDAType.isInMemory());
537-
setElevationProvider(tmpProvider);
538-
539545
// index
540546
preciseIndexResolution = args.getInt("index.highResolution", preciseIndexResolution);
541547
return this;
@@ -571,18 +577,34 @@ public GraphHopper importOrLoad()
571577
private GraphHopper process( String graphHopperLocation )
572578
{
573579
setGraphHopperLocation(graphHopperLocation);
580+
Lock lock = null;
574581
try
575582
{
576-
importData();
577-
graph.getProperties().put("osmreader.import.date", formatDateTime(new Date()));
578-
} catch (IOException ex)
583+
if (graph.getDirectory().getDefaultType().isStoring())
584+
{
585+
lockFactory.setLockDir(new File(graphHopperLocation));
586+
lock = lockFactory.create(fileLockName);
587+
if (!lock.obtain())
588+
throw new RuntimeException("To avoid multiple writers we need to obtain a lock but it failed. In " + graphHopperLocation);
589+
}
590+
591+
try
592+
{
593+
importData();
594+
graph.getProperties().put("osmreader.import.date", formatDateTime(new Date()));
595+
} catch (IOException ex)
596+
{
597+
throw new RuntimeException("Cannot parse OSM file " + getOSMFile(), ex);
598+
}
599+
cleanUp();
600+
optimize();
601+
postProcessing();
602+
flush();
603+
} finally
579604
{
580-
throw new RuntimeException("Cannot parse OSM file " + getOSMFile(), ex);
605+
if (lock != null)
606+
lock.release();
581607
}
582-
cleanUp();
583-
optimize();
584-
postProcessing();
585-
flush();
586608
return this;
587609
}
588610

@@ -676,12 +698,29 @@ else if (turnCosts)
676698
graph = new GraphHopperStorage(dir, encodingManager, hasElevation());
677699

678700
graph.setSegmentSize(defaultSegmentSize);
679-
if (!graph.loadExisting())
680-
return false;
681701

682-
postProcessing();
683-
fullyLoaded = true;
684-
return true;
702+
Lock lock = null;
703+
try
704+
{
705+
if (graph.getDirectory().getDefaultType().isStoring())
706+
{
707+
lockFactory.setLockDir(new File(ghLocation));
708+
lock = lockFactory.create(fileLockName);
709+
if (!lock.obtain())
710+
throw new RuntimeException("To avoid reading partial data we need to obtain the lock but it failed. In " + ghLocation + ", ");
711+
}
712+
713+
if (!graph.loadExisting())
714+
return false;
715+
716+
postProcessing();
717+
fullyLoaded = true;
718+
return true;
719+
} finally
720+
{
721+
if (lock != null)
722+
lock.release();
723+
}
685724
}
686725

687726
/**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to Peter Karich under one or more contributor license
3+
* agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* Peter Karich licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the
9+
* License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package com.graphhopper.storage;
20+
21+
/**
22+
* A write lock. Influenced by Lucene code
23+
* <p>
24+
* @author Peter Karich
25+
*/
26+
public interface Lock
27+
{
28+
String getName();
29+
30+
boolean obtain();
31+
32+
boolean isLocked();
33+
34+
void release();
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to Peter Karich under one or more contributor license
3+
* agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* Peter Karich licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the
9+
* License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package com.graphhopper.storage;
20+
21+
import java.io.File;
22+
23+
/**
24+
* @author Peter Karich
25+
*/
26+
public interface LockFactory
27+
{
28+
void setLockDir( File lockDir );
29+
30+
Lock create( String fileName );
31+
32+
void forceRemove( String fileName );
33+
}

0 commit comments

Comments
 (0)