[core] make forcing cache/network only more explicit

Previously, we used the existence of a `prior*` field in the Resource object as an indication for whether we should consult the cache or not. However, this is prone to error, since a failed cache lookup won't set any prior fields. Therefore, we manually set `priorExpires` to 0. This in turn triggered another bug where generated wrong expiration timestamps when the server response we got was expired (or expired between sending and receiving).

This commit changes the flags so that we can now explicitly request CacheOnly/NetworkOnly (or All) loading methods, rather than the implicit Optional/Required naming scheme.
This commit is contained in:
Konstantin Käfer
2017-10-09 12:59:27 +02:00
parent 98a47884f0
commit f4cb3b8a4f
25 changed files with 284 additions and 251 deletions

View File

@@ -34,7 +34,7 @@ public:
uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE);
~DefaultFileSource() override;
bool supportsOptionalRequests() const override {
bool supportsCacheOnlyRequests() const override {
return true;
}

View File

@@ -24,11 +24,11 @@ public:
// not be executed.
virtual std::unique_ptr<AsyncRequest> request(const Resource&, Callback) = 0;
// When a file source supports optional requests, it must return true.
// Optional requests are requests that aren't as urgent, but could be useful, e.g.
// When a file source supports consulting a local cache only, it must return true.
// Cache-only requests are requests that aren't as urgent, but could be useful, e.g.
// to cover part of the map while loading. The FileSource should only do cheap actions to
// retrieve the data, e.g. load it from a cache, but not from the internet.
virtual bool supportsOptionalRequests() const {
virtual bool supportsCacheOnlyRequests() const {
return false;
}
};

View File

@@ -4,6 +4,8 @@
#include <mbgl/util/optional.hpp>
#include <mbgl/util/font_stack.hpp>
#include <mbgl/util/tileset.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/traits.hpp>
#include <string>
@@ -30,18 +32,28 @@ public:
int8_t z;
};
enum Necessity : bool {
Optional = false,
Required = true,
enum class LoadingMethod : uint8_t {
None = 0b00,
Cache = 0b01,
Network = 0b10,
CacheOnly = Cache,
NetworkOnly = Network,
All = Cache | Network,
};
Resource(Kind kind_, std::string url_, optional<TileData> tileData_ = {}, Necessity necessity_ = Required)
Resource(Kind kind_,
std::string url_,
optional<TileData> tileData_ = {},
LoadingMethod loadingMethod_ = LoadingMethod::All)
: kind(kind_),
necessity(necessity_),
loadingMethod(loadingMethod_),
url(std::move(url_)),
tileData(std::move(tileData_)) {
}
bool hasLoadingMethod(LoadingMethod method);
static Resource style(const std::string& url);
static Resource source(const std::string& url);
static Resource tile(const std::string& urlTemplate,
@@ -50,7 +62,7 @@ public:
int32_t y,
int8_t z,
Tileset::Scheme scheme,
Necessity = Required);
LoadingMethod = LoadingMethod::All);
static Resource glyphs(const std::string& urlTemplate,
const FontStack& fontStack,
const std::pair<uint16_t, uint16_t>& glyphRange);
@@ -59,7 +71,7 @@ public:
static Resource image(const std::string& url);
Kind kind;
Necessity necessity;
LoadingMethod loadingMethod;
std::string url;
// Includes auxiliary data if this is a tile request.
@@ -71,4 +83,21 @@ public:
std::shared_ptr<const std::string> priorData;
};
MBGL_CONSTEXPR Resource::LoadingMethod operator|(Resource::LoadingMethod a, Resource::LoadingMethod b) {
return Resource::LoadingMethod(mbgl::underlying_type(a) | mbgl::underlying_type(b));
}
MBGL_CONSTEXPR Resource::LoadingMethod& operator|=(Resource::LoadingMethod& a, Resource::LoadingMethod b) {
return (a = a | b);
}
MBGL_CONSTEXPR Resource::LoadingMethod operator&(Resource::LoadingMethod a, Resource::LoadingMethod b) {
return Resource::LoadingMethod(mbgl::underlying_type(a) & mbgl::underlying_type(b));
}
inline bool Resource::hasLoadingMethod(Resource::LoadingMethod method) {
return (loadingMethod & method) != Resource::LoadingMethod::None;
}
} // namespace mbgl

View File

@@ -0,0 +1,15 @@
#pragma once
namespace mbgl {
// Tiles can have two states: optional or required.
// - optional means that only low-cost actions should be taken to obtain the data
// (e.g. load from cache, but accept stale data)
// - required means that every effort should be taken to obtain the data (e.g. load
// from internet and keep the data fresh if it expires)
enum class TileNecessity : bool {
Optional = false,
Required = true,
};
} // namespace mbgl