summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2019-02-19 13:22:24 +0100
committerŁukasz Paczos <lukasz.paczos@mapbox.com>2019-03-13 18:28:04 +0100
commit3f7b70fdb0e40d0cbbaea17505878c72491e3cd2 (patch)
treedbbc7be080142263797bafbff5c4b950c615dc90
parent5e58a0d81d702a543e898b489ead044a630229a0 (diff)
downloadqtlocation-mapboxgl-3f7b70fdb0e40d0cbbaea17505878c72491e3cd2.tar.gz
[core] expose an option to change the db file path in runtime
-rw-r--r--include/mbgl/storage/default_file_source.hpp2
-rw-r--r--platform/android/scripts/exclude-activity-gen.json2
-rw-r--r--platform/default/include/mbgl/storage/offline_database.hpp6
-rw-r--r--platform/default/src/mbgl/storage/default_file_source.cpp8
-rw-r--r--platform/default/src/mbgl/storage/offline_database.cpp30
-rw-r--r--test/storage/offline_database.test.cpp8
6 files changed, 45 insertions, 11 deletions
diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp
index 6ce7e8c6f9..5f6999e99f 100644
--- a/include/mbgl/storage/default_file_source.hpp
+++ b/include/mbgl/storage/default_file_source.hpp
@@ -47,6 +47,8 @@ public:
void setResourceTransform(optional<ActorRef<ResourceTransform>>&&);
+ void setResourceCachePath(const std::string&);
+
std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override;
/*
diff --git a/platform/android/scripts/exclude-activity-gen.json b/platform/android/scripts/exclude-activity-gen.json
index 841a04e5f1..9e0a1d154f 100644
--- a/platform/android/scripts/exclude-activity-gen.json
+++ b/platform/android/scripts/exclude-activity-gen.json
@@ -46,5 +46,7 @@
"GridSourceActivity",
"AnimatedImageSourceActivity",
"EspressoTestActivity",
+ "ChangeResourcesCachePathActivity",
+ "EspressoTestActivity",
"FragmentBackStackActivity"
]
diff --git a/platform/default/include/mbgl/storage/offline_database.hpp b/platform/default/include/mbgl/storage/offline_database.hpp
index 993f36a606..6414affbbe 100644
--- a/platform/default/include/mbgl/storage/offline_database.hpp
+++ b/platform/default/include/mbgl/storage/offline_database.hpp
@@ -43,6 +43,10 @@ public:
OfflineDatabase(std::string path, uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE);
~OfflineDatabase();
+ void changePath(const std::string&);
+
+ void cleanup();
+
optional<Response> get(const Resource&);
// Return value is (inserted, stored size)
@@ -112,7 +116,7 @@ private:
std::pair<int64_t, int64_t> getCompletedResourceCountAndSize(int64_t regionID);
std::pair<int64_t, int64_t> getCompletedTileCountAndSize(int64_t regionID);
- const std::string path;
+ std::string path;
std::unique_ptr<mapbox::sqlite::Database> db;
std::unordered_map<const char *, const std::unique_ptr<mapbox::sqlite::Statement>> statements;
diff --git a/platform/default/src/mbgl/storage/default_file_source.cpp b/platform/default/src/mbgl/storage/default_file_source.cpp
index cad68e7de9..32eb8b3d58 100644
--- a/platform/default/src/mbgl/storage/default_file_source.cpp
+++ b/platform/default/src/mbgl/storage/default_file_source.cpp
@@ -45,6 +45,10 @@ public:
onlineFileSource.setResourceTransform(std::move(transform));
}
+ void setResourceCachePath(const std::string& path) {
+ offlineDatabase->changePath(path);
+ }
+
void listRegions(std::function<void (expected<OfflineRegions, std::exception_ptr>)> callback) {
callback(offlineDatabase->listRegions());
}
@@ -243,6 +247,10 @@ void DefaultFileSource::setResourceTransform(optional<ActorRef<ResourceTransform
impl->actor().invoke(&Impl::setResourceTransform, std::move(transform));
}
+void DefaultFileSource::setResourceCachePath(const std::string& path) {
+ impl->actor().invoke(&Impl::setResourceCachePath, path);
+}
+
std::unique_ptr<AsyncRequest> DefaultFileSource::request(const Resource& resource, Callback callback) {
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
diff --git a/platform/default/src/mbgl/storage/offline_database.cpp b/platform/default/src/mbgl/storage/offline_database.cpp
index 7732076991..752f5ac20a 100644
--- a/platform/default/src/mbgl/storage/offline_database.cpp
+++ b/platform/default/src/mbgl/storage/offline_database.cpp
@@ -27,16 +27,7 @@ OfflineDatabase::OfflineDatabase(std::string path_, uint64_t maximumCacheSize_)
}
OfflineDatabase::~OfflineDatabase() {
- // Deleting these SQLite objects may result in exceptions, but we're in a destructor, so we
- // can't throw anything.
- try {
- statements.clear();
- db.reset();
- } catch (const util::IOException& ex) {
- handleError(ex, "close database");
- } catch (const mapbox::sqlite::Exception& ex) {
- handleError(ex, "close database");
- }
+ cleanup();
}
void OfflineDatabase::initialize() {
@@ -78,6 +69,25 @@ void OfflineDatabase::initialize() {
}
}
+void OfflineDatabase::changePath(const std::string& path_) {
+ Log::Info(Event::Database, "Changing the database path.");
+ cleanup();
+ path = path_;
+ initialize();
+}
+
+void OfflineDatabase::cleanup() {
+ // Deleting these SQLite objects may result in exceptions
+ try {
+ statements.clear();
+ db.reset();
+ } catch (const util::IOException& ex) {
+ handleError(ex, "close database");
+ } catch (const mapbox::sqlite::Exception& ex) {
+ handleError(ex, "close database");
+ }
+}
+
void OfflineDatabase::handleError(const mapbox::sqlite::Exception& ex, const char* action) {
if (ex.code == mapbox::sqlite::ResultCode::NotADB ||
ex.code == mapbox::sqlite::ResultCode::Corrupt ||
diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp
index d6405fd452..744803a4b2 100644
--- a/test/storage/offline_database.test.cpp
+++ b/test/storage/offline_database.test.cpp
@@ -1352,3 +1352,11 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(MergeDatabaseWithDiskFull)) {
}
#endif // __QT__
+TEST(OfflineDatabse, ChangePath) {
+ std::string newPath("test/fixtures/offline_database/test.db");
+ OfflineDatabase db(":memory:");
+ db.changePath(newPath);
+ mapbox::sqlite::Database::open(newPath, mapbox::sqlite::ReadOnly);
+ util::deleteFile(newPath);
+}
+