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 /platform/default/default_file_source.cpp | |
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 'platform/default/default_file_source.cpp')
-rw-r--r-- | platform/default/default_file_source.cpp | 33 |
1 files changed, 8 insertions, 25 deletions
diff --git a/platform/default/default_file_source.cpp b/platform/default/default_file_source.cpp index cdf5e1e442..db0f4ccac6 100644 --- a/platform/default/default_file_source.cpp +++ b/platform/default/default_file_source.cpp @@ -50,8 +50,8 @@ public: std::unique_ptr<FileRequest> onlineRequest; }; - Impl(const std::string& cachePath) - : offlineDatabase(cachePath) { + Impl(const std::string& cachePath, uint64_t maximumCacheSize, uint64_t maximumCacheEntrySize) + : offlineDatabase(cachePath, maximumCacheSize, maximumCacheEntrySize) { } void setAccessToken(const std::string& accessToken) { @@ -97,15 +97,6 @@ public: } } - void removeUnusedOfflineResources(std::function<void (std::exception_ptr)> callback) { - try { - offlineDatabase.removeUnusedResources(); - callback({}); - } catch (...) { - callback(std::current_exception()); - } - } - void setRegionObserver(int64_t regionID, std::unique_ptr<OfflineRegionObserver> observer) { getDownload(regionID).setObserver(std::move(observer)); } @@ -147,8 +138,12 @@ private: bool offline = false; }; -DefaultFileSource::DefaultFileSource(const std::string& cachePath, const std::string& assetRoot) - : thread(std::make_unique<util::Thread<DefaultFileSource::Impl>>(util::ThreadContext{"DefaultFileSource", util::ThreadType::Unknown, util::ThreadPriority::Low}, cachePath)), +DefaultFileSource::DefaultFileSource(const std::string& cachePath, + const std::string& assetRoot, + uint64_t maximumCacheSize, + uint64_t maximumCacheEntrySize) + : thread(std::make_unique<util::Thread<Impl>>(util::ThreadContext{"DefaultFileSource", util::ThreadType::Unknown, util::ThreadPriority::Low}, + cachePath, maximumCacheSize, maximumCacheEntrySize)), assetFileSource(std::make_unique<AssetFileSource>(assetRoot)) { } @@ -162,14 +157,6 @@ std::string DefaultFileSource::getAccessToken() const { return thread->invokeSync<std::string>(&Impl::getAccessToken); } -void DefaultFileSource::setMaximumCacheSize(uint64_t) { - // TODO -} - -void DefaultFileSource::setMaximumCacheEntrySize(uint64_t) { - // TODO -} - std::unique_ptr<FileRequest> DefaultFileSource::request(const Resource& resource, Callback callback) { class DefaultFileRequest : public FileRequest { public: @@ -219,10 +206,6 @@ void DefaultFileSource::getOfflineRegionStatus(OfflineRegion& region, std::funct thread->invoke(&Impl::getRegionStatus, region.getID(), callback); } -void DefaultFileSource::removeUnusedOfflineResources(std::function<void (std::exception_ptr)> callback) { - thread->invoke(&Impl::removeUnusedOfflineResources, callback); -} - // For testing only: void DefaultFileSource::put(const Resource& resource, const Response& response) { |