[core] Add API for invalidating tiles

Add new APIs for invalidating tiles, effectively forcing Mapbox
GL Native to check with the servers if the tiles are valid before
using them. This is more efficient then deleting tiles, because
in case of valid tiles, they won't get downloaded.

Fixes #4376.
This commit is contained in:
Thiago Marcos P. Santos
2019-05-16 16:29:12 +03:00
parent 2a3a1c7bb3
commit 1def782cf2
2 changed files with 49 additions and 0 deletions

View File

@@ -51,6 +51,13 @@ public:
// Return value is (inserted, stored size)
std::pair<bool, uint64_t> put(const Resource&, const Response&);
// Force Mapbox GL Native to revalidate tiles stored in the ambient
// cache with the tile server before using them, making sure they
// are the latest version. This is more efficient than cleaning the
// cache because if the tile is considered valid after the server
// lookup, it will not get downloaded again.
std::exception_ptr invalidateTileCache();
expected<OfflineRegions, std::exception_ptr> listRegions();
expected<OfflineRegion, std::exception_ptr> createRegion(const OfflineRegionDefinition&,
@@ -63,6 +70,7 @@ public:
updateMetadata(const int64_t regionID, const OfflineRegionMetadata&);
std::exception_ptr deleteRegion(OfflineRegion&&);
std::exception_ptr invalidateRegion(int64_t regionID);
// Return value is (response, stored size)
optional<std::pair<Response, uint64_t>> getRegionResource(int64_t regionID, const Resource&);

View File

@@ -606,6 +606,47 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile,
return true;
}
std::exception_ptr OfflineDatabase::invalidateTileCache() try {
// clang-format off
mapbox::sqlite::Query query{ getStatement(
"UPDATE tiles "
"SET expires = 0, must_revalidate = 1 "
"WHERE id NOT IN ("
" SELECT tile_id FROM region_tiles"
")"
) };
// clang-format on
query.run();
return nullptr;
} catch (const mapbox::sqlite::Exception& ex) {
handleError(ex, "invalidate tile cache");
return std::current_exception();
}
std::exception_ptr OfflineDatabase::invalidateRegion(int64_t regionID) try {
{
// clang-format off
mapbox::sqlite::Query query{ getStatement(
"UPDATE tiles "
"SET expires = 0, must_revalidate = 1 "
"WHERE id IN ("
" SELECT tile_id FROM region_tiles WHERE region_id = ?"
")"
) };
// clang-format on
query.bind(1, regionID);
query.run();
}
assert(db);
return nullptr;
} catch (const mapbox::sqlite::Exception& ex) {
handleError(ex, "invalidate region");
return std::current_exception();
}
expected<OfflineRegions, std::exception_ptr> OfflineDatabase::listRegions() try {
mapbox::sqlite::Query query{ getStatement("SELECT id, definition, description FROM regions") };
OfflineRegions result;