From 9d37cff56419ade11f8f992523534208fd09d778 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 10 Sep 2020 11:06:10 +0200 Subject: [PATCH] Small fixes~ --- .../plus/server/MetaTileFileSystemCache.java | 21 ++++++++++--------- .../plus/server/endpoints/TileEndpoint.java | 12 ++++++----- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/server/MetaTileFileSystemCache.java b/OsmAnd/src/net/osmand/plus/server/MetaTileFileSystemCache.java index 92393beba4..cafdd13b52 100644 --- a/OsmAnd/src/net/osmand/plus/server/MetaTileFileSystemCache.java +++ b/OsmAnd/src/net/osmand/plus/server/MetaTileFileSystemCache.java @@ -13,16 +13,16 @@ import java.util.concurrent.ConcurrentLinkedQueue; public class MetaTileFileSystemCache { private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); - private static final Object TILES_FOLDER = "tiles"; - private static final int MAX_IN_MEMORY_CACHE_SIZE = 128; + private static final String TILES_FOLDER = "webtiles"; + private static final int MAX_IN_MEMORY_CACHE_SIZE = 16; private static final int MAX_CACHE_SIZE = 64; private final ConcurrentLinkedQueue inMemoryCache = new ConcurrentLinkedQueue<>(); private final File externalCacheDir; public boolean inMemoryCacheEnabled = true; public MetaTileFileSystemCache(OsmandApplication application) { - externalCacheDir = new File( - application.getExternalCacheDir().getAbsoluteFile() + File.separator + TILES_FOLDER); + externalCacheDir = application.getAppPath(TILES_FOLDER); + new File(application.getExternalCacheDir(), TILES_FOLDER) if (!externalCacheDir.exists()) { externalCacheDir.mkdir(); } @@ -32,8 +32,9 @@ public class MetaTileFileSystemCache { while (inMemoryCache.size() > MAX_IN_MEMORY_CACHE_SIZE) { inMemoryCache.poll(); } + // TODO list files too slow, better to have local variable to monitor or local list while (externalCacheDir.listFiles().length > MAX_CACHE_SIZE) { - //remove outdated files + for (int i = 0; i < externalCacheDir.listFiles().length - MAX_CACHE_SIZE; i++) { externalCacheDir.listFiles()[i].delete(); } @@ -56,9 +57,9 @@ public class MetaTileFileSystemCache { } } - public TileEndpoint.MetaTileCache get(int zoom, int METATILE_SIZE, int x, int y) { - int mx = (x / METATILE_SIZE) * METATILE_SIZE; - int my = (y / METATILE_SIZE) * METATILE_SIZE; + public TileEndpoint.MetaTileCache get(int zoom, int metaTileSize, int x, int y) { + int mx = (x / metaTileSize) * metaTileSize; + int my = (y / metaTileSize) * metaTileSize; if (inMemoryCacheEnabled) { for (TileEndpoint.MetaTileCache r : inMemoryCache) { if (r.getZoom() == zoom && r.getEx() >= x && r.getEy() >= y && r.getSx() <= x && r.getSy() <= y) { @@ -66,11 +67,11 @@ public class MetaTileFileSystemCache { } } } - File file = new File(externalCacheDir, zoom + "_" + METATILE_SIZE + "_" + mx + "_" + my); + File file = new File(externalCacheDir, zoom + "_" + metaTileSize + "_" + mx + "_" + my); if (file.exists()) { TileEndpoint.MetaTileCache tile = new TileEndpoint.MetaTileCache( BitmapFactory.decodeFile(file.getAbsolutePath()), - mx, my, mx + METATILE_SIZE - 1, my + METATILE_SIZE - 1, zoom); + mx, my, mx + metaTileSize - 1, my + metaTileSize - 1, zoom); if (inMemoryCacheEnabled) { inMemoryCache.add(tile); } diff --git a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java index 3752fcdafd..91a054df17 100644 --- a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java +++ b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java @@ -132,10 +132,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { int zoom = Integer.parseInt(prms[1]); int x = Integer.parseInt(prms[2]); int y = Integer.parseInt(prms[3]); - MetaTileCache res; - synchronized (this) { - res = cache.get(zoom, METATILE_SIZE, x, y); - } + MetaTileCache res = cache.get(zoom, METATILE_SIZE, x, y); if (res == null) { res = requestMetatile(x, y, zoom); if (res == null) { @@ -143,6 +140,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { return OsmAndHttpServer.ErrorResponses.response500; } } + ByteArrayOutputStream stream = new ByteArrayOutputStream(); Bitmap bmp = res.getSubtile(x, y); if (bmp == null) { @@ -158,6 +156,10 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { } private synchronized MetaTileCache requestMetatile(int x, int y, int zoom) { + MetaTileCache cacheTile = this.cache.get(zoom, METATILE_SIZE, x, y); + if (cacheTile != null) { + return cacheTile; + } int mx = (x / METATILE_SIZE) * METATILE_SIZE; int my = (y / METATILE_SIZE) * METATILE_SIZE; double lat = MapUtils.getLatitudeFromTile(zoom, my + 0.5 * METATILE_SIZE); @@ -204,7 +206,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { res.zoom = zoom; Bitmap tempBmp = mapActivity.getMapView().getBufferBitmap(); res.bmp = tempBmp.copy(tempBmp.getConfig(), true); - cache.put(res); + this.cache.put(res); } return res; } catch (InterruptedException e) {