summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-22 18:09:24 +0200
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-22 18:10:11 +0200
commit7724d3028daf27734c8fde36bc42193556588581 (patch)
treecae557eee14518c5a9b8598e67796b3fbb8fa104
parent17a544176807538c9e7a85048b81b014e97566e9 (diff)
downloadqtlocation-mapboxgl-7724d3028daf27734c8fde36bc42193556588581.tar.gz
[core] Clearing ambient cache can skip vacuum
-rw-r--r--include/mbgl/storage/default_file_source.hpp15
-rw-r--r--platform/default/include/mbgl/storage/offline_database.hpp8
-rw-r--r--platform/default/src/mbgl/storage/default_file_source.cpp8
-rw-r--r--platform/default/src/mbgl/storage/offline_database.cpp4
-rw-r--r--test/storage/offline_database.test.cpp29
5 files changed, 50 insertions, 14 deletions
diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp
index 88f318581b..e3b5bc9fbc 100644
--- a/include/mbgl/storage/default_file_source.hpp
+++ b/include/mbgl/storage/default_file_source.hpp
@@ -212,14 +212,21 @@ public:
/*
* Erase resources from the ambient cache, freeing storage space.
*
- * Erases the ambient 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.
+ * Erases the ambient cache, freeing resources.
+ *
+ * If the |pack| argument is `true`, this operation includes database
+ * file packing. This operation can be potentially slow because it triggers
+ * a VACUUM on SQLite, forcing the database to move pages on the
+ * filesystem.
+ *
+ * If the |pack| argument is `false` the database file packing is skipped and the
+ * database file does not shrink after this operation completes. Database file
+ * packing can be done later with `packDatabase()`.
*
* Resources overlapping with offline regions will not be affected
* by this call.
*/
- void clearAmbientCache(std::function<void (std::exception_ptr)>);
+ void clearAmbientCache(std::function<void(std::exception_ptr)>, bool pack = true);
/*
* Sets the maximum size in bytes for the ambient cache.
diff --git a/platform/default/include/mbgl/storage/offline_database.hpp b/platform/default/include/mbgl/storage/offline_database.hpp
index ac997bf6ad..717d232e32 100644
--- a/platform/default/include/mbgl/storage/offline_database.hpp
+++ b/platform/default/include/mbgl/storage/offline_database.hpp
@@ -58,10 +58,10 @@ public:
// lookup, it will not get downloaded again.
std::exception_ptr invalidateAmbientCache();
- // Clear 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.
- std::exception_ptr clearAmbientCache();
+ // Clear the tile cache, freeing resources. If |pack| is `true`,
+ // this operation can be slow because it will trigger a VACUUM on
+ // SQLite, forcing the database to move pages on the filesystem.
+ std::exception_ptr clearAmbientCache(bool pack = true);
expected<OfflineRegions, std::exception_ptr> listRegions();
diff --git a/platform/default/src/mbgl/storage/default_file_source.cpp b/platform/default/src/mbgl/storage/default_file_source.cpp
index 36acb748de..c476ab3886 100644
--- a/platform/default/src/mbgl/storage/default_file_source.cpp
+++ b/platform/default/src/mbgl/storage/default_file_source.cpp
@@ -198,8 +198,8 @@ public:
callback(offlineDatabase->invalidateAmbientCache());
}
- void clearAmbientCache(std::function<void (std::exception_ptr)> callback) {
- callback(offlineDatabase->clearAmbientCache());
+ void clearAmbientCache(std::function<void(std::exception_ptr)> callback, bool pack) {
+ callback(offlineDatabase->clearAmbientCache(pack));
}
void setMaximumAmbientCacheSize(uint64_t size, std::function<void (std::exception_ptr)> callback) {
@@ -367,8 +367,8 @@ void DefaultFileSource::invalidateAmbientCache(std::function<void (std::exceptio
impl->actor().invoke(&Impl::invalidateAmbientCache, std::move(callback));
}
-void DefaultFileSource::clearAmbientCache(std::function<void(std::exception_ptr)> callback) {
- impl->actor().invoke(&Impl::clearAmbientCache, std::move(callback));
+void DefaultFileSource::clearAmbientCache(std::function<void(std::exception_ptr)> callback, bool pack) {
+ impl->actor().invoke(&Impl::clearAmbientCache, std::move(callback), pack);
}
void DefaultFileSource::setMaximumAmbientCacheSize(uint64_t size, std::function<void (std::exception_ptr)> callback) {
diff --git a/platform/default/src/mbgl/storage/offline_database.cpp b/platform/default/src/mbgl/storage/offline_database.cpp
index fef524ce57..c9b74a71f7 100644
--- a/platform/default/src/mbgl/storage/offline_database.cpp
+++ b/platform/default/src/mbgl/storage/offline_database.cpp
@@ -682,7 +682,7 @@ std::exception_ptr OfflineDatabase::invalidateAmbientCache() try {
return std::current_exception();
}
-std::exception_ptr OfflineDatabase::clearAmbientCache() try {
+std::exception_ptr OfflineDatabase::clearAmbientCache(bool pack) try {
// clang-format off
mapbox::sqlite::Query tileQuery{ getStatement(
"DELETE FROM tiles "
@@ -705,7 +705,7 @@ std::exception_ptr OfflineDatabase::clearAmbientCache() try {
resourceQuery.run();
- vacuum();
+ if (pack) vacuum();
return nullptr;
} catch (...) {
diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp
index 9d798d0a62..9db344d89c 100644
--- a/test/storage/offline_database.test.cpp
+++ b/test/storage/offline_database.test.cpp
@@ -952,6 +952,35 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(ClearAmbientCache)) {
EXPECT_EQ(0u, log.uncheckedCount());
}
+TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Pack)) {
+ FixtureLog log;
+ deleteDatabaseFiles();
+
+ OfflineDatabase db(filename);
+ size_t initialSize = util::read_file(filename).size();
+
+ Response response;
+ response.data = randomString(.5 * 1024 * 1024);
+
+ for (unsigned i = 0; i < 50; ++i) {
+ const Resource tile = Resource::tile("mapbox://tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
+ db.put(tile, response);
+
+ const Resource style = Resource::style("mapbox://style_" + std::to_string(i));
+ db.put(style, response);
+ }
+ size_t populatedSize = util::read_file(filename).size();
+ ASSERT_GT(populatedSize, initialSize);
+
+ db.clearAmbientCache(false /*pack*/);
+ EXPECT_EQ(populatedSize, util::read_file(filename).size());
+ EXPECT_EQ(0u, log.uncheckedCount());
+
+ db.pack();
+ EXPECT_EQ(initialSize, util::read_file(filename).size());
+ EXPECT_EQ(0u, log.uncheckedCount());
+}
+
TEST(OfflineDatabase, CreateRegionInfiniteMaxZoom) {
FixtureLog log;
OfflineDatabase db(":memory:");