Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] expose an option to change the db file path in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Mar 13, 2019
1 parent 5e58a0d commit 3f7b70f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class DefaultFileSource : public FileSource {

void setResourceTransform(optional<ActorRef<ResourceTransform>>&&);

void setResourceCachePath(const std::string&);

std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override;

/*
Expand Down
2 changes: 2 additions & 0 deletions platform/android/scripts/exclude-activity-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@
"GridSourceActivity",
"AnimatedImageSourceActivity",
"EspressoTestActivity",
"ChangeResourcesCachePathActivity",
"EspressoTestActivity",
"FragmentBackStackActivity"
]
6 changes: 5 additions & 1 deletion platform/default/include/mbgl/storage/offline_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class OfflineDatabase : private util::noncopyable {
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)
Expand Down Expand Up @@ -112,7 +116,7 @@ class OfflineDatabase : private util::noncopyable {
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;

Expand Down
8 changes: 8 additions & 0 deletions platform/default/src/mbgl/storage/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class DefaultFileSource::Impl {
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());
}
Expand Down Expand Up @@ -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));

Expand Down
30 changes: 20 additions & 10 deletions platform/default/src/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 ||
Expand Down
8 changes: 8 additions & 0 deletions test/storage/offline_database.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 3f7b70f

Please sign in to comment.