From 30aed4c366d0ca49784021903a39753b02187018 Mon Sep 17 00:00:00 2001 From: Anna Johnson Date: Mon, 13 Feb 2017 11:25:12 -0800 Subject: [PATCH 1/2] Added optional cache key for tile sources --- include/mbgl/storage/resource.hpp | 2 ++ include/mbgl/style/conversion/tileset.hpp | 11 +++++++++++ include/mbgl/util/tileset.hpp | 1 + platform/darwin/src/MGLTileSource.h | 8 ++++++++ platform/darwin/src/MGLTileSource.mm | 11 +++++++++++ .../default/mbgl/storage/offline_database.cpp | 16 ++++++++-------- .../default/mbgl/storage/offline_download.cpp | 2 +- src/mbgl/storage/resource.cpp | 2 ++ src/mbgl/tile/tile_loader_impl.hpp | 1 + 9 files changed, 45 insertions(+), 9 deletions(-) diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp index c05f40b65c4..8ec45d2bbcc 100644 --- a/include/mbgl/storage/resource.hpp +++ b/include/mbgl/storage/resource.hpp @@ -23,6 +23,7 @@ class Resource { struct TileData { std::string urlTemplate; + std::string cacheKey; uint8_t pixelRatio; int32_t x; int32_t y; @@ -44,6 +45,7 @@ class Resource { static Resource style(const std::string& url); static Resource source(const std::string& url); static Resource tile(const std::string& urlTemplate, + const std::string& cacheKey, float pixelRatio, int32_t x, int32_t y, diff --git a/include/mbgl/style/conversion/tileset.hpp b/include/mbgl/style/conversion/tileset.hpp index 1955cc16cf2..55c805ffe49 100644 --- a/include/mbgl/style/conversion/tileset.hpp +++ b/include/mbgl/style/conversion/tileset.hpp @@ -65,6 +65,17 @@ struct Converter { } result.attribution = std::move(*attribution); } + + auto cacheKeyValue = objectMember(value, "cacheKey"); + if (cacheKeyValue) { + optional cacheKey = toString(*cacheKeyValue); + if (!cacheKey) { + return Error { "source cacheKey must be a string" }; + } + result.cacheKey = std::move(*cacheKey); + } else { + result.cacheKey = result.tiles[0]; + } return result; } diff --git a/include/mbgl/util/tileset.hpp b/include/mbgl/util/tileset.hpp index 1f28a5039a6..0b49145cd2d 100644 --- a/include/mbgl/util/tileset.hpp +++ b/include/mbgl/util/tileset.hpp @@ -15,6 +15,7 @@ class Tileset { std::vector tiles; Range zoomRange { 0, 22 }; std::string attribution; + std::string cacheKey; Scheme scheme = Scheme::XYZ; // TileJSON also includes center, zoom, and bounds, but they are not used by mbgl. diff --git a/platform/darwin/src/MGLTileSource.h b/platform/darwin/src/MGLTileSource.h index bc29b0f95ce..d08ceeb5bbb 100644 --- a/platform/darwin/src/MGLTileSource.h +++ b/platform/darwin/src/MGLTileSource.h @@ -41,6 +41,14 @@ extern MGL_EXPORT const MGLTileSourceOption MGLTileSourceOptionMinimumZoomLevel; */ extern MGL_EXPORT const MGLTileSourceOption MGLTileSourceOptionMaximumZoomLevel; +/** + A string to use to cache this source. This is useful + if the url for this source may change. + + By default, the url template for the source is used + */ +extern MGL_EXPORT const MGLTileSourceOption MGLTileSourceOptionCacheKey; + #if TARGET_OS_IPHONE /** An HTML string defining the buttons to be displayed in an action sheet when the diff --git a/platform/darwin/src/MGLTileSource.mm b/platform/darwin/src/MGLTileSource.mm index aa2a299a464..9d66ce9f84c 100644 --- a/platform/darwin/src/MGLTileSource.mm +++ b/platform/darwin/src/MGLTileSource.mm @@ -16,6 +16,7 @@ const MGLTileSourceOption MGLTileSourceOptionAttributionHTMLString = @"MGLTileSourceOptionAttributionHTMLString"; const MGLTileSourceOption MGLTileSourceOptionAttributionInfos = @"MGLTileSourceOptionAttributionInfos"; const MGLTileSourceOption MGLTileSourceOptionTileCoordinateSystem = @"MGLTileSourceOptionTileCoordinateSystem"; +const MGLTileSourceOption MGLTileSourceOptionCacheKey = @"MGLTileSourceOptionCacheKey"; @implementation MGLTileSource @@ -125,5 +126,15 @@ - (NSString *)attributionHTMLString { } } + if (NSString *cacheKey = options[MGLTileSourceOptionCacheKey]) { + if (![cacheKey isKindOfClass:[NSString class]]) { + [NSException raise:NSInvalidArgumentException + format:@"MGLTileSourceOptionCacheKey must be set to a string."]; + } + tileSet.cacheKey = cacheKey.UTF8String; + } else { + tileSet.cacheKey = tileSet.tiles[0]; + } + return tileSet; } diff --git a/platform/default/mbgl/storage/offline_database.cpp b/platform/default/mbgl/storage/offline_database.cpp index 02736f10a4e..6b261c8ab68 100644 --- a/platform/default/mbgl/storage/offline_database.cpp +++ b/platform/default/mbgl/storage/offline_database.cpp @@ -357,7 +357,7 @@ optional> OfflineDatabase::getTile(const Resource: // clang-format on accessedStmt->bind(1, util::now()); - accessedStmt->bind(2, tile.urlTemplate); + accessedStmt->bind(2, tile.cacheKey); accessedStmt->bind(3, tile.pixelRatio); accessedStmt->bind(4, tile.x); accessedStmt->bind(5, tile.y); @@ -376,7 +376,7 @@ optional> OfflineDatabase::getTile(const Resource: " AND z = ?5 "); // clang-format on - stmt->bind(1, tile.urlTemplate); + stmt->bind(1, tile.cacheKey); stmt->bind(2, tile.pixelRatio); stmt->bind(3, tile.x); stmt->bind(4, tile.y); @@ -419,7 +419,7 @@ optional OfflineDatabase::hasTile(const Resource::TileData& tile) { " AND z = ?5 "); // clang-format on - stmt->bind(1, tile.urlTemplate); + stmt->bind(1, tile.cacheKey); stmt->bind(2, tile.pixelRatio); stmt->bind(3, tile.x); stmt->bind(4, tile.y); @@ -451,7 +451,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile, update->bind(1, util::now()); update->bind(2, response.expires); - update->bind(3, tile.urlTemplate); + update->bind(3, tile.cacheKey); update->bind(4, tile.pixelRatio); update->bind(5, tile.x); update->bind(6, tile.y); @@ -486,7 +486,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile, update->bind(2, response.etag); update->bind(3, response.expires); update->bind(4, util::now()); - update->bind(7, tile.urlTemplate); + update->bind(7, tile.cacheKey); update->bind(8, tile.pixelRatio); update->bind(9, tile.x); update->bind(10, tile.y); @@ -512,7 +512,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile, "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11) "); // clang-format on - insert->bind(1, tile.urlTemplate); + insert->bind(1, tile.cacheKey); insert->bind(2, tile.pixelRatio); insert->bind(3, tile.x); insert->bind(4, tile.y); @@ -648,7 +648,7 @@ bool OfflineDatabase::markUsed(int64_t regionID, const Resource& resource) { const Resource::TileData& tile = *resource.tileData; insert->bind(1, regionID); - insert->bind(2, tile.urlTemplate); + insert->bind(2, tile.cacheKey); insert->bind(3, tile.pixelRatio); insert->bind(4, tile.x); insert->bind(5, tile.y); @@ -673,7 +673,7 @@ bool OfflineDatabase::markUsed(int64_t regionID, const Resource& resource) { // clang-format on select->bind(1, regionID); - select->bind(2, tile.urlTemplate); + select->bind(2, tile.cacheKey); select->bind(3, tile.pixelRatio); select->bind(4, tile.x); select->bind(5, tile.y); diff --git a/platform/default/mbgl/storage/offline_download.cpp b/platform/default/mbgl/storage/offline_download.cpp index c8aa98d8742..b046f6c6ccf 100644 --- a/platform/default/mbgl/storage/offline_download.cpp +++ b/platform/default/mbgl/storage/offline_download.cpp @@ -243,7 +243,7 @@ void OfflineDownload::queueTiles(SourceType type, uint16_t tileSize, const Tiles for (const auto& tile : definition.tileCover(type, tileSize, tileset.zoomRange)) { status.requiredResourceCount++; resourcesRemaining.push_back( - Resource::tile(tileset.tiles[0], definition.pixelRatio, tile.x, tile.y, tile.z, tileset.scheme)); + Resource::tile(tileset.tiles[0], tileset.cacheKey, definition.pixelRatio, tile.x, tile.y, tile.z, tileset.scheme)); } } diff --git a/src/mbgl/storage/resource.cpp b/src/mbgl/storage/resource.cpp index 20dde1db56f..2f735a86d61 100644 --- a/src/mbgl/storage/resource.cpp +++ b/src/mbgl/storage/resource.cpp @@ -83,6 +83,7 @@ Resource Resource::glyphs(const std::string& urlTemplate, const FontStack& fontS } Resource Resource::tile(const std::string& urlTemplate, + const std::string& cacheKey, float pixelRatio, int32_t x, int32_t y, @@ -119,6 +120,7 @@ Resource Resource::tile(const std::string& urlTemplate, }), Resource::TileData { urlTemplate, + cacheKey, uint8_t(supportsRatio && pixelRatio > 1.0 ? 2 : 1), x, y, diff --git a/src/mbgl/tile/tile_loader_impl.hpp b/src/mbgl/tile/tile_loader_impl.hpp index 9a5b35a7af4..427fb7d2803 100644 --- a/src/mbgl/tile/tile_loader_impl.hpp +++ b/src/mbgl/tile/tile_loader_impl.hpp @@ -18,6 +18,7 @@ TileLoader::TileLoader(T& tile_, necessity(Necessity::Optional), resource(Resource::tile( tileset.tiles.at(0), + tileset.cacheKey, parameters.pixelRatio, id.canonical.x, id.canonical.y, From ae6e1adf1da13ec7b5857862e349df998d7a4024 Mon Sep 17 00:00:00 2001 From: Anna Johnson Date: Mon, 13 Feb 2017 12:27:17 -0800 Subject: [PATCH 2/2] updated change log --- platform/ios/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 1140753ebbe..5a943f68174 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -28,6 +28,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Fixed flickering that occurred when panning past the antimeridian. ([#7574](https://github.com/mapbox/mapbox-gl-native/pull/7574)) * Fixed an issue that could prevent a cached style from appearing while the device is offline. ([#7770](https://github.com/mapbox/mapbox-gl-native/pull/7770)) * Added `MGLDistanceFormatter` which can be used to format geographic distances. ([#7888](https://github.com/mapbox/mapbox-gl-native/pull/7888)) +* Added optional cacheKey to tilesets to allow caching of tile sources by key instead of url template ## 3.4.1