Small fixes~

This commit is contained in:
Victor Shcherb 2020-09-10 11:06:10 +02:00
parent d0c6270a00
commit 9d37cff564
2 changed files with 18 additions and 15 deletions

View file

@ -13,16 +13,16 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public class MetaTileFileSystemCache { public class MetaTileFileSystemCache {
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
private static final Object TILES_FOLDER = "tiles"; private static final String TILES_FOLDER = "webtiles";
private static final int MAX_IN_MEMORY_CACHE_SIZE = 128; private static final int MAX_IN_MEMORY_CACHE_SIZE = 16;
private static final int MAX_CACHE_SIZE = 64; private static final int MAX_CACHE_SIZE = 64;
private final ConcurrentLinkedQueue<TileEndpoint.MetaTileCache> inMemoryCache = new ConcurrentLinkedQueue<>(); private final ConcurrentLinkedQueue<TileEndpoint.MetaTileCache> inMemoryCache = new ConcurrentLinkedQueue<>();
private final File externalCacheDir; private final File externalCacheDir;
public boolean inMemoryCacheEnabled = true; public boolean inMemoryCacheEnabled = true;
public MetaTileFileSystemCache(OsmandApplication application) { public MetaTileFileSystemCache(OsmandApplication application) {
externalCacheDir = new File( externalCacheDir = application.getAppPath(TILES_FOLDER);
application.getExternalCacheDir().getAbsoluteFile() + File.separator + TILES_FOLDER); new File(application.getExternalCacheDir(), TILES_FOLDER)
if (!externalCacheDir.exists()) { if (!externalCacheDir.exists()) {
externalCacheDir.mkdir(); externalCacheDir.mkdir();
} }
@ -32,8 +32,9 @@ public class MetaTileFileSystemCache {
while (inMemoryCache.size() > MAX_IN_MEMORY_CACHE_SIZE) { while (inMemoryCache.size() > MAX_IN_MEMORY_CACHE_SIZE) {
inMemoryCache.poll(); inMemoryCache.poll();
} }
// TODO list files too slow, better to have local variable to monitor or local list
while (externalCacheDir.listFiles().length > MAX_CACHE_SIZE) { while (externalCacheDir.listFiles().length > MAX_CACHE_SIZE) {
//remove outdated files
for (int i = 0; i < externalCacheDir.listFiles().length - MAX_CACHE_SIZE; i++) { for (int i = 0; i < externalCacheDir.listFiles().length - MAX_CACHE_SIZE; i++) {
externalCacheDir.listFiles()[i].delete(); externalCacheDir.listFiles()[i].delete();
} }
@ -56,9 +57,9 @@ public class MetaTileFileSystemCache {
} }
} }
public TileEndpoint.MetaTileCache get(int zoom, int METATILE_SIZE, int x, int y) { public TileEndpoint.MetaTileCache get(int zoom, int metaTileSize, int x, int y) {
int mx = (x / METATILE_SIZE) * METATILE_SIZE; int mx = (x / metaTileSize) * metaTileSize;
int my = (y / METATILE_SIZE) * METATILE_SIZE; int my = (y / metaTileSize) * metaTileSize;
if (inMemoryCacheEnabled) { if (inMemoryCacheEnabled) {
for (TileEndpoint.MetaTileCache r : inMemoryCache) { for (TileEndpoint.MetaTileCache r : inMemoryCache) {
if (r.getZoom() == zoom && r.getEx() >= x && r.getEy() >= y && r.getSx() <= x && r.getSy() <= y) { 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()) { if (file.exists()) {
TileEndpoint.MetaTileCache tile = new TileEndpoint.MetaTileCache( TileEndpoint.MetaTileCache tile = new TileEndpoint.MetaTileCache(
BitmapFactory.decodeFile(file.getAbsolutePath()), 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) { if (inMemoryCacheEnabled) {
inMemoryCache.add(tile); inMemoryCache.add(tile);
} }

View file

@ -132,10 +132,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
int zoom = Integer.parseInt(prms[1]); int zoom = Integer.parseInt(prms[1]);
int x = Integer.parseInt(prms[2]); int x = Integer.parseInt(prms[2]);
int y = Integer.parseInt(prms[3]); int y = Integer.parseInt(prms[3]);
MetaTileCache res; MetaTileCache res = cache.get(zoom, METATILE_SIZE, x, y);
synchronized (this) {
res = cache.get(zoom, METATILE_SIZE, x, y);
}
if (res == null) { if (res == null) {
res = requestMetatile(x, y, zoom); res = requestMetatile(x, y, zoom);
if (res == null) { if (res == null) {
@ -143,6 +140,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
return OsmAndHttpServer.ErrorResponses.response500; return OsmAndHttpServer.ErrorResponses.response500;
} }
} }
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = res.getSubtile(x, y); Bitmap bmp = res.getSubtile(x, y);
if (bmp == null) { if (bmp == null) {
@ -158,6 +156,10 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
} }
private synchronized MetaTileCache requestMetatile(int x, int y, int zoom) { 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 mx = (x / METATILE_SIZE) * METATILE_SIZE;
int my = (y / METATILE_SIZE) * METATILE_SIZE; int my = (y / METATILE_SIZE) * METATILE_SIZE;
double lat = MapUtils.getLatitudeFromTile(zoom, my + 0.5 * 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; res.zoom = zoom;
Bitmap tempBmp = mapActivity.getMapView().getBufferBitmap(); Bitmap tempBmp = mapActivity.getMapView().getBufferBitmap();
res.bmp = tempBmp.copy(tempBmp.getConfig(), true); res.bmp = tempBmp.copy(tempBmp.getConfig(), true);
cache.put(res); this.cache.put(res);
} }
return res; return res;
} catch (InterruptedException e) { } catch (InterruptedException e) {