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

Commit

Permalink
[core, ios] Introduce revalidation state API on default file source
Browse files Browse the repository at this point in the history
This adds an API to default file source so that it can be put in a
state where requests are not checked for invalidation in sqlite
(or sent over the network). The iOS map view has also been updated
to use this new API when it knows that the host application is
going into the background or will otherwise be in a state where
it is not visible by the user.

This optimization means that the SDK will not make requests for data
that the user cannot see or hit edge cases on some OSs where resources
(i.e. sqlite) are not available when the host app is not visible to the
user.
  • Loading branch information
boundsj committed Feb 20, 2017
1 parent 62e8e64 commit 8f3abad
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace util {
template <typename T> class Thread;
} // namespace util

enum class DefaultFileSourceRevalidationState {
Active,
Inactive
};

class DefaultFileSource : public FileSource {
public:
/*
Expand All @@ -38,6 +43,16 @@ class DefaultFileSource : public FileSource {

void setResourceTransform(std::function<std::string(Resource::Kind, std::string&&)>);

/*
* Pause or resume revalidation requests.
*
* If the revalidation state is set to inactive, then attempts to
* call request will call the callback immediately with a request
* with an error indicating that the file source is inactive.
*/
void setRevalidationState(DefaultFileSourceRevalidationState state);
DefaultFileSourceRevalidationState getRevalidationState() const;

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

/*
Expand Down
26 changes: 26 additions & 0 deletions platform/default/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class DefaultFileSource::Impl {
onlineFileSource.setResourceTransform(std::move(transform));
}

void setRevalidationState(DefaultFileSourceRevalidationState state) {
revalidationState = state;
}

DefaultFileSourceRevalidationState getRevalidationState() const {
return revalidationState;
}

void listRegions(std::function<void (std::exception_ptr, optional<std::vector<OfflineRegion>>)> callback) {
try {
callback({}, offlineDatabase.listRegions());
Expand Down Expand Up @@ -105,6 +113,15 @@ class DefaultFileSource::Impl {
}

void request(AsyncRequest* req, Resource resource, Callback callback) {

if (revalidationState == DefaultFileSourceRevalidationState::Inactive) {
Response inactiveResponse;
inactiveResponse.error = std::make_unique<Response::Error>(
Response::Error::Reason::Other, "File source is inactive");
callback(inactiveResponse);
return;
}

Resource revalidation = resource;

const bool hasPrior = resource.priorEtag || resource.priorModified || resource.priorExpires;
Expand Down Expand Up @@ -162,6 +179,7 @@ class DefaultFileSource::Impl {
OnlineFileSource onlineFileSource;
std::unordered_map<AsyncRequest*, std::unique_ptr<AsyncRequest>> tasks;
std::unordered_map<int64_t, std::unique_ptr<OfflineDownload>> downloads;
DefaultFileSourceRevalidationState revalidationState;
};

DefaultFileSource::DefaultFileSource(const std::string& cachePath,
Expand Down Expand Up @@ -200,6 +218,14 @@ void DefaultFileSource::setResourceTransform(std::function<std::string(Resource:
});
}

void DefaultFileSource::setRevalidationState(DefaultFileSourceRevalidationState state) {
thread->invokeSync(&Impl::setRevalidationState, state);
}

DefaultFileSourceRevalidationState DefaultFileSource::getRevalidationState() const {
return thread->invokeSync(&Impl::getRevalidationState);
}

std::unique_ptr<AsyncRequest> DefaultFileSource::request(const Resource& resource, Callback callback) {
class DefaultFileRequest : public AsyncRequest {
public:
Expand Down
4 changes: 4 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,8 @@ - (void)sleepGL:(__unused NSNotification *)notification
{
self.dormant = YES;

[MGLOfflineStorage sharedOfflineStorage].mbglFileSource->setRevalidationState(mbgl::DefaultFileSourceRevalidationState::Inactive);

[self validateLocationServices];

[MGLMapboxEvents flush];
Expand Down Expand Up @@ -1107,6 +1109,8 @@ - (void)wakeGL:(__unused NSNotification *)notification
{
self.dormant = NO;

[MGLOfflineStorage sharedOfflineStorage].mbglFileSource->setRevalidationState(mbgl::DefaultFileSourceRevalidationState::Active);

[self createGLView];

self.glSnapshotView.hidden = YES;
Expand Down
29 changes: 29 additions & 0 deletions test/storage/default_file_source.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,32 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(SetResourceTransform)) {

loop.run();
}

// Test that if we make a request when the file source is inactive, no
// invalidation occurs and not request is actually made
TEST(DefaultFileSource, NoInvalidationIfInactive) {
util::RunLoop loop;
DefaultFileSource fs(":memory:", ".");

fs.setRevalidationState(DefaultFileSourceRevalidationState::Inactive);
EXPECT_EQ(fs.getRevalidationState(), DefaultFileSourceRevalidationState::Inactive);

const Resource optionalResource { Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::Optional };

using namespace std::chrono_literals;

std::unique_ptr<AsyncRequest> req;
req = fs.request(optionalResource, [&](Response res) {
req.reset();
ASSERT_TRUE(res.error.get());
EXPECT_EQ(Response::Error::Reason::Other, res.error->reason);
EXPECT_EQ("File source is inactive", res.error->message);
EXPECT_FALSE(res.data);
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
loop.stop();
});

loop.run();
}

0 comments on commit 8f3abad

Please sign in to comment.