From 5b4bb1398b3bab12d1493660f7ae1d7fadfa5ea0 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 10 Jun 2019 14:55:11 +0300 Subject: [core] Surface cache management APIs These APIs need to be on the DefaultFileSource in order to be visible for the SDKs bindings. --- include/mbgl/storage/default_file_source.hpp | 28 ++++++++++++++++++++++ .../src/mbgl/storage/default_file_source.cpp | 24 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 2a1e32baed..130f4f1022 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -144,6 +144,14 @@ public: */ void deleteOfflineRegion(OfflineRegion&&, std::function); + /* + * Invalidate all the tiles from an offline region forcing Mapbox GL to revalidate + * the tiles with the server before using. This is more efficient than deleting the + * offline region and downloading it again because if the data on the cache matches + * the server, no new data gets transmitted. + */ + void invalidateOfflineRegion(OfflineRegion&, std::function); + /* * Changing or bypassing this limit without permission from Mapbox is prohibited * by the Mapbox Terms of Service. @@ -186,6 +194,26 @@ public: */ void resetCache(std::function); + /* + * Forces revalidation of tiles in the ambient cache. + * + * Forces 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. + */ + void invalidateTileCache(std::function); + + /* + * Erase tiles from the ambient cache, freeing storage space. + * + * Erases the tile cache, freeing resources. This operation can be + * potentially slow because it will trigger a VACUUM on SQLite, + * forcing the database to move pages on the filesystem. + */ + void clearTileCache(std::function); + // For testing only. void setOnlineStatus(bool); diff --git a/platform/default/src/mbgl/storage/default_file_source.cpp b/platform/default/src/mbgl/storage/default_file_source.cpp index eabb7b45c3..92e86b25d0 100644 --- a/platform/default/src/mbgl/storage/default_file_source.cpp +++ b/platform/default/src/mbgl/storage/default_file_source.cpp @@ -87,6 +87,10 @@ public: callback(offlineDatabase->deleteRegion(std::move(region))); } + void invalidateRegion(int64_t regionID, std::function callback) { + callback(offlineDatabase->invalidateRegion(regionID)); + } + void setRegionObserver(int64_t regionID, std::unique_ptr observer) { if (auto download = getDownload(regionID)) { download.value()->setObserver(std::move(observer)); @@ -184,6 +188,14 @@ public: callback(offlineDatabase->resetCache()); } + void invalidateTileCache(std::function callback) { + callback(offlineDatabase->invalidateTileCache()); + } + + void clearTileCache(std::function callback) { + callback(offlineDatabase->clearTileCache()); + } + private: expected getDownload(int64_t regionID) { auto it = downloads.find(regionID); @@ -295,6 +307,10 @@ void DefaultFileSource::deleteOfflineRegion(OfflineRegion&& region, std::functio impl->actor().invoke(&Impl::deleteRegion, std::move(region), callback); } +void DefaultFileSource::invalidateOfflineRegion(OfflineRegion& region, std::function callback) { + impl->actor().invoke(&Impl::invalidateRegion, region.getID(), callback); +} + void DefaultFileSource::setOfflineRegionObserver(OfflineRegion& region, std::unique_ptr observer) { impl->actor().invoke(&Impl::setRegionObserver, region.getID(), std::move(observer)); } @@ -327,6 +343,14 @@ void DefaultFileSource::resetCache(std::function call impl->actor().invoke(&Impl::resetCache, callback); } +void DefaultFileSource::invalidateTileCache(std::function callback) { + impl->actor().invoke(&Impl::invalidateTileCache, callback); +} + +void DefaultFileSource::clearTileCache(std::function callback) { + impl->actor().invoke(&Impl::clearTileCache, callback); +} + // For testing only: void DefaultFileSource::setOnlineStatus(const bool status) { -- cgit v1.2.1