diff options
author | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2019-06-14 15:11:32 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2019-06-18 16:26:51 +0300 |
commit | 68e3cbacbc35ae6cd6c1e0fe2ab59ed262c32786 (patch) | |
tree | a8eb342508e1b089d05eb4c35bb1ce424ecee088 /platform/default/src/mbgl/storage/offline_database.cpp | |
parent | 78eeffe1717a37a91e6c59681421a20c52a4d003 (diff) | |
download | qtlocation-mapboxgl-68e3cbacbc35ae6cd6c1e0fe2ab59ed262c32786.tar.gz |
[core] Add method for setting a maximum size for the ambient cache
- Removed this parameter from the constructor, now that we
have a method to set it.
- Add the method and enforce the limits if the ambient cache exceeds the
size.
Diffstat (limited to 'platform/default/src/mbgl/storage/offline_database.cpp')
-rw-r--r-- | platform/default/src/mbgl/storage/offline_database.cpp | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/platform/default/src/mbgl/storage/offline_database.cpp b/platform/default/src/mbgl/storage/offline_database.cpp index 7caa013f9f..24ad3e8432 100644 --- a/platform/default/src/mbgl/storage/offline_database.cpp +++ b/platform/default/src/mbgl/storage/offline_database.cpp @@ -13,9 +13,8 @@ namespace mbgl { -OfflineDatabase::OfflineDatabase(std::string path_, uint64_t maximumCacheSize_) - : path(std::move(path_)), - maximumCacheSize(maximumCacheSize_) { +OfflineDatabase::OfflineDatabase(std::string path_) + : path(std::move(path_)) { try { initialize(); } catch (const util::IOException& ex) { @@ -88,6 +87,19 @@ void OfflineDatabase::cleanup() { } } +bool OfflineDatabase::disabled() { + if (maximumAmbientCacheSize) { + return false; + } + + auto regions = listRegions(); + if (regions && !regions.value().empty()) { + return false; + } + + return true; +} + void OfflineDatabase::handleError(const mapbox::sqlite::Exception& ex, const char* action) { if (ex.code == mapbox::sqlite::ResultCode::NotADB || ex.code == mapbox::sqlite::ResultCode::Corrupt || @@ -177,6 +189,10 @@ mapbox::sqlite::Statement& OfflineDatabase::getStatement(const char* sql) { } optional<Response> OfflineDatabase::get(const Resource& resource) try { + if (disabled()) { + return nullopt; + } + auto result = getInternal(resource); return result ? optional<Response>{ result->first } : nullopt; } catch (const util::IOException& ex) { @@ -209,6 +225,11 @@ std::pair<bool, uint64_t> OfflineDatabase::put(const Resource& resource, const R if (!db) { initialize(); } + + if (disabled()) { + return { false, 0 }; + } + mapbox::sqlite::Transaction transaction(*db, mapbox::sqlite::Transaction::Immediate); auto result = putInternal(resource, response, true); transaction.commit(); @@ -1120,7 +1141,7 @@ bool OfflineDatabase::evict(uint64_t neededFreeSize) { // The addition of pageSize is a fudge factor to account for non `data` column // size, and because pages can get fragmented on the database. - while (usedSize() + neededFreeSize + pageSize > maximumCacheSize) { + while (usedSize() + neededFreeSize + pageSize > maximumAmbientCacheSize) { // clang-format off mapbox::sqlite::Query accessedQuery{ getStatement( "SELECT max(accessed) " @@ -1187,6 +1208,29 @@ bool OfflineDatabase::evict(uint64_t neededFreeSize) { return true; } +std::exception_ptr OfflineDatabase::setMaximumAmbientCacheSize(uint64_t size) { + uint64_t previousMaximumAmbientCacheSize = maximumAmbientCacheSize; + + try { + maximumAmbientCacheSize = size; + + uint64_t databaseSize = getPragma<int64_t>("PRAGMA page_size") + * getPragma<int64_t>("PRAGMA page_count"); + + if (databaseSize > maximumAmbientCacheSize) { + evict(0); + db->exec("VACUUM"); + } + + return nullptr; + } catch (const mapbox::sqlite::Exception& ex) { + maximumAmbientCacheSize = previousMaximumAmbientCacheSize; + handleError(ex, "set maximum ambient cache size"); + + return std::current_exception(); + } +} + void OfflineDatabase::setOfflineMapboxTileCountLimit(uint64_t limit) { offlineMapboxTileCountLimit = limit; } |