diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 4f4c7136c6a..8fc8ed50df1 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -121,6 +121,11 @@ class DefaultFileSource : public FileSource { * Eviction works by removing the least-recently requested resources not also required * by other regions, until the database shrinks below a certain size. * + * 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()`. It is a useful optimization + * e.g. when several regions should be deleted in a row. + * * 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 * region. @@ -129,7 +134,7 @@ class DefaultFileSource : public FileSource { * 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 deleteOfflineRegion(OfflineRegion&&, std::function); + void deleteOfflineRegion(OfflineRegion&&, std::function, bool pack = true); /* * Invalidate all the tiles from an offline region forcing Mapbox GL to revalidate @@ -137,7 +142,7 @@ class DefaultFileSource : public FileSource { * offline region and downloading it again because if the data on the cache matches * the server, no new data gets transmitted. */ - void invalidateOfflineRegion(OfflineRegion&, std::function); + void invalidateOfflineRegion(OfflineRegion&, std::function); /* * Changing or bypassing this limit without permission from Mapbox is prohibited @@ -179,7 +184,16 @@ class DefaultFileSource : public FileSource { * 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 resetDatabase(std::function); + void resetDatabase(std::function); + + /* + * Packs the existing database file into a minimal amount of disk space. + * + * 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 packDatabase(std::function callback); /* * Forces revalidation of the ambient cache. diff --git a/platform/android/CHANGELOG.md b/platform/android/CHANGELOG.md index 092c974a47f..178ad12c351 100644 --- a/platform/android/CHANGELOG.md +++ b/platform/android/CHANGELOG.md @@ -14,6 +14,9 @@ Mapbox welcomes participation and contributions from everyone. If you'd like to - Convert GeoJSON features to tiles in a background thread and thus unblock the UI thread on updating the GeoJsonSource [#15871](https://github.com/mapbox/mapbox-gl-native/pull/15871) - Convert GeoJSON features to tiles for the loaded source description in a background thread and thus unblock the UI thread [#15885](https://github.com/mapbox/mapbox-gl-native/pull/15885) +### Features +- Introduce `OfflineManager#packDatabase` and `OfflineRegion#deleteAndSkipPackDatabase` API in order to decouple offline storage vacuum and delete region operations and thus to gain performance benefits e.g. when several regions should be deleted in a row [#15899](https://github.com/mapbox/mapbox-gl-native/pull/15899) + ## 8.5.0-alpha.2 - October 10, 2019 [Changes](https://github.com/mapbox/mapbox-gl-native/compare/android-v8.5.0-alpha.1...android-v8.5.0-alpha.2) since [Mapbox Maps SDK for Android v8.5.0-alpha.1](https://github.com/mapbox/mapbox-gl-native/releases/tag/android-v8.5.0-alpha.1): diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java index faed46662ab..0051c17e030 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java @@ -306,6 +306,47 @@ public void run() { }); } + /** + * Packs the existing database file into a minimal amount of disk space. + *

+ * 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. + *

+ * + * @param callback the callback to be invoked when the database was reset or when the operation erred. + */ + public void packDatabase(@Nullable final FileSourceCallback callback) { + fileSource.activate(); + nativePackDatabase(new FileSourceCallback() { + @Override + public void onSuccess() { + handler.post(new Runnable() { + @Override + public void run() { + fileSource.deactivate(); + if (callback != null) { + callback.onSuccess(); + } + } + }); + } + + @Override + public void onError(@NonNull final String message) { + handler.post(new Runnable() { + @Override + public void run() { + fileSource.deactivate(); + if (callback != null) { + callback.onError(message); + } + } + }); + } + }); + } + /** * Forces re-validation of the ambient cache. *

@@ -648,6 +689,9 @@ private native void createOfflineRegion(FileSource fileSource, OfflineRegionDefi @Keep private native void nativeResetDatabase(@Nullable FileSourceCallback callback); + @Keep + private native void nativePackDatabase(@Nullable FileSourceCallback callback); + @Keep private native void nativeInvalidateAmbientCache(@Nullable FileSourceCallback callback); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineRegion.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineRegion.java index f13128da632..b188d60a9ec 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineRegion.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineRegion.java @@ -401,7 +401,55 @@ public void delete(@NonNull final OfflineRegionDeleteCallback callback) { if (!isDeleted) { isDeleted = true; fileSource.activate(); - deleteOfflineRegion(new OfflineRegionDeleteCallback() { + deleteOfflineRegion(true /*pack*/, new OfflineRegionDeleteCallback() { + @Override + public void onDelete() { + handler.post(new Runnable() { + @Override + public void run() { + fileSource.deactivate(); + callback.onDelete(); + OfflineRegion.this.finalize(); + } + }); + } + + @Override + public void onError(final String error) { + handler.post(new Runnable() { + @Override + public void run() { + isDeleted = false; + fileSource.deactivate(); + callback.onError(error); + } + }); + } + }); + } + } + + /** + * Same as {@link OfflineRegion#delete} but skipping database file packing for performance reasons. + *

+ * Database file packing can be done later with {@link OfflineManager#packDatabase}. + * This method is a useful optimization e.g. when several regions should be deleted in a row. + *

+ *

+ * When the operation is complete or encounters an error, the given callback will be + * executed on the main thread. + *

+ *

+ * After you call this method, you may not call any additional methods on this object. + *

+ * + * @param callback the callback to be invoked + */ + public void deleteAndSkipPackDatabase(@NonNull final OfflineRegionDeleteCallback callback) { + if (!isDeleted) { + isDeleted = true; + fileSource.activate(); + deleteOfflineRegion(false /*pack*/, new OfflineRegionDeleteCallback() { @Override public void onDelete() { handler.post(new Runnable() { @@ -521,7 +569,7 @@ public void run() { private native void getOfflineRegionStatus(OfflineRegionStatusCallback callback); @Keep - private native void deleteOfflineRegion(OfflineRegionDeleteCallback callback); + private native void deleteOfflineRegion(boolean pack, OfflineRegionDeleteCallback callback); @Keep private native void updateOfflineRegionMetadata(byte[] metadata, OfflineRegionUpdateMetadataCallback callback); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt index a3214c9f114..da4cfbaf018 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt @@ -85,4 +85,20 @@ class CacheTest { } countDownLatch.await() } + + @Test + fun testSetPackDatabase() { + rule.activity.runOnUiThread { + OfflineManager.getInstance(context).packDatabase(object : OfflineManager.FileSourceCallback { + override fun onSuccess() { + countDownLatch.countDown() + } + + override fun onError(message: String) { + Assert.assertNull("onError should not be called", message) + } + }) + } + countDownLatch.await() + } } \ No newline at end of file diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index 029252f7867..c45386e3a78 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -7,8 +7,21 @@ namespace mbgl { namespace android { -// OfflineManager // +namespace { +// Reattach, the callback comes from a different thread +void handleException(std::exception_ptr exception, + const jni::Object& callback, + android::UniqueEnv env = android::AttachEnv()) { + if (exception) { + OfflineManager::FileSourceCallback::onError( + *env, callback, jni::Make(*env, mbgl::util::toString(exception))); + } else { + OfflineManager::FileSourceCallback::onSuccess(*env, callback); + } +} +} // namespace +// OfflineManager // OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object& jFileSource) : fileSource(std::static_pointer_cast(mbgl::FileSource::getSharedFileSource(FileSource::getSharedResourceOptions(env, jFileSource)))) {} @@ -107,77 +120,52 @@ void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object& callback_) { auto globalCallback = jni::NewGlobal(env_, callback_); - fileSource->resetDatabase([ - //Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile - callback = std::make_shared(std::move(globalCallback)) - ](std::exception_ptr exception) mutable { + fileSource->resetDatabase( + [ + // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile + callback = std::make_shared(std::move(globalCallback))]( + std::exception_ptr exception) mutable { handleException(exception, *callback); }); +} - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); +void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object& callback_) { + auto globalCallback = jni::NewGlobal(env_, callback_); - if (exception) { - OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make(*env, mbgl::util::toString(exception))); - } else { - OfflineManager::FileSourceCallback::onSuccess(*env, *callback); - } - }); + fileSource->packDatabase( + [ + // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile + callback = std::make_shared(std::move(globalCallback))]( + std::exception_ptr exception) mutable { handleException(exception, *callback); }); } void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object& callback_) { auto globalCallback = jni::NewGlobal(env_, callback_); - fileSource->invalidateAmbientCache([ - //Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile - callback = std::make_shared(std::move(globalCallback)) - ](std::exception_ptr exception) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (exception) { - OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make(*env, mbgl::util::toString(exception))); - } else { - OfflineManager::FileSourceCallback::onSuccess(*env, *callback); - } - }); + fileSource->invalidateAmbientCache( + [ + // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile + callback = std::make_shared(std::move(globalCallback))]( + std::exception_ptr exception) mutable { handleException(exception, *callback); }); } void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object& callback_) { auto globalCallback = jni::NewGlobal(env_, callback_); - fileSource->clearAmbientCache([ - //Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile - callback = std::make_shared(std::move(globalCallback)) - ](std::exception_ptr exception) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (exception) { - OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make(*env, mbgl::util::toString(exception))); - } else { - OfflineManager::FileSourceCallback::onSuccess(*env, *callback); - } - }); + fileSource->clearAmbientCache( + [ + // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile + callback = std::make_shared(std::move(globalCallback))]( + std::exception_ptr exception) mutable { handleException(exception, *callback); }); } void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jlong size_, const jni::Object& callback_) { auto globalCallback = jni::NewGlobal(env_, callback_); - fileSource->setMaximumAmbientCacheSize(size_, [ - //Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile - callback = std::make_shared(std::move(globalCallback)) - ](std::exception_ptr exception) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (exception) { - OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make(*env, mbgl::util::toString(exception))); - } else { - OfflineManager::FileSourceCallback::onSuccess(*env, *callback); - } - }); + fileSource->setMaximumAmbientCacheSize( + size_, + [ + // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile + callback = std::make_shared(std::move(globalCallback))]( + std::exception_ptr exception) mutable { handleException(exception, *callback); }); } // FileSource::FileSourceCallback // @@ -207,7 +195,10 @@ void OfflineManager::registerNative(jni::JNIEnv& env) { #define METHOD(MethodPtr, name) jni::MakeNativePeerMethod(name) - jni::RegisterNativePeer( env, javaClass, "nativePtr", + jni::RegisterNativePeer( + env, + javaClass, + "nativePtr", jni::MakePeer&>, "initialize", "finalize", @@ -216,6 +207,7 @@ void OfflineManager::registerNative(jni::JNIEnv& env) { METHOD(&OfflineManager::createOfflineRegion, "createOfflineRegion"), METHOD(&OfflineManager::mergeOfflineRegions, "mergeOfflineRegions"), METHOD(&OfflineManager::resetDatabase, "nativeResetDatabase"), + METHOD(&OfflineManager::packDatabase, "nativePackDatabase"), METHOD(&OfflineManager::invalidateAmbientCache, "nativeInvalidateAmbientCache"), METHOD(&OfflineManager::clearAmbientCache, "nativeClearAmbientCache"), METHOD(&OfflineManager::setMaximumAmbientCacheSize, "nativeSetMaximumAmbientCacheSize"), diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index 058cfb5b484..64e00f91fc2 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -95,6 +95,8 @@ class OfflineManager { void resetDatabase(jni::JNIEnv&, const jni::Object& callback_); + void packDatabase(jni::JNIEnv&, const jni::Object& callback_); + void invalidateAmbientCache(jni::JNIEnv&, const jni::Object& callback_); void clearAmbientCache(jni::JNIEnv&, const jni::Object& callback_); diff --git a/platform/android/src/offline/offline_region.cpp b/platform/android/src/offline/offline_region.cpp index ac9f491ab6f..264b8216364 100644 --- a/platform/android/src/offline/offline_region.cpp +++ b/platform/android/src/offline/offline_region.cpp @@ -101,40 +101,51 @@ void OfflineRegion::getOfflineRegionStatus(jni::JNIEnv& env_, const jni::Object< }); } -void OfflineRegion::deleteOfflineRegion(jni::JNIEnv& env_, const jni::Object& callback_) { - auto globalCallback = jni::NewGlobal(env_, callback_); +namespace { +// Reattach, the callback comes from a different thread +void handleException(std::exception_ptr exception, + const jni::Object& callback, + android::UniqueEnv env = android::AttachEnv()) { + if (exception) { + OfflineRegion::OfflineRegionDeleteCallback::onError(*env, callback, exception); + } else { + OfflineRegion::OfflineRegionDeleteCallback::onDelete(*env, callback); + } +} +} // namespace - fileSource->deleteOfflineRegion(std::move(*region), [ - //Ensure the object is not gc'd in the meanwhile - callback = std::make_shared(std::move(globalCallback)) - ](std::exception_ptr error) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); +void OfflineRegion::deleteOfflineRegion(jni::JNIEnv& env_, + jni::jboolean pack, + const jni::Object& callback_) { + auto globalCallback = jni::NewGlobal(env_, callback_); - if (error) { - OfflineRegionDeleteCallback::onError(*env, *callback, error); - } else { - OfflineRegionDeleteCallback::onDelete(*env, *callback); - } - }); + fileSource->deleteOfflineRegion( + std::move(*region), + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared(std::move(globalCallback))]( + std::exception_ptr error) mutable { handleException(error, *callback); }, + pack); } -void OfflineRegion::invalidateOfflineRegion(jni::JNIEnv& env_, const jni::Object& callback_) { +void OfflineRegion::invalidateOfflineRegion(jni::JNIEnv& env_, + const jni::Object& callback_) { auto globalCallback = jni::NewGlobal(env_, callback_); - fileSource->invalidateOfflineRegion(*region, [ - //Ensure the object is not gc'd in the meanwhile - callback = std::make_shared(std::move(globalCallback)) - ](std::exception_ptr error) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (error) { - OfflineRegionInvalidateCallback::onError(*env, *callback, error); - } else { - OfflineRegionInvalidateCallback::onInvalidate(*env, *callback); - } - }); + fileSource->invalidateOfflineRegion(*region, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared( + std::move(globalCallback))](std::exception_ptr error) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (error) { + OfflineRegionInvalidateCallback::onError(*env, *callback, error); + } else { + OfflineRegionInvalidateCallback::onInvalidate(*env, *callback); + } + }); } void OfflineRegion::updateOfflineRegionMetadata(jni::JNIEnv& env_, const jni::Array& jMetadata, const jni::Object& callback_) { @@ -206,7 +217,10 @@ void OfflineRegion::registerNative(jni::JNIEnv& env) { #define METHOD(MethodPtr, name) jni::MakeNativePeerMethod(name) - jni::RegisterNativePeer( env, javaClass, "nativePtr", + jni::RegisterNativePeer( + env, + javaClass, + "nativePtr", jni::MakePeer&>, "initialize", "finalize", @@ -215,8 +229,7 @@ void OfflineRegion::registerNative(jni::JNIEnv& env) { METHOD(&OfflineRegion::getOfflineRegionStatus, "getOfflineRegionStatus"), METHOD(&OfflineRegion::deleteOfflineRegion, "deleteOfflineRegion"), METHOD(&OfflineRegion::invalidateOfflineRegion, "invalidateOfflineRegion"), - METHOD(&OfflineRegion::updateOfflineRegionMetadata, "updateOfflineRegionMetadata") - ); + METHOD(&OfflineRegion::updateOfflineRegionMetadata, "updateOfflineRegionMetadata")); } // OfflineRegionObserver // @@ -227,7 +240,7 @@ void OfflineRegion::OfflineRegionStatusCallback::onError(jni::JNIEnv& env, const jni::Object& callback, std::exception_ptr error) { static auto& javaClass = jni::Class::Singleton(env); - static auto method = javaClass.GetMethod(env, "onError"); + static auto method = javaClass.GetMethod(env, "onError"); callback.Call(env, method, jni::Make(env, mbgl::util::toString(error))); } diff --git a/platform/android/src/offline/offline_region.hpp b/platform/android/src/offline/offline_region.hpp index dda253469e0..2c92951f696 100644 --- a/platform/android/src/offline/offline_region.hpp +++ b/platform/android/src/offline/offline_region.hpp @@ -69,7 +69,7 @@ class OfflineRegion { void getOfflineRegionStatus(jni::JNIEnv&, const jni::Object&); - void deleteOfflineRegion(jni::JNIEnv&, const jni::Object&); + void deleteOfflineRegion(jni::JNIEnv&, jni::jboolean pack, const jni::Object&); void invalidateOfflineRegion(jni::JNIEnv&, const jni::Object&); diff --git a/platform/default/include/mbgl/storage/offline_database.hpp b/platform/default/include/mbgl/storage/offline_database.hpp index f097cd54406..ac997bf6adb 100644 --- a/platform/default/include/mbgl/storage/offline_database.hpp +++ b/platform/default/include/mbgl/storage/offline_database.hpp @@ -74,7 +74,7 @@ class OfflineDatabase : private util::noncopyable { expected updateMetadata(const int64_t regionID, const OfflineRegionMetadata&); - std::exception_ptr deleteRegion(OfflineRegion&&); + std::exception_ptr deleteRegion(OfflineRegion&&, bool pack = true); std::exception_ptr invalidateRegion(int64_t regionID); // Return value is (response, stored size) @@ -93,6 +93,7 @@ class OfflineDatabase : private util::noncopyable { uint64_t getOfflineMapboxTileCount(); bool exceedsOfflineMapboxTileCountLimit(const Resource&); void markUsedResources(int64_t regionID, const std::list&); + std::exception_ptr pack(); private: void initialize(); diff --git a/platform/default/src/mbgl/storage/default_file_source.cpp b/platform/default/src/mbgl/storage/default_file_source.cpp index dc4b2908a8c..3982f3dbc77 100644 --- a/platform/default/src/mbgl/storage/default_file_source.cpp +++ b/platform/default/src/mbgl/storage/default_file_source.cpp @@ -82,9 +82,9 @@ class DefaultFileSource::Impl { } } - void deleteRegion(OfflineRegion&& region, std::function callback) { + void deleteRegion(OfflineRegion&& region, std::function callback, bool pack) { downloads.erase(region.getID()); - callback(offlineDatabase->deleteRegion(std::move(region))); + callback(offlineDatabase->deleteRegion(std::move(region), pack)); } void invalidateRegion(int64_t regionID, std::function callback) { @@ -200,6 +200,8 @@ class DefaultFileSource::Impl { callback(offlineDatabase->setMaximumAmbientCacheSize(size)); } + void packDatabase(std::function callback) { callback(offlineDatabase->pack()); } + private: expected getDownload(int64_t regionID) { auto it = downloads.find(regionID); @@ -308,11 +310,14 @@ void DefaultFileSource::updateOfflineMetadata(const int64_t regionID, impl->actor().invoke(&Impl::updateMetadata, regionID, metadata, callback); } -void DefaultFileSource::deleteOfflineRegion(OfflineRegion&& region, std::function callback) { - impl->actor().invoke(&Impl::deleteRegion, std::move(region), callback); +void DefaultFileSource::deleteOfflineRegion(OfflineRegion&& region, + std::function callback, + bool pack) { + impl->actor().invoke(&Impl::deleteRegion, std::move(region), callback, pack); } -void DefaultFileSource::invalidateOfflineRegion(OfflineRegion& region, std::function callback) { +void DefaultFileSource::invalidateOfflineRegion(OfflineRegion& region, + std::function callback) { impl->actor().invoke(&Impl::invalidateRegion, region.getID(), callback); } @@ -348,11 +353,15 @@ void DefaultFileSource::resetDatabase(std::function c impl->actor().invoke(&Impl::resetDatabase, std::move(callback)); } +void DefaultFileSource::packDatabase(std::function callback) { + impl->actor().invoke(&Impl::packDatabase, std::move(callback)); +} + void DefaultFileSource::invalidateAmbientCache(std::function callback) { impl->actor().invoke(&Impl::invalidateAmbientCache, std::move(callback)); } -void DefaultFileSource::clearAmbientCache(std::function callback) { +void DefaultFileSource::clearAmbientCache(std::function callback) { impl->actor().invoke(&Impl::clearAmbientCache, std::move(callback)); } diff --git a/platform/default/src/mbgl/storage/offline_database.cpp b/platform/default/src/mbgl/storage/offline_database.cpp index 4f813e906d5..fef524ce570 100644 --- a/platform/default/src/mbgl/storage/offline_database.cpp +++ b/platform/default/src/mbgl/storage/offline_database.cpp @@ -197,6 +197,7 @@ void OfflineDatabase::migrateToVersion6() { } void OfflineDatabase::vacuum() { + assert(db); if (getPragma("PRAGMA auto_vacuum") != 2 /*INCREMENTAL*/) { db->exec("PRAGMA auto_vacuum = INCREMENTAL"); db->exec("VACUUM"); @@ -883,7 +884,7 @@ OfflineDatabase::updateMetadata(const int64_t regionID, const OfflineRegionMetad return unexpected(std::current_exception()); } -std::exception_ptr OfflineDatabase::deleteRegion(OfflineRegion&& region) try { +std::exception_ptr OfflineDatabase::deleteRegion(OfflineRegion&& region, bool pack) try { { mapbox::sqlite::Query query{ getStatement("DELETE FROM regions WHERE id = ?") }; query.bind(1, region.getID()); @@ -892,10 +893,10 @@ std::exception_ptr OfflineDatabase::deleteRegion(OfflineRegion&& region) try { evict(0); assert(db); - vacuum(); + if (pack) vacuum(); // Ensure that the cached offlineTileCount value is recalculated. - offlineMapboxTileCount = {}; + offlineMapboxTileCount = nullopt; return nullptr; } catch (...) { handleError("delete region"); @@ -1303,6 +1304,15 @@ void OfflineDatabase::markUsedResources(int64_t regionID, const std::list> generateResources(const std::string& tilePrefix, + const std::string& stylePrefix) { + static const auto responseData = randomString(.5 * 1024 * 1024); + Response response; + response.data = responseData; + std::list> resources; + for (unsigned i = 0; i < 50; ++i) { + resources.emplace_back(Resource::tile(tilePrefix + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ), + response); + resources.emplace_back(Resource::style(stylePrefix + std::to_string(i)), response); + } + return resources; +} +} // namespace + TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DeleteRegion)) { FixtureLog log; deleteDatabaseFiles(); @@ -677,25 +693,35 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DeleteRegion)) { OfflineTilePyramidRegionDefinition definition{ "mapbox://style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true }; OfflineRegionMetadata metadata{{ 1, 2, 3 }}; - auto region = db.createRegion(definition, metadata); + auto region1 = db.createRegion(definition, metadata); + auto region2 = db.createRegion(definition, metadata); - 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.putRegionResource(region->getID(), tile, response); + OfflineRegionStatus status; + db.putRegionResources(region1->getID(), generateResources("mapbox://tile_1", "mapbox://style_1"), status); + db.putRegionResources(region2->getID(), generateResources("mapbox://tile_2", "mapbox://style_2"), status); + const size_t sizeWithTwoRegions = util::read_file(filename).size(); - const Resource style = Resource::style("mapbox://style_" + std::to_string(i)); - db.putRegionResource(region->getID(), style, response); - } + db.deleteRegion(std::move(*region1), false /*pack*/); - db.deleteRegion(std::move(*region)); + ASSERT_EQ(1u, db.listRegions().value().size()); + // Region is removed but the size of the database is the same. + EXPECT_EQ(sizeWithTwoRegions, util::read_file(filename).size()); - auto regions = db.listRegions().value(); - ASSERT_EQ(0u, regions.size()); + db.pack(); + // The size of the database has shrunk after pack(). + const size_t sizeWithOneRegion = util::read_file(filename).size(); + EXPECT_LT(sizeWithOneRegion, sizeWithTwoRegions); + + db.deleteRegion(std::move(*region2)); + // The size of the database has shrunk right away. + const size_t sizeWithoutRegions = util::read_file(filename).size(); + ASSERT_EQ(0u, db.listRegions().value().size()); + EXPECT_LT(sizeWithoutRegions, sizeWithOneRegion); // The tiles from the offline region will migrate to the // ambient cache and shrink the database to the maximum // size defined by default. - EXPECT_LE(util::read_file(filename).size(), util::DEFAULT_MAX_CACHE_SIZE); + EXPECT_LE(sizeWithoutRegions, util::DEFAULT_MAX_CACHE_SIZE); // After clearing the cache, the size of the database // should get back to the original size.