From e373d8a5924e4f4cf3904ecacbf1d1cf86a5d60f Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Mon, 25 Nov 2019 15:17:37 +0200 Subject: [PATCH] [android] Add OfflineManager.runPackDatabaseAutomatically(boolean) API --- .../mapboxsdk/offline/OfflineManager.java | 30 ++++++++++++++++--- .../mapboxsdk/offline/OfflineRegion.java | 4 +++ .../android/src/offline/offline_manager.cpp | 5 ++++ .../android/src/offline/offline_manager.hpp | 2 ++ 4 files changed, 37 insertions(+), 4 deletions(-) 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 0051c17e030..a7bb9a99850 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 @@ -309,6 +309,9 @@ public void run() { /** * Packs the existing database file into a minimal amount of disk space. *

+ * This operation has a performance impact as it will vacuum the database, + * forcing it to move pages on the filesystem. + *

* 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. @@ -396,9 +399,10 @@ public void run() { /** * 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. + *

+ * Note that this operation can be potentially slow if packing the database + * occurs automatically ({@link OfflineManager#runPackDatabaseAutomatically(boolean)}). *

*

* Resources overlapping with offline regions will not be affected @@ -661,7 +665,7 @@ private boolean isValidOfflineRegionDefinition(OfflineRegionDefinition definitio *

* Once this limit is reached, {@link OfflineRegion.OfflineRegionObserver#mapboxTileCountLimitExceeded(long)} * fires every additional attempt to download additional tiles until already downloaded tiles are removed - * by calling {@link OfflineRegion#deleteOfflineRegion(OfflineRegion.OfflineRegionDeleteCallback)}. + * by calling {@link OfflineRegion#delete(OfflineRegion.OfflineRegionDeleteCallback)}. *

* * @param limit the maximum number of tiles allowed to be downloaded @@ -669,6 +673,24 @@ private boolean isValidOfflineRegionDefinition(OfflineRegionDefinition definitio @Keep public native void setOfflineMapboxTileCountLimit(long limit); + /** + * Sets whether database file packing occurs automatically. + * By default, the automatic database file packing is enabled. + *

+ * If packing is enabled, database file packing occurs automatically + * after an offline region is deleted by calling + * {@link OfflineRegion#delete(OfflineRegion.OfflineRegionDeleteCallback)} + * or the ambient cache is cleared by calling {@link OfflineManager#clearAmbientCache()}. + * + * If packing is disabled, disk space will not be freed after + * resources are removed unless {@link OfflineManager#packDatabase()} is explicitly called. + *

+ * + * @param autopack flag setting the automatic database file packing. + */ + @Keep + public native void runPackDatabaseAutomatically(boolean autopack); + @Keep private native void initialize(FileSource fileSource); 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..d116535c5a6 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 @@ -388,6 +388,10 @@ public void run() { * by other regions, until the database shrinks below a certain size. *

*

+ * Note that this operation can be potentially slow if packing the database + * occurs automatically ({@link OfflineManager#runPackDatabaseAutomatically(boolean)}). + *

+ *

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

diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index c45386e3a78..be864e18aa7 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -168,6 +168,10 @@ void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jl std::exception_ptr exception) mutable { handleException(exception, *callback); }); } +void OfflineManager::runPackDatabaseAutomatically(jni::JNIEnv&, jboolean autopack) { + fileSource->runPackDatabaseAutomatically(autopack); +} + // FileSource::FileSourceCallback // void OfflineManager::FileSourceCallback::onSuccess(jni::JNIEnv& env, @@ -211,6 +215,7 @@ void OfflineManager::registerNative(jni::JNIEnv& env) { METHOD(&OfflineManager::invalidateAmbientCache, "nativeInvalidateAmbientCache"), METHOD(&OfflineManager::clearAmbientCache, "nativeClearAmbientCache"), METHOD(&OfflineManager::setMaximumAmbientCacheSize, "nativeSetMaximumAmbientCacheSize"), + METHOD(&OfflineManager::runPackDatabaseAutomatically, "runPackDatabaseAutomatically"), METHOD(&OfflineManager::putResourceWithUrl, "putResourceWithUrl")); } diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index 64e00f91fc2..0af92f81158 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -103,6 +103,8 @@ class OfflineManager { void setMaximumAmbientCacheSize(jni::JNIEnv&, const jni::jlong size, const jni::Object& callback_); + void runPackDatabaseAutomatically(jni::JNIEnv&, jboolean autopack); + private: std::shared_ptr fileSource; };