summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSudarsana Babu Nagineni <sudarsana.babu@mapbox.com>2019-04-03 15:08:02 +0300
committerSudarsana Babu Nagineni <sudarsana.babu@mapbox.com>2019-04-29 17:09:58 +0300
commit2604991b2b0090ed69893f6b156a6e640a789c9f (patch)
treed6b29f0a512b3de53fb4cc23d96cc2f52b1fdffe
parent4c8295e67200e15979ddc0c6b16d9b09e4f0a192 (diff)
downloadqtlocation-mapboxgl-2604991b2b0090ed69893f6b156a6e640a789c9f.tar.gz
[core] Introduce resetCache API
Add an API to delete existing database and re-initialize.
-rw-r--r--include/mbgl/storage/default_file_source.hpp9
-rw-r--r--platform/default/include/mbgl/storage/offline_database.hpp4
-rw-r--r--platform/default/src/mbgl/storage/default_file_source.cpp8
-rw-r--r--platform/default/src/mbgl/storage/offline_database.cpp8
-rw-r--r--platform/glfw/glfw_view.cpp4
-rw-r--r--platform/glfw/glfw_view.hpp5
-rw-r--r--platform/glfw/main.cpp9
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<void (std::exception_ptr)>);
+
// 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<Response> 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<void (std::exception_ptr)> callback) {
+ callback(offlineDatabase->resetCache());
+ }
+
private:
expected<OfflineDownload*, std::exception_ptr> 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<void (std::exception_ptr)> 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<void()> callback) {
+ resetCacheCallback = callback;
+ };
+
void setShouldClose();
void setWindowTitle(const std::string&);
@@ -120,6 +124,7 @@ private:
std::function<void()> changeStyleCallback;
std::function<void()> pauseResumeCallback;
std::function<void()> onlineStatusCallback;
+ std::function<void()> resetCacheCallback;
std::function<void(mbgl::Map*)> 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 <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/default_thread_pool.hpp>
+#include <mbgl/util/string.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/renderer/renderer.hpp>
@@ -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");