From 2604991b2b0090ed69893f6b156a6e640a789c9f Mon Sep 17 00:00:00 2001 From: Sudarsana Babu Nagineni Date: Wed, 3 Apr 2019 15:08:02 +0300 Subject: [core] Introduce resetCache API Add an API to delete existing database and re-initialize. --- include/mbgl/storage/default_file_source.hpp | 9 +++++++++ platform/default/include/mbgl/storage/offline_database.hpp | 4 ++-- platform/default/src/mbgl/storage/default_file_source.cpp | 8 ++++++++ platform/default/src/mbgl/storage/offline_database.cpp | 8 ++++++++ platform/glfw/glfw_view.cpp | 4 ++++ platform/glfw/glfw_view.hpp | 5 +++++ platform/glfw/main.cpp | 9 +++++++++ 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 8f88964acf..10a836094c 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -174,6 +174,15 @@ public: */ void put(const Resource&, const Response&); + /* + * Delete existing database and re-initialize. + * + * 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 resetCache(std::function); + // For testing only. void setOnlineStatus(bool); diff --git a/platform/default/include/mbgl/storage/offline_database.hpp b/platform/default/include/mbgl/storage/offline_database.hpp index 6414affbbe..3ba15d5813 100644 --- a/platform/default/include/mbgl/storage/offline_database.hpp +++ b/platform/default/include/mbgl/storage/offline_database.hpp @@ -44,8 +44,7 @@ public: ~OfflineDatabase(); void changePath(const std::string&); - - void cleanup(); + std::exception_ptr resetCache(); optional get(const Resource&); @@ -91,6 +90,7 @@ private: void migrateToVersion5(); void migrateToVersion3(); void migrateToVersion6(); + void cleanup(); mapbox::sqlite::Statement& getStatement(const char *); diff --git a/platform/default/src/mbgl/storage/default_file_source.cpp b/platform/default/src/mbgl/storage/default_file_source.cpp index 1ccf999109..08fbbff9a0 100644 --- a/platform/default/src/mbgl/storage/default_file_source.cpp +++ b/platform/default/src/mbgl/storage/default_file_source.cpp @@ -177,6 +177,10 @@ public: offlineDatabase->put(resource, response); } + void resetCache(std::function callback) { + callback(offlineDatabase->resetCache()); + } + private: expected getDownload(int64_t regionID) { auto it = downloads.find(regionID); @@ -316,6 +320,10 @@ void DefaultFileSource::put(const Resource& resource, const Response& response) impl->actor().invoke(&Impl::put, resource, response); } +void DefaultFileSource::resetCache(std::function callback) { + impl->actor().invoke(&Impl::resetCache, callback); +} + // For testing only: void DefaultFileSource::setOnlineStatus(const bool status) { diff --git a/platform/default/src/mbgl/storage/offline_database.cpp b/platform/default/src/mbgl/storage/offline_database.cpp index 752f5ac20a..1639c4484c 100644 --- a/platform/default/src/mbgl/storage/offline_database.cpp +++ b/platform/default/src/mbgl/storage/offline_database.cpp @@ -1136,4 +1136,12 @@ bool OfflineDatabase::exceedsOfflineMapboxTileCountLimit(const Resource& resourc && offlineMapboxTileCountLimitExceeded(); } +std::exception_ptr OfflineDatabase::resetCache() try { + removeExisting(); + initialize(); + return nullptr; +} catch (...) { + return std::current_exception(); +} + } // namespace mbgl diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 2772ed0773..fe956d8a68 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -124,6 +124,7 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_) printf("- Press `O` to toggle online connectivity\n"); printf("- Press `Z` to cycle through north orientations\n"); printf("- Prezz `X` to cycle through the viewport modes\n"); + printf("- Press `I` to Delete existing database and re-initialize\n"); printf("- Press `A` to cycle through Mapbox offices in the world + dateline monument\n"); printf("- Press `B` to cycle through the color, stencil, and depth buffer\n"); printf("- Press `D` to cycle through camera bounds: inside, crossing IDL at left, crossing IDL at right, and disabled\n"); @@ -221,6 +222,9 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, case GLFW_KEY_C: view->clearAnnotations(); break; + case GLFW_KEY_I: + view->resetCacheCallback(); + break; case GLFW_KEY_K: view->addRandomCustomPointAnnotations(1); break; diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index 9b580f80cc..1957970b85 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -40,6 +40,10 @@ public: onlineStatusCallback = callback; } + void setResetCacheCallback(std::function callback) { + resetCacheCallback = callback; + }; + void setShouldClose(); void setWindowTitle(const std::string&); @@ -120,6 +124,7 @@ private: std::function changeStyleCallback; std::function pauseResumeCallback; std::function onlineStatusCallback; + std::function resetCacheCallback; std::function animateRouteCallback; mbgl::util::RunLoop runLoop; diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index 0a4ace6a19..4e631ec895 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -159,6 +160,14 @@ int main(int argc, char *argv[]) { isPaused = !isPaused; }); + view->setResetCacheCallback([fileSource] () { + fileSource->resetCache([](std::exception_ptr ex) { + if (ex) { + mbgl::Log::Error(mbgl::Event::Database, "Failed to reset cache:: %s", mbgl::util::toString(ex).c_str()); + } + }); + }); + // Load style if (style.empty()) { const char *url = getenv("MAPBOX_STYLE_URL"); -- cgit v1.2.1