From 14a18fd770faad0ab061891d944032f435d2d9eb Mon Sep 17 00:00:00 2001 From: Arndt Date: Sun, 25 Oct 2015 13:39:23 +0100 Subject: [PATCH] more re-use after timeout --- .../src/main/java/btools/router/OsmTrack.java | 251 ++++++++++-------- .../main/java/btools/router/ProfileCache.java | 98 +++++++ .../java/btools/router/RoutingEngine.java | 102 ++++--- .../java/btools/mapaccess/NodesCache.java | 2 +- .../java/btools/mapaccess/PhysicalFile.java | 2 +- .../java/btools/routingapp/BRouterWorker.java | 9 +- 6 files changed, 301 insertions(+), 163 deletions(-) create mode 100644 brouter-core/src/main/java/btools/router/ProfileCache.java diff --git a/brouter-core/src/main/java/btools/router/OsmTrack.java b/brouter-core/src/main/java/btools/router/OsmTrack.java index 4f6946a..8de42ce 100644 --- a/brouter-core/src/main/java/btools/router/OsmTrack.java +++ b/brouter-core/src/main/java/btools/router/OsmTrack.java @@ -31,14 +31,14 @@ public final class OsmTrack public MatchedWaypoint endPoint; public long[] nogoChecksums; - + public boolean isDirty; + private static class OsmPathElementHolder { public OsmPathElement node; public OsmPathElementHolder nextHolder; } - - + public ArrayList nodes = new ArrayList(); private CompactLongMap nodesMap; @@ -55,16 +55,16 @@ public final class OsmTrack public void buildMap() { - nodesMap = new CompactLongMap(); - for( OsmPathElement node: nodes ) - { + nodesMap = new CompactLongMap(); + for ( OsmPathElement node : nodes ) + { long id = node.getIdFromPos(); OsmPathElementHolder nh = new OsmPathElementHolder(); nh.node = node; OsmPathElementHolder h = nodesMap.get( id ); if ( h != null ) { - while( h.nextHolder != null ) + while (h.nextHolder != null) { h = h.nextHolder; } @@ -74,15 +74,15 @@ public final class OsmTrack { nodesMap.fastPut( id, nh ); } - } - nodesMap = new FrozenLongMap( nodesMap ); + } + nodesMap = new FrozenLongMap( nodesMap ); } - + private ArrayList aggregateMessages() { ArrayList res = new ArrayList(); MessageData current = null; - for( OsmPathElement n : nodes ) + for ( OsmPathElement n : nodes ) { if ( n.message != null ) { @@ -110,7 +110,9 @@ public final class OsmTrack /** * writes the track in binary-format to a file - * @param filename the filename to write to + * + * @param filename + * the filename to write to */ public void writeBinary( String filename ) throws Exception { @@ -118,45 +120,46 @@ public final class OsmTrack endPoint.writeToStream( dos ); dos.writeInt( nodes.size() ); - for( OsmPathElement node: nodes ) - { + for ( OsmPathElement node : nodes ) + { node.writeToStream( dos ); - } + } dos.writeLong( nogoChecksums[0] ); dos.writeLong( nogoChecksums[1] ); dos.writeLong( nogoChecksums[2] ); + dos.writeBoolean( isDirty ); dos.close(); } public static OsmTrack readBinary( String filename, OsmNodeNamed newEp, long[] nogoChecksums ) { - OsmTrack t = null; - if ( filename != null ) - { - File f = new File( filename ); - if ( f.exists() ) - { - try - { + OsmTrack t = null; + if ( filename != null ) + { + File f = new File( filename ); + if ( f.exists() ) + { + try + { DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream( f ) ) ); MatchedWaypoint ep = MatchedWaypoint.readFromStream( dis ); int dlon = ep.waypoint.ilon - newEp.ilon; int dlat = ep.waypoint.ilat - newEp.ilat; if ( dlon < 20 && dlon > -20 && dlat < 20 && dlat > -20 ) { - t = new OsmTrack(); - t.endPoint = ep; - int n = dis.readInt(); - OsmPathElement last_pe = null; - for( int i=0; i 0 || nodes.size() == 0 ) { - nodes.add( t.nodes.get(i) ); + nodes.add( t.nodes.get( i ) ); } } distance += t.distance; @@ -228,7 +239,9 @@ public final class OsmTrack /** * writes the track in gpx-format to a file - * @param filename the filename to write to + * + * @param filename + * the filename to write to */ public void writeGpx( String filename ) throws Exception { @@ -240,14 +253,16 @@ public final class OsmTrack public String formatAsGpx() { - StringBuilder sb = new StringBuilder(8192); + StringBuilder sb = new StringBuilder( 8192 ); sb.append( "\n" ); - for( int i=messageList.size()-1; i >= 0; i-- ) + for ( int i = messageList.size() - 1; i >= 0; i-- ) { - String message = messageList.get(i); - if ( i < messageList.size()-1 ) message = "(alt-index " + i + ": " + message + " )"; - if ( message != null ) sb.append("\n"); + String message = messageList.get( i ); + if ( i < messageList.size() - 1 ) + message = "(alt-index " + i + ": " + message + " )"; + if ( message != null ) + sb.append( "\n" ); } sb.append( "\n" ); sb.append( " \n" ); - sb.append(" ").append(name).append("\n"); + sb.append( " " ).append( name ).append( "\n" ); sb.append( " \n" ); - for( OsmPathElement n : nodes ) + for ( OsmPathElement n : nodes ) { String sele = n.getSElev() == Short.MIN_VALUE ? "" : "" + n.getElev() + ""; - sb.append(" ").append(sele).append("\n"); + sb.append( " " ).append( sele ).append( "\n" ); } sb.append( " \n" ); @@ -281,7 +297,7 @@ public final class OsmTrack public String formatAsKml() { - StringBuilder sb = new StringBuilder(8192); + StringBuilder sb = new StringBuilder( 8192 ); sb.append( "\n" ); @@ -304,10 +320,9 @@ public final class OsmTrack sb.append( " 1\n" ); sb.append( " " ); - - for( OsmPathElement n : nodes ) + for ( OsmPathElement n : nodes ) { - sb.append(formatPos(n.getILon() - 180000000)).append(",").append(formatPos(n.getILat() - 90000000)).append("\n"); + sb.append( formatPos( n.getILon() - 180000000 ) ).append( "," ).append( formatPos( n.getILat() - 90000000 ) ).append( "\n" ); } sb.append( " \n" ); @@ -321,10 +336,10 @@ public final class OsmTrack } public List iternity; - + public String formatAsGeoJson() { - StringBuilder sb = new StringBuilder(8192); + StringBuilder sb = new StringBuilder( 8192 ); sb.append( "{\n" ); sb.append( " \"type\": \"FeatureCollection\",\n" ); @@ -339,22 +354,22 @@ public final class OsmTrack sb.append( " \"plain-ascend\": \"" ).append( plainAscend ).append( "\",\n" ); sb.append( " \"cost\": \"" ).append( cost ).append( "\",\n" ); sb.append( " \"messages\": [\n" ); - sb.append( " [\"").append( MESSAGES_HEADER.replaceAll("\t", "\", \"") ).append( "\"],\n" ); - for( String m : aggregateMessages() ) + sb.append( " [\"" ).append( MESSAGES_HEADER.replaceAll( "\t", "\", \"" ) ).append( "\"],\n" ); + for ( String m : aggregateMessages() ) { - sb.append( " [\"").append( m.replaceAll("\t", "\", \"") ).append( "\"],\n" ); + sb.append( " [\"" ).append( m.replaceAll( "\t", "\", \"" ) ).append( "\"],\n" ); } sb.deleteCharAt( sb.lastIndexOf( "," ) ); sb.append( " ]\n" ); - + sb.append( " },\n" ); - + if ( iternity != null ) { sb.append( " \"iternity\": [\n" ); - for( String s : iternity ) + for ( String s : iternity ) { - sb.append( " \"").append( s ).append( "\",\n" ); + sb.append( " \"" ).append( s ).append( "\",\n" ); } sb.deleteCharAt( sb.lastIndexOf( "," ) ); sb.append( " ],\n" ); @@ -363,10 +378,11 @@ public final class OsmTrack sb.append( " \"type\": \"LineString\",\n" ); sb.append( " \"coordinates\": [\n" ); - for( OsmPathElement n : nodes ) + for ( OsmPathElement n : nodes ) { String sele = n.getSElev() == Short.MIN_VALUE ? "" : ", " + n.getElev(); - sb.append( " [" ).append(formatPos(n.getILon() - 180000000)).append(", ").append(formatPos(n.getILat() - 90000000)).append(sele).append( "],\n" ); + sb.append( " [" ).append( formatPos( n.getILon() - 180000000 ) ).append( ", " ).append( formatPos( n.getILat() - 90000000 ) ) + .append( sele ).append( "],\n" ); } sb.deleteCharAt( sb.lastIndexOf( "," ) ); @@ -382,17 +398,20 @@ public final class OsmTrack private static String formatPos( int p ) { boolean negative = p < 0; - if ( negative ) p = -p; + if ( negative ) + p = -p; char[] ac = new char[12]; int i = 11; - while( p != 0 || i > 3 ) + while (p != 0 || i > 3) { - ac[i--] = (char)('0' + (p % 10)); + ac[i--] = (char) ( '0' + ( p % 10 ) ); p /= 10; - if ( i == 5 ) ac[i--] = '.'; + if ( i == 5 ) + ac[i--] = '.'; } - if ( negative ) ac[i--] = '-'; - return new String( ac, i+1, 11-i ); + if ( negative ) + ac[i--] = '-'; + return new String( ac, i + 1, 11 - i ); } public void dumpMessages( String filename, RoutingContext rc ) throws Exception @@ -404,14 +423,15 @@ public final class OsmTrack public void writeMessages( BufferedWriter bw, RoutingContext rc ) throws Exception { dumpLine( bw, MESSAGES_HEADER ); - for( String m : aggregateMessages() ) + for ( String m : aggregateMessages() ) { dumpLine( bw, m ); } - if ( bw != null ) bw.close(); + if ( bw != null ) + bw.close(); } - private void dumpLine( BufferedWriter bw, String s) throws Exception + private void dumpLine( BufferedWriter bw, String s ) throws Exception { if ( bw == null ) { @@ -426,42 +446,45 @@ public final class OsmTrack public void readGpx( String filename ) throws Exception { - File f = new File( filename ); - if ( !f.exists() ) return; - BufferedReader br = new BufferedReader( - new InputStreamReader( - new FileInputStream( f ) ) ); + File f = new File( filename ); + if ( !f.exists() ) + return; + BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( f ) ) ); - for(;;) + for ( ;; ) + { + String line = br.readLine(); + if ( line == null ) + break; + + int idx0 = line.indexOf( "= 0 ) { - String line = br.readLine(); - if ( line == null ) break; - - int idx0 = line.indexOf( "= 0 ) - { - idx0 += 12; - int idx1 = line.indexOf( '"', idx0 ); - int ilon = (int)((Double.parseDouble( line.substring( idx0, idx1 ) ) + 180. )*1000000. + 0.5); - int idx2 = line.indexOf( " lat=\"" ); - if ( idx2 < 0 ) continue; - idx2 += 6; - int idx3 = line.indexOf( '"', idx2 ); - int ilat = (int)((Double.parseDouble( line.substring( idx2, idx3 ) ) + 90. )*1000000. + 0.5); - nodes.add( OsmPathElement.create( ilon, ilat, (short)0, null, false ) ); - } + idx0 += 12; + int idx1 = line.indexOf( '"', idx0 ); + int ilon = (int) ( ( Double.parseDouble( line.substring( idx0, idx1 ) ) + 180. ) * 1000000. + 0.5 ); + int idx2 = line.indexOf( " lat=\"" ); + if ( idx2 < 0 ) + continue; + idx2 += 6; + int idx3 = line.indexOf( '"', idx2 ); + int ilat = (int) ( ( Double.parseDouble( line.substring( idx2, idx3 ) ) + 90. ) * 1000000. + 0.5 ); + nodes.add( OsmPathElement.create( ilon, ilat, (short) 0, null, false ) ); } - br.close(); + } + br.close(); } public boolean equalsTrack( OsmTrack t ) { - if ( nodes.size() != t.nodes.size() ) return false; - for( int i=0; i 30L*firstMatchCost ) + if ( matchPath != null && fastPartialRecalc && firstMatchCost < 500 && path.cost > 30L*firstMatchCost + && !costCuttingTrack.isDirty ) { logInfo( "early exit: firstMatchCost=" + firstMatchCost + " path.cost=" + path.cost ); throw new IllegalArgumentException( "early exit for a close recalc" ); diff --git a/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java b/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java index 725bb34..e0fbc07 100644 --- a/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java +++ b/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java @@ -194,7 +194,7 @@ public final class NodesCache } catch (Exception e) { - throw new RuntimeException( "error reading datafile " + currentFileName + ": ", e ); + throw new RuntimeException( "error reading datafile " + currentFileName + ": " + e, e ); } } diff --git a/brouter-mapaccess/src/main/java/btools/mapaccess/PhysicalFile.java b/brouter-mapaccess/src/main/java/btools/mapaccess/PhysicalFile.java index 2c4641b..fd73744 100644 --- a/brouter-mapaccess/src/main/java/btools/mapaccess/PhysicalFile.java +++ b/brouter-mapaccess/src/main/java/btools/mapaccess/PhysicalFile.java @@ -108,7 +108,7 @@ final public class PhysicalFile if ( len < pos+extraLen ) // > is o.k. for future extensions! { - throw new IOException( "file of size " + len + " + too short, should be " + (pos+extraLen) ); + throw new IOException( "file of size " + len + " too short, should be " + (pos+extraLen) ); } ra.seek( pos ); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java index 7f8f11b..5021d1e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java @@ -52,12 +52,9 @@ public class BRouterWorker RoutingEngine cr = new RoutingEngine( null, null, segmentDir, readPositions(params), rc ); cr.quite = true; cr.doRun( maxRunningTime ); - if ( cr.getErrorMessage() != null ) - { - return cr.getErrorMessage(); - } // store new reference track if any + // (can exist fot timeed-out search) if ( cr.getFoundRawTrack() != null ) { try @@ -67,6 +64,10 @@ public class BRouterWorker catch( Exception e ) {} } + if ( cr.getErrorMessage() != null ) + { + return cr.getErrorMessage(); + } String format = params.getString("trackFormat"); boolean writeKml = format != null && "kml".equals( format );