[core] expose an option to change the db file path in runtime
This commit is contained in:
committed by
Łukasz Paczos
parent
5e58a0d81d
commit
3f7b70fdb0
@@ -47,6 +47,8 @@ public:
|
||||
|
||||
void setResourceTransform(optional<ActorRef<ResourceTransform>>&&);
|
||||
|
||||
void setResourceCachePath(const std::string&);
|
||||
|
||||
std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override;
|
||||
|
||||
/*
|
||||
|
||||
@@ -46,5 +46,7 @@
|
||||
"GridSourceActivity",
|
||||
"AnimatedImageSourceActivity",
|
||||
"EspressoTestActivity",
|
||||
"ChangeResourcesCachePathActivity",
|
||||
"EspressoTestActivity",
|
||||
"FragmentBackStackActivity"
|
||||
]
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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 ||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user