@@ -72,6 +72,8 @@ public static void main( String[] strs ) throws Exception
72
72
private boolean sortGraph = false ;
73
73
boolean removeZipped = true ;
74
74
private boolean elevation = false ;
75
+ private LockFactory lockFactory = new NativeFSLockFactory ();
76
+ private final String fileLockName = "write.lock" ;
75
77
// for routing
76
78
private boolean simplifyRequest = true ;
77
79
// for index
@@ -495,6 +497,27 @@ public GraphHopper init( CmdArgs args )
495
497
sortGraph = args .getBool ("graph.doSort" , sortGraph );
496
498
removeZipped = args .getBool ("graph.removeZipped" , removeZipped );
497
499
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 );
498
521
499
522
// optimizable prepare
500
523
minNetworkSize = args .getInt ("prepare.minNetworkSize" , minNetworkSize );
@@ -519,23 +542,6 @@ public GraphHopper init( CmdArgs args )
519
542
workerThreads = args .getInt ("osmreader.workerThreads" , workerThreads );
520
543
enableInstructions = args .getBool ("osmreader.instructions" , enableInstructions );
521
544
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
-
539
545
// index
540
546
preciseIndexResolution = args .getInt ("index.highResolution" , preciseIndexResolution );
541
547
return this ;
@@ -571,18 +577,34 @@ public GraphHopper importOrLoad()
571
577
private GraphHopper process ( String graphHopperLocation )
572
578
{
573
579
setGraphHopperLocation (graphHopperLocation );
580
+ Lock lock = null ;
574
581
try
575
582
{
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
579
604
{
580
- throw new RuntimeException ("Cannot parse OSM file " + getOSMFile (), ex );
605
+ if (lock != null )
606
+ lock .release ();
581
607
}
582
- cleanUp ();
583
- optimize ();
584
- postProcessing ();
585
- flush ();
586
608
return this ;
587
609
}
588
610
@@ -676,12 +698,29 @@ else if (turnCosts)
676
698
graph = new GraphHopperStorage (dir , encodingManager , hasElevation ());
677
699
678
700
graph .setSegmentSize (defaultSegmentSize );
679
- if (!graph .loadExisting ())
680
- return false ;
681
701
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
+ }
685
724
}
686
725
687
726
/**
0 commit comments