From 7fb5e973edbf546046defb67dd28f1275326319b Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 11 Jan 2016 17:20:07 -0800 Subject: [PATCH] [core] Eliminate FileCache interface There is only one implementation and we're unlikely to add more. --- include/mbgl/storage/file_cache.hpp | 28 --------------------- include/mbgl/storage/online_file_source.hpp | 5 ++-- platform/default/online_file_source.cpp | 13 +++++----- src/mbgl/storage/request_base.hpp | 1 - src/mbgl/storage/sqlite_cache.hpp | 21 +++++++++++----- test/storage/cache_response.cpp | 6 ++--- 6 files changed, 28 insertions(+), 46 deletions(-) delete mode 100644 include/mbgl/storage/file_cache.hpp diff --git a/include/mbgl/storage/file_cache.hpp b/include/mbgl/storage/file_cache.hpp deleted file mode 100644 index 65d6dfbff..000000000 --- a/include/mbgl/storage/file_cache.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef MBGL_STORAGE_FILE_CACHE -#define MBGL_STORAGE_FILE_CACHE - -#include - -#include -#include - -namespace mbgl { - -struct Resource; -class Response; -class WorkRequest; - -class FileCache : private util::noncopyable { -public: - virtual ~FileCache() = default; - - enum class Hint : bool { Full, Refresh }; - using Callback = std::function)>; - - virtual std::unique_ptr get(const Resource &resource, Callback callback) = 0; - virtual void put(const Resource &resource, std::shared_ptr response, Hint hint) = 0; -}; - -} // namespace mbgl - -#endif diff --git a/include/mbgl/storage/online_file_source.hpp b/include/mbgl/storage/online_file_source.hpp index e5253bc3f..bdf14c7b3 100644 --- a/include/mbgl/storage/online_file_source.hpp +++ b/include/mbgl/storage/online_file_source.hpp @@ -2,17 +2,18 @@ #define MBGL_STORAGE_ONLINE_FILE_SOURCE #include -#include namespace mbgl { +class SQLiteCache; + namespace util { template class Thread; } // namespace util class OnlineFileSource : public FileSource { public: - OnlineFileSource(FileCache*); + OnlineFileSource(SQLiteCache*); ~OnlineFileSource() override; void setAccessToken(const std::string& t) { accessToken = t; } diff --git a/platform/default/online_file_source.cpp b/platform/default/online_file_source.cpp index 330ecba09..30f3701d2 100644 --- a/platform/default/online_file_source.cpp +++ b/platform/default/online_file_source.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -68,7 +69,7 @@ class OnlineFileSource::Impl { public: using Callback = std::function; - Impl(FileCache*); + Impl(SQLiteCache*); ~Impl(); void networkIsReachableAgain(); @@ -80,12 +81,12 @@ private: friend OnlineFileRequestImpl; std::unordered_map> pending; - FileCache* const cache; + SQLiteCache* const cache; const std::unique_ptr httpContext; util::AsyncTask reachability; }; -OnlineFileSource::OnlineFileSource(FileCache* cache) +OnlineFileSource::OnlineFileSource(SQLiteCache* cache) : thread(std::make_unique>( util::ThreadContext{ "OnlineFileSource", util::ThreadType::Unknown, util::ThreadPriority::Low }, cache)) { @@ -134,7 +135,7 @@ void OnlineFileSource::cancel(FileRequest* req) { // ----- Impl ----- -OnlineFileSource::Impl::Impl(FileCache* cache_) +OnlineFileSource::Impl::Impl(SQLiteCache* cache_) : cache(cache_), httpContext(HTTPContextBase::createContext()), reachability(std::bind(&Impl::networkIsReachableAgain, this)) { @@ -251,9 +252,9 @@ void OnlineFileRequestImpl::scheduleRealRequest(OnlineFileSource::Impl& impl, bo (!response_->error || (response_->error->reason == Response::Error::Reason::NotFound))) { // Store response in database. Make sure we only refresh the expires column if the data // didn't change. - FileCache::Hint hint = FileCache::Hint::Full; + SQLiteCache::Hint hint = SQLiteCache::Hint::Full; if (response && response_->data == response->data) { - hint = FileCache::Hint::Refresh; + hint = SQLiteCache::Hint::Refresh; } impl.cache->put(resource, response_, hint); } diff --git a/src/mbgl/storage/request_base.hpp b/src/mbgl/storage/request_base.hpp index a0476d49b..e3d33571d 100644 --- a/src/mbgl/storage/request_base.hpp +++ b/src/mbgl/storage/request_base.hpp @@ -2,7 +2,6 @@ #define MBGL_STORAGE_REQUEST_BASE #include -#include #include #include diff --git a/src/mbgl/storage/sqlite_cache.hpp b/src/mbgl/storage/sqlite_cache.hpp index 6e79d44a3..ed94dfbc8 100644 --- a/src/mbgl/storage/sqlite_cache.hpp +++ b/src/mbgl/storage/sqlite_cache.hpp @@ -1,27 +1,36 @@ #ifndef MBGL_STORAGE_DEFAULT_SQLITE_CACHE #define MBGL_STORAGE_DEFAULT_SQLITE_CACHE -#include +#include +#include +#include +#include #include namespace mbgl { +struct Resource; +class Response; +class WorkRequest; + namespace util { template class Thread; } // namespace util -class SQLiteCache : public FileCache { +class SQLiteCache : private util::noncopyable { public: SQLiteCache(const std::string &path = ":memory:"); - ~SQLiteCache() override; + ~SQLiteCache(); void setMaximumCacheSize(uint64_t size); void setMaximumCacheEntrySize(uint64_t size); - // FileCache API - std::unique_ptr get(const Resource &resource, Callback callback) override; - void put(const Resource &resource, std::shared_ptr response, Hint hint) override; + enum class Hint : bool { Full, Refresh }; + using Callback = std::function)>; + + std::unique_ptr get(const Resource&, Callback); + void put(const Resource&, std::shared_ptr response, Hint hint); class Impl; diff --git a/test/storage/cache_response.cpp b/test/storage/cache_response.cpp index b27f1caa9..4f68edf0c 100644 --- a/test/storage/cache_response.cpp +++ b/test/storage/cache_response.cpp @@ -66,7 +66,7 @@ TEST_F(Storage, CacheNotFound) { // Insert existing data into the cache that will be marked as stale. auto response = std::make_shared(); response->data = std::make_shared("existing data"); - cache.put(resource, response, FileCache::Hint::Full); + cache.put(resource, response, SQLiteCache::Hint::Full); std::unique_ptr req1; std::unique_ptr req2; @@ -124,7 +124,7 @@ TEST_F(Storage, DontCacheConnectionErrors) { // Insert existing data into the cache that will be marked as stale. auto response = std::make_shared(); response->data = std::make_shared("existing data"); - cache.put(resource, response, FileCache::Hint::Full); + cache.put(resource, response, SQLiteCache::Hint::Full); std::unique_ptr req1; std::unique_ptr req2; @@ -180,7 +180,7 @@ TEST_F(Storage, DontCacheServerErrors) { // Insert existing data into the cache that will be marked as stale. auto response = std::make_shared(); response->data = std::make_shared("existing data"); - cache.put(resource, response, FileCache::Hint::Full); + cache.put(resource, response, SQLiteCache::Hint::Full); std::unique_ptr req1; std::unique_ptr req2;