Changeset 8649 in josm


Ignore:
Timestamp:
2015-08-08T18:32:10+02:00 (9 years ago)
Author:
wiktorn
Message:
  • performance improvements - move all cache accesses to worker thread, as now, mostly we will have accesses to disks
  • follow HTTP 302 redirection when downloading tiles. Addresses: #11770
Location:
trunk/src/org/openstreetmap/josm/data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r8643 r8649  
    174174        if (first || force) {
    175175            ensureCacheElement();
    176             if (!force && cacheElement != null && isCacheElementValid() && isObjectLoadable()) {
    177                 // we got something in cache, and it's valid, so lets return it
    178                 log.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey());
    179                 finishLoading(LoadResult.SUCCESS);
    180                 return;
    181             }
    182             // object not in cache, so submit work to separate thread
     176            // submit all jobs to separate thread, so calling thread is not blocked with IO when loading from disk
    183177            downloadJobExecutor.execute(this);
    184178        }
     
    229223        currentThread.setName("JCS Downloading: " + getUrl());
    230224        try {
     225            // try to fetch from cache
     226            if (!force && cacheElement != null && isCacheElementValid() && isObjectLoadable()) {
     227                // we got something in cache, and it's valid, so lets return it
     228                log.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey());
     229                finishLoading(LoadResult.SUCCESS);
     230                return;
     231            }
     232
    231233            // try to load object from remote resource
    232234            if (loadObject()) {
     
    306308            }
    307309
    308             HttpURLConnection urlConn = getURLConnection();
     310            HttpURLConnection urlConn = getURLConnection(getUrl());
    309311
    310312            if (isObjectLoadable()  &&
     
    314316            if (isObjectLoadable() && attributes.getEtag() != null) {
    315317                urlConn.addRequestProperty("If-None-Match", attributes.getEtag());
     318            }
     319
     320            // follow redirects
     321            for (int i = 0; i < 5; i++) {
     322                if (urlConn.getResponseCode() == 302) {
     323                    urlConn = getURLConnection(new URL(urlConn.getHeaderField("Location")));
     324                } else {
     325                    break;
     326                }
    316327            }
    317328            if (urlConn.getResponseCode() == 304) {
     
    447458    }
    448459
    449     private HttpURLConnection getURLConnection() throws IOException {
    450         HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
     460    private HttpURLConnection getURLConnection(URL url) throws IOException {
     461        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    451462        urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
    452463        urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
     
    465476
    466477    private boolean isCacheValidUsingHead() throws IOException {
    467         HttpURLConnection urlConn = getURLConnection();
     478        HttpURLConnection urlConn = getURLConnection(getUrl());
    468479        urlConn.setRequestMethod("HEAD");
     480        for (int i = 0; i < 5; i++) {
     481            if (urlConn.getResponseCode() == 302) {
     482                urlConn = getURLConnection(new URL(urlConn.getHeaderField("Location")));
     483            } else {
     484                break;
     485            }
     486        }
    469487        long lastModified = urlConn.getLastModified();
    470488        return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
  • trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java

    r8647 r8649  
    3737 */
    3838public class TemplatedWMSTileSource extends TMSTileSource implements TemplatedTileSource {
    39     private Map<String, String> headers = new ConcurrentHashMap<>();
     39    private final Map<String, String> headers = new ConcurrentHashMap<>();
    4040    private final Set<String> serverProjections;
    4141    private EastNorth topLeftCorner;
    4242    private Bounds worldBounds;
     43    private int[] tileXMax;
     44    private int[] tileYMax;
    4345
    4446    private static final Pattern PATTERN_HEADER  = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}");
     
    8991        EastNorth max = proj.latlon2eastNorth(worldBounds.getMax());
    9092        this.topLeftCorner = new EastNorth(min.east(), max.north());
     93
     94        LatLon bottomRight = new LatLon(worldBounds.getMinLat(), worldBounds.getMaxLon());
     95        tileXMax = new int[getMaxZoom() + 1];
     96        tileYMax = new int[getMaxZoom() + 1];
     97        for(int zoom = getMinZoom(); zoom <= getMaxZoom(); zoom++) {
     98            TileXY maxTileIndex = latLonToTileXY(bottomRight.toCoordinate(), zoom);
     99            tileXMax[zoom] = maxTileIndex.getXIndex();
     100            tileYMax[zoom] = maxTileIndex.getYIndex();
     101        }
    91102    }
    92103
     
    229240    @Override
    230241    public int getTileXMax(int zoom) {
    231         LatLon bottomRight = new LatLon(worldBounds.getMinLat(), worldBounds.getMaxLon());
    232         return latLonToTileXY(bottomRight.toCoordinate(), zoom).getXIndex();
     242        return tileXMax[zoom];
    233243    }
    234244
     
    240250    @Override
    241251    public int getTileYMax(int zoom) {
    242         LatLon bottomRight = new LatLon(worldBounds.getMinLat(), worldBounds.getMaxLon());
    243         return latLonToTileXY(bottomRight.toCoordinate(), zoom).getYIndex();
     252        return tileYMax[zoom];
    244253    }
    245254
Note: See TracChangeset for help on using the changeset viewer.