From 78ea88d757ecf3a0a75b786ae343d746617ba46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 9 Oct 2017 12:59:27 +0200 Subject: [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. --- include/mbgl/storage/default_file_source.hpp | 2 +- include/mbgl/storage/file_source.hpp | 6 ++-- include/mbgl/storage/resource.hpp | 43 +++++++++++++++++++++++----- include/mbgl/tile/tile_necessity.hpp | 15 ++++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 include/mbgl/tile/tile_necessity.hpp (limited to 'include') diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 91e442cf85..b9c8de5052 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -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; } diff --git a/include/mbgl/storage/file_source.hpp b/include/mbgl/storage/file_source.hpp index 404c683fdb..0709a1c245 100644 --- a/include/mbgl/storage/file_source.hpp +++ b/include/mbgl/storage/file_source.hpp @@ -24,11 +24,11 @@ public: // not be executed. virtual std::unique_ptr 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; } }; diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp index 5d44f4869f..318fa389f4 100644 --- a/include/mbgl/storage/resource.hpp +++ b/include/mbgl/storage/resource.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include @@ -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_ = {}, Necessity necessity_ = Required) + Resource(Kind kind_, + std::string url_, + optional 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& 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 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 diff --git a/include/mbgl/tile/tile_necessity.hpp b/include/mbgl/tile/tile_necessity.hpp new file mode 100644 index 0000000000..e51bf51d10 --- /dev/null +++ b/include/mbgl/tile/tile_necessity.hpp @@ -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 -- cgit v1.2.1