From 418fdf30ac52e05960a0a109aa864d1e1ce4e662 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 16 May 2019 16:29:12 +0300 Subject: [PATCH] [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. --- .../include/mbgl/storage/offline_database.hpp | 8 ++++ .../src/mbgl/storage/offline_database.cpp | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/platform/default/include/mbgl/storage/offline_database.hpp b/platform/default/include/mbgl/storage/offline_database.hpp index 3ba15d58131..3de829e6350 100644 --- a/platform/default/include/mbgl/storage/offline_database.hpp +++ b/platform/default/include/mbgl/storage/offline_database.hpp @@ -51,6 +51,13 @@ class OfflineDatabase : private util::noncopyable { // Return value is (inserted, stored size) std::pair 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 listRegions(); expected createRegion(const OfflineRegionDefinition&, @@ -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> getRegionResource(int64_t regionID, const Resource&); diff --git a/platform/default/src/mbgl/storage/offline_database.cpp b/platform/default/src/mbgl/storage/offline_database.cpp index 1639c4484ce..0b3bcc049f7 100644 --- a/platform/default/src/mbgl/storage/offline_database.cpp +++ b/platform/default/src/mbgl/storage/offline_database.cpp @@ -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 OfflineDatabase::listRegions() try { mapbox::sqlite::Query query{ getStatement("SELECT id, definition, description FROM regions") }; OfflineRegions result;