diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-02-27 11:39:01 +0200 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-02-27 12:21:50 +0200 |
commit | 382a6000da3e77fe32da060380b584a548c2e2dd (patch) | |
tree | 2ad662dda93489047499fca6be37ee939783dc5c | |
parent | 56616ad4ec15e08dc49fd5c24f9c6026058055af (diff) | |
download | qtlocation-mapboxgl-upstream/mikhail_improve_DatabaseFileSource_API.tar.gz |
[glfw][android][darwin] Access DatabaseFileSource only with JointDatabaseStorage interfaceupstream/mikhail_improve_DatabaseFileSource_API
-rw-r--r-- | bin/offline.cpp | 45 | ||||
-rw-r--r-- | include/mbgl/storage/database_file_source.hpp | 1 | ||||
-rw-r--r-- | platform/android/src/file_source.cpp | 7 | ||||
-rw-r--r-- | platform/android/src/file_source.hpp | 2 | ||||
-rw-r--r-- | platform/android/src/offline/offline_manager.cpp | 126 | ||||
-rw-r--r-- | platform/android/src/offline/offline_manager.hpp | 2 | ||||
-rw-r--r-- | platform/android/src/offline/offline_region.cpp | 125 | ||||
-rw-r--r-- | platform/android/src/offline/offline_region.hpp | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflinePack.mm | 14 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 26 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage_Private.h | 4 | ||||
-rw-r--r-- | platform/glfw/main.cpp | 9 | ||||
-rw-r--r-- | test/storage/main_resource_loader.test.cpp | 7 |
13 files changed, 186 insertions, 184 deletions
diff --git a/bin/offline.cpp b/bin/offline.cpp index 97b60c223f..a7a7794ca6 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -163,29 +163,28 @@ int main(int argc, char *argv[]) { util::RunLoop loop; - std::shared_ptr<DatabaseFileSource> fileSource = std::static_pointer_cast<DatabaseFileSource>( - std::shared_ptr<FileSource>(FileSourceManager::get()->getFileSource( - FileSourceType::Database, - ResourceOptions().withAccessToken(token).withBaseURL(apiBaseURL).withCachePath(output)))); + std::shared_ptr<JointDatabaseStorage> databaseStorage = FileSourceManager::get()->getDatabaseStorage( + ResourceOptions().withAccessToken(token).withBaseURL(apiBaseURL).withCachePath(output)); std::unique_ptr<OfflineRegion> region; if (inputDb && mergePath) { DatabaseFileSource inputSource(ResourceOptions().withCachePath(*inputDb)); + JointDatabaseStorage* storage = &inputSource; int retCode = 0; std::cout << "Start Merge" << std::endl; - inputSource.mergeOfflineRegions(*mergePath, [&] (mbgl::expected<std::vector<OfflineRegion>, std::exception_ptr> result) { - - if (!result) { - std::cerr << "Error merging database: " << util::toString(result.error()) << std::endl; - retCode = 1; - } else { - std::cout << " Added " << result->size() << " Regions" << std::endl; - std::cout << "Finished Merge" << std::endl; - } - loop.stop(); - }); + storage->mergeOfflineRegions( + *mergePath, [&](mbgl::expected<std::vector<OfflineRegion>, std::exception_ptr> result) { + if (!result) { + std::cerr << "Error merging database: " << util::toString(result.error()) << std::endl; + retCode = 1; + } else { + std::cout << " Added " << result->size() << " Regions" << std::endl; + std::cout << "Finished Merge" << std::endl; + } + loop.stop(); + }); loop.run(); return retCode; } @@ -195,11 +194,11 @@ int main(int argc, char *argv[]) { class Observer : public OfflineRegionObserver { public: Observer(OfflineRegion& region_, - std::shared_ptr<DatabaseFileSource> fileSource_, + std::shared_ptr<JointDatabaseStorage> databaseStorage_, util::RunLoop& loop_, mbgl::optional<std::string> mergePath_) : region(region_), - fileSource(std::move(fileSource_)), + databaseStorage(std::move(databaseStorage_)), loop(loop_), mergePath(std::move(mergePath_)), start(util::now()) {} @@ -239,7 +238,7 @@ int main(int argc, char *argv[]) { } OfflineRegion& region; - std::shared_ptr<DatabaseFileSource> fileSource; + std::shared_ptr<JointDatabaseStorage> databaseStorage; util::RunLoop& loop; mbgl::optional<std::string> mergePath; Timestamp start; @@ -248,13 +247,13 @@ int main(int argc, char *argv[]) { static auto stop = [&] { if (region) { std::cout << "Stopping download... "; - fileSource->setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Inactive); + databaseStorage->setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Inactive); } }; std::signal(SIGINT, [] (int) { stop(); }); - fileSource->createOfflineRegion( + databaseStorage->createOfflineRegion( definition, metadata, [&](mbgl::expected<OfflineRegion, std::exception_ptr> region_) { if (!region_) { std::cerr << "Error creating region: " << util::toString(region_.error()) << std::endl; @@ -263,9 +262,9 @@ int main(int argc, char *argv[]) { } else { assert(region_); region = std::make_unique<OfflineRegion>(std::move(*region_)); - fileSource->setOfflineRegionObserver(*region, - std::make_unique<Observer>(*region, fileSource, loop, mergePath)); - fileSource->setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Active); + databaseStorage->setOfflineRegionObserver( + *region, std::make_unique<Observer>(*region, databaseStorage, loop, mergePath)); + databaseStorage->setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Active); } }); diff --git a/include/mbgl/storage/database_file_source.hpp b/include/mbgl/storage/database_file_source.hpp index 7f3d84a182..cc492709b8 100644 --- a/include/mbgl/storage/database_file_source.hpp +++ b/include/mbgl/storage/database_file_source.hpp @@ -231,6 +231,7 @@ public: explicit DatabaseFileSource(const ResourceOptions& options); ~DatabaseFileSource() override; +private: // JointDatabaseStorage overrides void setDatabasePath(const std::string&, std::function<void()> callback) override; void resetDatabase(std::function<void(std::exception_ptr)>) override; diff --git a/platform/android/src/file_source.cpp b/platform/android/src/file_source.cpp index f95066546a..c8e57e66ff 100644 --- a/platform/android/src/file_source.cpp +++ b/platform/android/src/file_source.cpp @@ -41,8 +41,7 @@ FileSource::FileSource(jni::JNIEnv& _env, const jni::String& accessToken, const // TODO: Split Android FileSource API to smaller interfaces resourceLoader = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::ResourceLoader, resourceOptions); - databaseSource = std::static_pointer_cast<mbgl::DatabaseFileSource>(std::shared_ptr<mbgl::FileSource>( - mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions))); + databaseStorage = mbgl::FileSourceManager::get()->getDatabaseStorage(resourceOptions); onlineSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions); } @@ -110,7 +109,7 @@ void FileSource::setResourceTransform(jni::JNIEnv& env, const jni::Object<FileSo void FileSource::setResourceCachePath(jni::JNIEnv& env, const jni::String& path, const jni::Object<FileSource::ResourcesCachePathChangeCallback>& _callback) { - if (!databaseSource) { + if (!databaseStorage) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); return; } @@ -132,7 +131,7 @@ void FileSource::setResourceCachePath(jni::JNIEnv& env, pathChangeCallback = {}; }); - databaseSource->setDatabasePath(newPath + DATABASE_FILE, pathChangeCallback); + databaseStorage->setDatabasePath(newPath + DATABASE_FILE, pathChangeCallback); } void FileSource::resume(jni::JNIEnv&) { diff --git a/platform/android/src/file_source.hpp b/platform/android/src/file_source.hpp index 3bf0fed2eb..a93a7d9182 100644 --- a/platform/android/src/file_source.hpp +++ b/platform/android/src/file_source.hpp @@ -74,7 +74,7 @@ private: mbgl::ResourceOptions resourceOptions; std::unique_ptr<Actor<ResourceTransform::TransformCallback>> resourceTransform; std::function<void()> pathChangeCallback; - std::shared_ptr<mbgl::DatabaseFileSource> databaseSource; + std::shared_ptr<mbgl::JointDatabaseStorage> databaseStorage; std::shared_ptr<mbgl::FileSource> onlineSource; std::shared_ptr<mbgl::FileSource> resourceLoader; }; diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index 51d2f70fe0..a5108203f7 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -25,10 +25,9 @@ void handleException(std::exception_ptr exception, // OfflineManager // OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource) - : fileSource(std::static_pointer_cast<mbgl::DatabaseFileSource>( - std::shared_ptr<mbgl::FileSource>(mbgl::FileSourceManager::get()->getFileSource( - mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource))))) { - if (!fileSource) { + : databaseStorage( + FileSourceManager::get()->getDatabaseStorage(FileSource::getSharedResourceOptions(env, jFileSource))) { + if (!databaseStorage) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); } } @@ -36,30 +35,29 @@ OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& OfflineManager::~OfflineManager() {} void OfflineManager::setOfflineMapboxTileCountLimit(jni::JNIEnv&, jni::jlong limit) { - fileSource->setOfflineMapboxTileCountLimit(limit); + databaseStorage->setOfflineMapboxTileCountLimit(limit); } void OfflineManager::listOfflineRegions(jni::JNIEnv& env_, const jni::Object<FileSource>& jFileSource_, const jni::Object<ListOfflineRegionsCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); - fileSource->listOfflineRegions([ - //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), - jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) - ](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (regions) { - OfflineManager::ListOfflineRegionsCallback::onList( - *env, *jFileSource, *callback, *regions); - } else { - OfflineManager::ListOfflineRegionsCallback::onError( - *env, *callback, regions.error()); - } - }); + databaseStorage->listOfflineRegions( + [ + // Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the + // meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource))]( + mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (regions) { + OfflineManager::ListOfflineRegionsCallback::onList(*env, *jFileSource, *callback, *regions); + } else { + OfflineManager::ListOfflineRegionsCallback::onError(*env, *callback, regions.error()); + } + }); } void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, @@ -79,24 +77,24 @@ void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); // Create region - fileSource->createOfflineRegion(definition, metadata, [ - //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), - jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) - ](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> region) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (region) { - OfflineManager::CreateOfflineRegionCallback::onCreate( - *env, *jFileSource, *callback, *region - ); - } else { - OfflineManager::CreateOfflineRegionCallback::onError( - *env, *callback, region.error()); - } - }); + databaseStorage->createOfflineRegion( + definition, + metadata, + [ + // Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the + // meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource))]( + mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> region) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (region) { + OfflineManager::CreateOfflineRegionCallback::onCreate(*env, *jFileSource, *callback, *region); + } else { + OfflineManager::CreateOfflineRegionCallback::onError(*env, *callback, region.error()); + } + }); } void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<FileSource>& jFileSource_, @@ -106,29 +104,29 @@ void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<Fi auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); auto path = jni::Make<std::string>(env_, jString_); - fileSource->mergeOfflineRegions(path, [ - //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), - jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) - ](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (regions) { - OfflineManager::MergeOfflineRegionsCallback::onMerge( - *env, *jFileSource, *callback, *regions); - } else { - OfflineManager::MergeOfflineRegionsCallback::onError( - *env, *callback, regions.error()); - } - }); + databaseStorage->mergeOfflineRegions( + path, + [ + // Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the + // meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource))]( + mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (regions) { + OfflineManager::MergeOfflineRegionsCallback::onMerge(*env, *jFileSource, *callback, *regions); + } else { + OfflineManager::MergeOfflineRegionsCallback::onError(*env, *callback, regions.error()); + } + }); } void OfflineManager::resetDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->resetDatabase( + databaseStorage->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<decltype(globalCallback)>(std::move(globalCallback))]( @@ -138,7 +136,7 @@ void OfflineManager::resetDatabase(jni::JNIEnv& env_, const jni::Object<FileSour void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->packDatabase( + databaseStorage->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<decltype(globalCallback)>(std::move(globalCallback))]( @@ -148,7 +146,7 @@ void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourc void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->invalidateAmbientCache( + databaseStorage->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<decltype(globalCallback)>(std::move(globalCallback))]( @@ -158,7 +156,7 @@ void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->clearAmbientCache( + databaseStorage->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<decltype(globalCallback)>(std::move(globalCallback))]( @@ -168,7 +166,7 @@ void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object<File void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jlong size_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->setMaximumAmbientCacheSize( + databaseStorage->setMaximumAmbientCacheSize( size_, [ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile @@ -177,7 +175,7 @@ void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jl } void OfflineManager::runPackDatabaseAutomatically(jni::JNIEnv&, jboolean autopack) { - fileSource->runPackDatabaseAutomatically(autopack); + databaseStorage->runPackDatabaseAutomatically(autopack); } // FileSource::FileSourceCallback // @@ -328,7 +326,7 @@ void OfflineManager::putResourceWithUrl(jni::JNIEnv& env, response.expires = Timestamp(mbgl::Seconds(expires)); } - fileSource->put(resource, response); + databaseStorage->put(resource, response); } } // namespace android diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index 84111a7423..62d6b75b06 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -104,7 +104,7 @@ public: void runPackDatabaseAutomatically(jni::JNIEnv&, jboolean autopack); private: - std::shared_ptr<mbgl::DatabaseFileSource> fileSource; + std::shared_ptr<JointDatabaseStorage> databaseStorage; }; } // namespace android diff --git a/platform/android/src/offline/offline_region.cpp b/platform/android/src/offline/offline_region.cpp index de7abef624..53c1c7d014 100644 --- a/platform/android/src/offline/offline_region.cpp +++ b/platform/android/src/offline/offline_region.cpp @@ -16,10 +16,9 @@ namespace android { OfflineRegion::OfflineRegion(jni::JNIEnv& env, jni::jlong offlineRegionPtr, const jni::Object<FileSource>& jFileSource) : region(reinterpret_cast<mbgl::OfflineRegion*>(offlineRegionPtr)), - fileSource(std::static_pointer_cast<mbgl::DatabaseFileSource>( - std::shared_ptr<mbgl::FileSource>(mbgl::FileSourceManager::get()->getFileSource( - mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource))))) { - if (!fileSource) { + databaseStorage( + FileSourceManager::get()->getDatabaseStorage(FileSource::getSharedResourceOptions(env, jFileSource))) { + if (!databaseStorage) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); } } @@ -69,7 +68,8 @@ void OfflineRegion::setOfflineRegionObserver(jni::JNIEnv& env_, const jni::Objec }; // Set the observer - fileSource->setOfflineRegionObserver(*region, std::make_unique<Observer>(jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback))); + databaseStorage->setOfflineRegionObserver( + *region, std::make_unique<Observer>(jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback))); } void OfflineRegion::setOfflineRegionDownloadState(jni::JNIEnv&, jni::jint jState) { @@ -87,83 +87,88 @@ void OfflineRegion::setOfflineRegionDownloadState(jni::JNIEnv&, jni::jint jState return; } - fileSource->setOfflineRegionDownloadState(*region, state); + databaseStorage->setOfflineRegionDownloadState(*region, state); } void OfflineRegion::getOfflineRegionStatus(jni::JNIEnv& env_, const jni::Object<OfflineRegionStatusCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->getOfflineRegionStatus(*region, [ - //Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) - ](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (status) { - OfflineRegionStatusCallback::onStatus(*env, *callback, std::move(*status)); - } else { - OfflineRegionStatusCallback::onError(*env, *callback, status.error()); - } - }); + databaseStorage->getOfflineRegionStatus( + *region, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( + mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (status) { + OfflineRegionStatusCallback::onStatus(*env, *callback, std::move(*status)); + } else { + OfflineRegionStatusCallback::onError(*env, *callback, status.error()); + } + }); } void OfflineRegion::deleteOfflineRegion(jni::JNIEnv& env_, const jni::Object<OfflineRegionDeleteCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->deleteOfflineRegion(std::move(*region), - [ - // Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>( - std::move(globalCallback))](std::exception_ptr error) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (error) { - OfflineRegionDeleteCallback::onError(*env, *callback, error); - } else { - OfflineRegionDeleteCallback::onDelete(*env, *callback); - } - }); + databaseStorage->deleteOfflineRegion(std::move(*region), + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>( + std::move(globalCallback))](std::exception_ptr error) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (error) { + OfflineRegionDeleteCallback::onError(*env, *callback, error); + } else { + OfflineRegionDeleteCallback::onDelete(*env, *callback); + } + }); } void OfflineRegion::invalidateOfflineRegion(jni::JNIEnv& env_, const jni::Object<OfflineRegionInvalidateCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->invalidateOfflineRegion(*region, - [ - // Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>( - 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); - } - }); + databaseStorage->invalidateOfflineRegion(*region, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>( + 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<jni::jbyte>& jMetadata, const jni::Object<OfflineRegionUpdateMetadataCallback>& callback_) { auto metadata = OfflineRegion::metadata(env_, jMetadata); auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->updateOfflineMetadata(region->getID(), metadata, [ - //Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) - ](mbgl::expected<mbgl::OfflineRegionMetadata, std::exception_ptr> data) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (data) { - OfflineRegionUpdateMetadataCallback::onUpdate(*env, *callback, std::move(*data)); - } else { - OfflineRegionUpdateMetadataCallback::onError(*env, *callback, data.error()); - } - }); + databaseStorage->updateOfflineMetadata( + region->getID(), + metadata, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( + mbgl::expected<mbgl::OfflineRegionMetadata, std::exception_ptr> data) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (data) { + OfflineRegionUpdateMetadataCallback::onUpdate(*env, *callback, std::move(*data)); + } else { + OfflineRegionUpdateMetadataCallback::onError(*env, *callback, data.error()); + } + }); } jni::Local<jni::Object<OfflineRegion>> OfflineRegion::New(jni::JNIEnv& env, diff --git a/platform/android/src/offline/offline_region.hpp b/platform/android/src/offline/offline_region.hpp index 6844008bb4..34fa156476 100644 --- a/platform/android/src/offline/offline_region.hpp +++ b/platform/android/src/offline/offline_region.hpp @@ -85,7 +85,7 @@ public: private: std::unique_ptr<mbgl::OfflineRegion> region; - std::shared_ptr<mbgl::DatabaseFileSource> fileSource; + std::shared_ptr<mbgl::JointDatabaseStorage> databaseStorage; }; } // namespace android diff --git a/platform/darwin/src/MGLOfflinePack.mm b/platform/darwin/src/MGLOfflinePack.mm index edee549744..7503551e68 100644 --- a/platform/darwin/src/MGLOfflinePack.mm +++ b/platform/darwin/src/MGLOfflinePack.mm @@ -59,7 +59,7 @@ private: @implementation MGLOfflinePack { BOOL _isSuspending; - std::shared_ptr<mbgl::DatabaseFileSource> _mbglDatabaseFileSource; + std::shared_ptr<mbgl::JointDatabaseStorage> _mbglDatabaseStorage; } - (instancetype)init { @@ -76,8 +76,8 @@ private: _mbglOfflineRegion = region; _state = MGLOfflinePackStateUnknown; - _mbglDatabaseFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglDatabaseFileSource]; - _mbglDatabaseFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); + _mbglDatabaseStorage = [[MGLOfflineStorage sharedOfflineStorage] mbglDatabaseStorage]; + _mbglDatabaseStorage->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); } return self; } @@ -117,7 +117,7 @@ private: self.state = MGLOfflinePackStateActive; - _mbglDatabaseFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); + _mbglDatabaseStorage->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); } - (void)suspend { @@ -129,7 +129,7 @@ private: _isSuspending = YES; } - _mbglDatabaseFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); + _mbglDatabaseStorage->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); } - (void)invalidate { @@ -140,7 +140,7 @@ private: @synchronized (self) { self.state = MGLOfflinePackStateInvalid; if (self.mbglOfflineRegion) { - _mbglDatabaseFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); + _mbglDatabaseStorage->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); } self.mbglOfflineRegion = nil; } @@ -169,7 +169,7 @@ private: MGLAssertOfflinePackIsValid(); __weak MGLOfflinePack *weakSelf = self; - _mbglDatabaseFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) { + _mbglDatabaseStorage->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) { if (status) { mbgl::OfflineRegionStatus checkedStatus = *status; dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 4c71286b79..29d1d2b76f 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -45,7 +45,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio @interface MGLOfflineStorage () @property (nonatomic, strong, readwrite) NSMutableArray<MGLOfflinePack *> *packs; -@property (nonatomic) std::shared_ptr<mbgl::DatabaseFileSource> mbglDatabaseFileSource; +@property (nonatomic) std::shared_ptr<mbgl::JointDatabaseStorage> mbglDatabaseStorage; @property (nonatomic) std::shared_ptr<mbgl::FileSource> mbglOnlineFileSource; @property (nonatomic) std::shared_ptr<mbgl::FileSource> mbglFileSource; @property (nonatomic) std::string mbglCachePath; @@ -234,7 +234,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); _mbglFileSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::ResourceLoader, options); _mbglOnlineFileSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, options); - _mbglDatabaseFileSource = std::static_pointer_cast<mbgl::DatabaseFileSource>(std::shared_ptr<mbgl::FileSource>(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, options))); + _mbglDatabaseStorage = mbgl::FileSourceManager::get()->getDatabaseStorage(options); // Observe for changes to the API base URL (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self @@ -341,7 +341,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)_addContentsOfFile:(NSString *)filePath withCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error))completion { - _mbglDatabaseFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + _mbglDatabaseStorage->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { @@ -406,7 +406,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio const mbgl::OfflineRegionDefinition regionDefinition = [(id <MGLOfflineRegion_Private>)region offlineRegionDefinition]; mbgl::OfflineRegionMetadata metadata(context.length); [context getBytes:&metadata[0] length:metadata.size()]; - _mbglDatabaseFileSource->createOfflineRegion(regionDefinition, metadata, [&, completion](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> mbglOfflineRegion) { + _mbglDatabaseStorage->createOfflineRegion(regionDefinition, metadata, [&, completion](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> mbglOfflineRegion) { NSError *error; if (!mbglOfflineRegion) { NSString *errorDescription = @(mbgl::util::toString(mbglOfflineRegion.error()).c_str()); @@ -447,7 +447,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio return; } - _mbglDatabaseFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { + _mbglDatabaseStorage->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { NSError *error; if (exception) { error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ @@ -474,7 +474,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio return; } - _mbglDatabaseFileSource->invalidateOfflineRegion(region, [&](std::exception_ptr exception) { + _mbglDatabaseStorage->invalidateOfflineRegion(region, [&](std::exception_ptr exception) { if (exception) { error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), @@ -502,7 +502,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)getPacksWithCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error))completion { - _mbglDatabaseFileSource->listOfflineRegions([&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + _mbglDatabaseStorage->listOfflineRegions([&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { @@ -530,13 +530,13 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio - (void)setMaximumAllowedMapboxTiles:(uint64_t)maximumCount { MGLLogDebug(@"Setting maximumAllowedMapboxTiles: %lu", (unsigned long)maximumCount); - _mbglDatabaseFileSource->setOfflineMapboxTileCountLimit(maximumCount); + _mbglDatabaseStorage->setOfflineMapboxTileCountLimit(maximumCount); } #pragma mark - Ambient Cache management - (void)setMaximumAmbientCacheSize:(NSUInteger)cacheSize withCompletionHandler:(void (^)(NSError * _Nullable))completion { - _mbglDatabaseFileSource->setMaximumAmbientCacheSize(cacheSize, [&, completion](std::exception_ptr exception) { + _mbglDatabaseStorage->setMaximumAmbientCacheSize(cacheSize, [&, completion](std::exception_ptr exception) { NSError *error; if (completion) { if (exception) { @@ -555,7 +555,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)invalidateAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable))completion { - _mbglDatabaseFileSource->invalidateAmbientCache([&, completion](std::exception_ptr exception){ + _mbglDatabaseStorage->invalidateAmbientCache([&, completion](std::exception_ptr exception){ NSError *error; if (completion) { if (exception) { @@ -575,7 +575,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)clearAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable error))completion { - _mbglDatabaseFileSource->clearAmbientCache([&, completion](std::exception_ptr exception){ + _mbglDatabaseStorage->clearAmbientCache([&, completion](std::exception_ptr exception){ NSError *error; if (completion) { if (exception) { @@ -594,7 +594,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)resetDatabaseWithCompletionHandler:(void (^)(NSError *_Nullable error))completion { - _mbglDatabaseFileSource->resetDatabase([&, completion](std::exception_ptr exception) { + _mbglDatabaseStorage->resetDatabase([&, completion](std::exception_ptr exception) { NSError *error; if (completion) { if (exception) { @@ -642,7 +642,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio response.expires = mbgl::Timestamp() + std::chrono::duration_cast<mbgl::Seconds>(MGLDurationFromTimeInterval(expires.timeIntervalSince1970)); } - _mbglDatabaseFileSource->put(resource, response); + _mbglDatabaseStorage->put(resource, response); } - (void)putResourceWithUrl:(NSURL *)url data:(NSData *)data modified:(nullable NSDate *)modified expires:(nullable NSDate *)expires etag:(nullable NSString *)etag mustRevalidate:(BOOL)mustRevalidate { diff --git a/platform/darwin/src/MGLOfflineStorage_Private.h b/platform/darwin/src/MGLOfflineStorage_Private.h index c01163e766..2657860f5e 100644 --- a/platform/darwin/src/MGLOfflineStorage_Private.h +++ b/platform/darwin/src/MGLOfflineStorage_Private.h @@ -12,9 +12,9 @@ NS_ASSUME_NONNULL_BEGIN @interface MGLOfflineStorage (Private) /** - The shared database file source object owned by the shared offline storage object. + The shared database file storage object. */ -@property (nonatomic) std::shared_ptr<mbgl::DatabaseFileSource> mbglDatabaseFileSource; +@property (nonatomic) std::shared_ptr<mbgl::JointDatabaseStorage> mbglDatabaseStorage; /** The shared online file source object owned by the shared offline storage object. diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index 784e002a65..240cbf5f49 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -180,10 +180,11 @@ int main(int argc, char *argv[]) { }); // Database file source. - auto databaseFileSource = std::static_pointer_cast<mbgl::DatabaseFileSource>(std::shared_ptr<mbgl::FileSource>( - mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions))); - view->setResetCacheCallback([databaseFileSource]() { - databaseFileSource->resetDatabase([](std::exception_ptr ex) { + std::shared_ptr<mbgl::JointDatabaseStorage> database = + mbgl::FileSourceManager::get()->getDatabaseStorage(resourceOptions); + assert(database); + view->setResetCacheCallback([database]() { + database->resetDatabase([](std::exception_ptr ex) { if (ex) { mbgl::Log::Error(mbgl::Event::Database, "Failed to reset cache:: %s", mbgl::util::toString(ex).c_str()); } diff --git a/test/storage/main_resource_loader.test.cpp b/test/storage/main_resource_loader.test.cpp index 2622d91183..1a8f05c201 100644 --- a/test/storage/main_resource_loader.test.cpp +++ b/test/storage/main_resource_loader.test.cpp @@ -586,10 +586,9 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(SetResourceTransform)) { TEST(MainResourceLoader, SetResourceCachePath) { util::RunLoop loop; MainResourceLoader resourceLoader(ResourceOptions{}); - std::shared_ptr<FileSource> fs = - FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); - auto dbfs = std::static_pointer_cast<DatabaseFileSource>(fs); - dbfs->setDatabasePath("./new_offline.db", [&loop] { loop.stop(); }); + std::shared_ptr<mbgl::JointDatabaseStorage> database = + mbgl::FileSourceManager::get()->getDatabaseStorage(ResourceOptions{}); + database->setDatabasePath("./new_offline.db", [&loop] { loop.stop(); }); loop.run(); } |