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

[core] initialize OfflineDatabase asynchronously in DefaultFileSource #9864

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions platform/default/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ namespace mbgl {

class DefaultFileSource::Impl {
public:
Impl(ActorRef<Impl>, std::shared_ptr<FileSource> assetFileSource_, const std::string& cachePath, uint64_t maximumCacheSize)
Impl(ActorRef<Impl> self, std::shared_ptr<FileSource> assetFileSource_, const std::string& cachePath, uint64_t maximumCacheSize)
: assetFileSource(assetFileSource_)
, localFileSource(std::make_unique<LocalFileSource>())
, offlineDatabase(cachePath, maximumCacheSize) {
, localFileSource(std::make_unique<LocalFileSource>()) {
// Initialize the Database asynchronously so as to not block Actor creation.
self.invoke(&Impl::initializeOfflineDatabase, cachePath, maximumCacheSize);
}

void initializeOfflineDatabase(std::string cachePath, uint64_t maximumCacheSize) {
offlineDatabase = std::make_unique<OfflineDatabase>(cachePath, maximumCacheSize);
}

void setAPIBaseURL(const std::string& url) {
Expand All @@ -56,7 +61,7 @@ class DefaultFileSource::Impl {

void listRegions(std::function<void (std::exception_ptr, optional<std::vector<OfflineRegion>>)> callback) {
try {
callback({}, offlineDatabase.listRegions());
callback({}, offlineDatabase->listRegions());
} catch (...) {
callback(std::current_exception(), {});
}
Expand All @@ -66,7 +71,7 @@ class DefaultFileSource::Impl {
const OfflineRegionMetadata& metadata,
std::function<void (std::exception_ptr, optional<OfflineRegion>)> callback) {
try {
callback({}, offlineDatabase.createRegion(definition, metadata));
callback({}, offlineDatabase->createRegion(definition, metadata));
} catch (...) {
callback(std::current_exception(), {});
}
Expand All @@ -76,7 +81,7 @@ class DefaultFileSource::Impl {
const OfflineRegionMetadata& metadata,
std::function<void (std::exception_ptr, optional<OfflineRegionMetadata>)> callback) {
try {
callback({}, offlineDatabase.updateMetadata(regionID, metadata));
callback({}, offlineDatabase->updateMetadata(regionID, metadata));
} catch (...) {
callback(std::current_exception(), {});
}
Expand All @@ -93,7 +98,7 @@ class DefaultFileSource::Impl {
void deleteRegion(OfflineRegion&& region, std::function<void (std::exception_ptr)> callback) {
try {
downloads.erase(region.getID());
offlineDatabase.deleteRegion(std::move(region));
offlineDatabase->deleteRegion(std::move(region));
callback({});
} catch (...) {
callback(std::current_exception());
Expand Down Expand Up @@ -125,7 +130,7 @@ class DefaultFileSource::Impl {

const bool hasPrior = resource.priorEtag || resource.priorModified || resource.priorExpires;
if (!hasPrior || resource.necessity == Resource::Optional) {
auto offlineResponse = offlineDatabase.get(resource);
auto offlineResponse = offlineDatabase->get(resource);

if (resource.necessity == Resource::Optional && !offlineResponse) {
// Ensure there's always a response that we can send, so the caller knows that
Expand Down Expand Up @@ -157,7 +162,7 @@ class DefaultFileSource::Impl {
// Get from the online file source
if (resource.necessity == Resource::Required) {
tasks[req] = onlineFileSource.request(revalidation, [=] (Response onlineResponse) mutable {
this->offlineDatabase.put(revalidation, onlineResponse);
this->offlineDatabase->put(revalidation, onlineResponse);
callback(onlineResponse);
});
}
Expand All @@ -169,11 +174,11 @@ class DefaultFileSource::Impl {
}

void setOfflineMapboxTileCountLimit(uint64_t limit) {
offlineDatabase.setOfflineMapboxTileCountLimit(limit);
offlineDatabase->setOfflineMapboxTileCountLimit(limit);
}

void put(const Resource& resource, const Response& response) {
offlineDatabase.put(resource, response);
offlineDatabase->put(resource, response);
}

private:
Expand All @@ -183,13 +188,13 @@ class DefaultFileSource::Impl {
return *it->second;
}
return *downloads.emplace(regionID,
std::make_unique<OfflineDownload>(regionID, offlineDatabase.getRegionDefinition(regionID), offlineDatabase, onlineFileSource)).first->second;
std::make_unique<OfflineDownload>(regionID, offlineDatabase->getRegionDefinition(regionID), *offlineDatabase, onlineFileSource)).first->second;
}

// shared so that destruction is done on the creating thread
const std::shared_ptr<FileSource> assetFileSource;
const std::unique_ptr<FileSource> localFileSource;
OfflineDatabase offlineDatabase;
std::unique_ptr<OfflineDatabase> offlineDatabase;
OnlineFileSource onlineFileSource;
std::unordered_map<AsyncRequest*, std::unique_ptr<AsyncRequest>> tasks;
std::unordered_map<int64_t, std::unique_ptr<OfflineDownload>> downloads;
Expand Down