diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-02-09 17:19:27 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-02-10 15:40:20 -0800 |
commit | db3620c58f0c3351e26b0e3bcfd23ff414f829a1 (patch) | |
tree | e8def564e12307a997270b66a4ed46b68f5e13a4 /include/mbgl/storage | |
parent | 591012401072e63b89071787d90bf5ae4362dca1 (diff) | |
download | qtlocation-mapboxgl-db3620c58f0c3351e26b0e3bcfd23ff414f829a1.tar.gz |
[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".
Diffstat (limited to 'include/mbgl/storage')
-rw-r--r-- | include/mbgl/storage/default_file_source.hpp | 29 |
1 files changed, 9 insertions, 20 deletions
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 <mbgl/storage/file_source.hpp> #include <mbgl/storage/offline.hpp> +#include <mbgl/util/constants.hpp> #include <vector> @@ -14,15 +15,15 @@ template <typename T> 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<FileRequest> request(const Resource&, Callback) override; /* @@ -71,13 +72,11 @@ public: optional<OfflineRegionStatus>)>) 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<void (std::exception_ptr)>); - /* - * 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<void (std::exception_ptr)>); - // For testing only. void put(const Resource&, const Response&); void goOffline(); |