From db3620c58f0c3351e26b0e3bcfd23ff414f829a1 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 9 Feb 2016 17:19:27 -0800 Subject: [core] Implement an eviction policy for OfflineDatabase When inserting an cached resource, or removing a region, remove least-recently used resources and tiles, not used by offline regions, until the used database size, as calculated by multiplying the number of in-use pages by the page size, is less than the maximum cache size minus 5 times the page size. In addition, OfflineDatabase may be configured to ignore cache puts of individual resources larger than a certain size. This policy is similar but not identical to the former SQLiteCache policy: * It accounts for offline, by exempting resources required by offline regions from eviction. * It must delete from two tables (resources and tiles), rather than one. Currently the strategy is naive: evict 50 rows at a time from each table. * It makes maximumCacheSize and maximumCacheEntrySize completely independent. The SQLiteCache implementation evicted when `usedSize > maximumCacheSize - 2 * maximumCacheEntrySize`. This evicts when `usedSize > maximumCacheSize - 5 * pageSize`. * It uses a non-unlimited default value for maximumCacheSize: 50 MB. We should have always had a limit in place; "a cache without an eviction policy is a resource leak". --- include/mbgl/storage/default_file_source.hpp | 29 +++++++++------------------- 1 file changed, 9 insertions(+), 20 deletions(-) (limited to 'include/mbgl/storage') diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index e7ffe125b1..0cee7de328 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -14,15 +15,15 @@ template class Thread; class DefaultFileSource : public FileSource { public: - DefaultFileSource(const std::string& cachePath, const std::string& assetRoot); + DefaultFileSource(const std::string& cachePath, + const std::string& assetRoot, + uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE, + uint64_t maximumCacheEntrySize = util::DEFAULT_MAX_CACHE_ENTRY_SIZE); ~DefaultFileSource() override; void setAccessToken(const std::string&); std::string getAccessToken() const; - void setMaximumCacheSize(uint64_t size); - void setMaximumCacheEntrySize(uint64_t size); - std::unique_ptr request(const Resource&, Callback) override; /* @@ -71,13 +72,11 @@ public: optional)>) const; /* - * Initiate the removal of offline region from the database. + * Remove an offline region from the database and perform any resources evictions + * necessary as a result. * - * All resources required by the region, but not also required by other regions, will - * become eligible for removal for space-optimization. Because the offline database is - * also used for ambient usage-based caching, and offline resources may still be useful - * for ambient usage, they are not immediately removed. To immediately remove resources - * not used by any extant region, call removeUnusedOfflineResources(). + * Eviction works by removing the least-recently requested resources not also required + * by other regions, until the database shrinks below a certain size. * * Note that this method takes ownership of the input, reflecting the fact that once * region deletion is initiated, it is not legal to perform further actions with the @@ -89,16 +88,6 @@ public: */ void deleteOfflineRegion(OfflineRegion&&, std::function); - /* - * Remove all resources in the database that are not required by any region, thus - * optimizing the disk space used by the offline database. - * - * When the operation is complete or encounters an error, the given callback will be - * executed on the database thread; it is the responsibility of the SDK bindings - * to re-execute a user-provided callback on the main thread. - */ - void removeUnusedOfflineResources(std::function); - // For testing only. void put(const Resource&, const Response&); void goOffline(); -- cgit v1.2.1