Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Feature/cachebykey #8041

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/mbgl/storage/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Resource {

struct TileData {
std::string urlTemplate;
std::string cacheKey;
uint8_t pixelRatio;
int32_t x;
int32_t y;
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions include/mbgl/style/conversion/tileset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ struct Converter<Tileset> {
}
result.attribution = std::move(*attribution);
}

auto cacheKeyValue = objectMember(value, "cacheKey");
if (cacheKeyValue) {
optional<std::string> 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;
}
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/util/tileset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Tileset {
std::vector<std::string> tiles;
Range<uint8_t> 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.
Expand Down
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLTileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions platform/darwin/src/MGLTileSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
const MGLTileSourceOption MGLTileSourceOptionAttributionHTMLString = @"MGLTileSourceOptionAttributionHTMLString";
const MGLTileSourceOption MGLTileSourceOptionAttributionInfos = @"MGLTileSourceOptionAttributionInfos";
const MGLTileSourceOption MGLTileSourceOptionTileCoordinateSystem = @"MGLTileSourceOptionTileCoordinateSystem";
const MGLTileSourceOption MGLTileSourceOptionCacheKey = @"MGLTileSourceOptionCacheKey";

@implementation MGLTileSource

Expand Down Expand Up @@ -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;
}
16 changes: 8 additions & 8 deletions platform/default/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ optional<std::pair<Response, uint64_t>> 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);
Expand All @@ -376,7 +376,7 @@ optional<std::pair<Response, uint64_t>> 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);
Expand Down Expand Up @@ -419,7 +419,7 @@ optional<int64_t> 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion platform/default/mbgl/storage/offline_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/storage/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/tile/tile_loader_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TileLoader<T>::TileLoader(T& tile_,
necessity(Necessity::Optional),
resource(Resource::tile(
tileset.tiles.at(0),
tileset.cacheKey,
parameters.pixelRatio,
id.canonical.x,
id.canonical.y,
Expand Down