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

Commit

Permalink
[core] Add API for invalidating tiles
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tmpsantos committed May 20, 2019
1 parent 521ac5c commit 418fdf3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions platform/default/include/mbgl/storage/offline_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class OfflineDatabase : private util::noncopyable {
// 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&,
Expand All @@ -63,6 +70,7 @@ class OfflineDatabase : private util::noncopyable {
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&);
Expand Down
41 changes: 41 additions & 0 deletions platform/default/src/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 418fdf3

Please sign in to comment.