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

Commit

Permalink
[core] harden OfflineDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Aug 6, 2018
1 parent 99a365d commit 03a4dc2
Show file tree
Hide file tree
Showing 16 changed files with 1,157 additions and 353 deletions.
2 changes: 2 additions & 0 deletions cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ set(MBGL_TEST_FILES
test/src/mbgl/test/fixture_log_observer.hpp
test/src/mbgl/test/getrss.cpp
test/src/mbgl/test/getrss.hpp
test/src/mbgl/test/sqlite3_test_fs.cpp
test/src/mbgl/test/sqlite3_test_fs.hpp
test/src/mbgl/test/stub_file_source.cpp
test/src/mbgl/test/stub_file_source.hpp
test/src/mbgl/test/stub_geometry_tile_feature.hpp
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/storage/offline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ class OfflineRegion {
public:
// Move-only; not publicly constructible.
OfflineRegion(OfflineRegion&&);
OfflineRegion& operator=(OfflineRegion&&);
~OfflineRegion();

OfflineRegion() = delete;
OfflineRegion(const OfflineRegion&) = delete;
OfflineRegion& operator=(OfflineRegion&&) = delete;
OfflineRegion& operator=(const OfflineRegion&) = delete;

int64_t getID() const;
Expand Down
28 changes: 19 additions & 9 deletions platform/default/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class DefaultFileSource::Impl {
}

void getRegionStatus(int64_t regionID, std::function<void (std::exception_ptr, optional<OfflineRegionStatus>)> callback) {
try {
callback({}, getDownload(regionID).getStatus());
} catch (...) {
if (auto download = getDownload(regionID)) {
callback({}, download->getStatus());
} else {
callback(std::current_exception(), {});
}
}
Expand All @@ -92,11 +92,15 @@ class DefaultFileSource::Impl {
}

void setRegionObserver(int64_t regionID, std::unique_ptr<OfflineRegionObserver> observer) {
getDownload(regionID).setObserver(std::move(observer));
if (auto download = getDownload(regionID)) {
download->setObserver(std::move(observer));
}
}

void setRegionDownloadState(int64_t regionID, OfflineRegionDownloadState state) {
getDownload(regionID).setState(state);
if (auto download = getDownload(regionID)) {
download->setState(state);
}
}

void request(AsyncRequest* req, Resource resource, ActorRef<FileSourceRequest> ref) {
Expand Down Expand Up @@ -181,13 +185,19 @@ class DefaultFileSource::Impl {
}

private:
OfflineDownload& getDownload(int64_t regionID) {
OfflineDownload* getDownload(int64_t regionID) {
auto it = downloads.find(regionID);
if (it != downloads.end()) {
return *it->second;
return it->second.get();
}
auto definition = offlineDatabase->getRegionDefinition(regionID);
if (!definition) {
return nullptr;
}
return *downloads.emplace(regionID,
std::make_unique<OfflineDownload>(regionID, offlineDatabase->getRegionDefinition(regionID), *offlineDatabase, onlineFileSource)).first->second;

auto download = std::make_unique<OfflineDownload>(regionID, std::move(*definition),
*offlineDatabase, onlineFileSource);
return downloads.emplace(regionID, std::move(download)).first->second.get();
}

// shared so that destruction is done on the creating thread
Expand Down
Loading

0 comments on commit 03a4dc2

Please sign in to comment.