From e7b0b31d58997ce0c849129d07a97cb0740beb7e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 6 Jan 2016 18:01:31 -0800 Subject: [core] Privatize SQLiteCache --- bin/render.cpp | 4 +-- include/mbgl/storage/default_file_source.hpp | 7 +++-- include/mbgl/storage/sqlite_cache.hpp | 44 ---------------------------- platform/android/src/native_map_view.cpp | 4 +-- platform/android/src/native_map_view.hpp | 2 -- platform/default/default_file_source.cpp | 19 +++++++++--- platform/ios/src/MGLMapView.mm | 5 +--- platform/linux/main.cpp | 9 ++---- platform/osx/src/MGLMapView.mm | 5 +--- src/mbgl/storage/sqlite_cache.hpp | 44 ++++++++++++++++++++++++++++ 10 files changed, 70 insertions(+), 73 deletions(-) delete mode 100644 include/mbgl/storage/sqlite_cache.hpp create mode 100644 src/mbgl/storage/sqlite_cache.hpp diff --git a/bin/render.cpp b/bin/render.cpp index 71f74b043c..26fd06d507 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunknown-pragmas" @@ -65,8 +64,7 @@ int main(int argc, char *argv[]) { using namespace mbgl; util::RunLoop loop; - SQLiteCache cache(cache_file); - DefaultFileSource fileSource(&cache); + DefaultFileSource fileSource(cache_file); // Try to load the token from the environment. if (!token.size()) { diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 3689b9e932..83ea4adf5d 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -5,16 +5,17 @@ namespace mbgl { -class FileCache; - class DefaultFileSource : public FileSource { public: - DefaultFileSource(FileCache*, const std::string& root = ""); + DefaultFileSource(const std::string& cachePath = ":memory:", const std::string& root = ""); ~DefaultFileSource() override; void setAccessToken(const std::string&); std::string getAccessToken() const; + void setMaximumCacheSize(uint64_t size); + void setMaximumCacheEntrySize(uint64_t size); + std::unique_ptr request(const Resource&, Callback) override; private: diff --git a/include/mbgl/storage/sqlite_cache.hpp b/include/mbgl/storage/sqlite_cache.hpp deleted file mode 100644 index 6e79d44a33..0000000000 --- a/include/mbgl/storage/sqlite_cache.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef MBGL_STORAGE_DEFAULT_SQLITE_CACHE -#define MBGL_STORAGE_DEFAULT_SQLITE_CACHE - -#include - -#include - -namespace mbgl { - -namespace util { -template class Thread; -} // namespace util - -class SQLiteCache : public FileCache { -public: - SQLiteCache(const std::string &path = ":memory:"); - ~SQLiteCache() override; - - 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; - - class Impl; - -private: - const std::unique_ptr> thread; -}; - -class SharedSQLiteCache : util::noncopyable { -public: - static std::shared_ptr get(const std::string &path = ":memory:"); - -private: - SharedSQLiteCache() {} - - static std::weak_ptr masterPtr; -}; - -} // namespace mbgl - -#endif diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 30fdee81b9..96b48e12c6 100644 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -75,8 +75,7 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a return; } - fileCache = mbgl::SharedSQLiteCache::get(mbgl::android::cachePath + "/mbgl-cache.db"); - fileSource = std::make_unique(fileCache.get()); + fileSource = std::make_unique(mbgl::android::cachePath + "/mbgl-cache.db"); map = std::make_unique(*this, *fileSource, MapMode::Continuous); float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1; @@ -103,7 +102,6 @@ NativeMapView::~NativeMapView() { map.reset(); fileSource.reset(); - fileCache.reset(); jint ret; JNIEnv *env = nullptr; diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 7b5473d48c..cc88403c39 100644 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -91,7 +90,6 @@ private: JNIEnv *renderEnv = nullptr; // Ensure these are initialised last - std::shared_ptr fileCache; std::unique_ptr fileSource; std::unique_ptr map; }; diff --git a/platform/default/default_file_source.cpp b/platform/default/default_file_source.cpp index b2ab5abd6c..d2bbce6002 100644 --- a/platform/default/default_file_source.cpp +++ b/platform/default/default_file_source.cpp @@ -1,19 +1,22 @@ #include #include +#include namespace mbgl { class DefaultFileSource::Impl { public: - Impl(FileCache* cache, const std::string& root) - : onlineFileSource(cache, root) { + Impl(const std::string& cachePath, const std::string& root) + : cache(SharedSQLiteCache::get(cachePath)), + onlineFileSource(cache.get(), root) { } + std::shared_ptr cache; OnlineFileSource onlineFileSource; }; -DefaultFileSource::DefaultFileSource(FileCache* cache, const std::string& root) - : impl(std::make_unique(cache, root)) { +DefaultFileSource::DefaultFileSource(const std::string& cachePath, const std::string& root) + : impl(std::make_unique(cachePath, root)) { } DefaultFileSource::~DefaultFileSource() = default; @@ -26,6 +29,14 @@ std::string DefaultFileSource::getAccessToken() const { return impl->onlineFileSource.getAccessToken(); } +void DefaultFileSource::setMaximumCacheSize(uint64_t size) { + impl->cache->setMaximumCacheSize(size); +} + +void DefaultFileSource::setMaximumCacheEntrySize(uint64_t size) { + impl->cache->setMaximumCacheEntrySize(size); +} + std::unique_ptr DefaultFileSource::request(const Resource& resource, Callback callback) { return impl->onlineFileSource.request(resource, callback); } diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 7fe040b0f1..4ee12942b4 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -154,7 +153,6 @@ public: { mbgl::Map *_mbglMap; MBGLView *_mbglView; - std::shared_ptr _mbglFileCache; mbgl::DefaultFileSource *_mbglFileSource; BOOL _opaque; @@ -281,8 +279,7 @@ std::chrono::steady_clock::duration MGLDurationInSeconds(float duration) NSString *libraryDirectory = [paths objectAtIndex:0]; fileCachePath = [libraryDirectory stringByAppendingPathComponent:@"cache.db"]; } - _mbglFileCache = mbgl::SharedSQLiteCache::get([fileCachePath UTF8String]); - _mbglFileSource = new mbgl::DefaultFileSource(_mbglFileCache.get()); + _mbglFileSource = new mbgl::DefaultFileSource([fileCachePath UTF8String]); // setup mbgl map _mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous); diff --git a/platform/linux/main.cpp b/platform/linux/main.cpp index 45f2b8defa..d6cf10fe70 100644 --- a/platform/linux/main.cpp +++ b/platform/linux/main.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -106,11 +105,9 @@ int main(int argc, char *argv[]) { view = std::make_unique(fullscreen, benchmark); - mbgl::SQLiteCache cache("/tmp/mbgl-cache.db"); - cache.setMaximumCacheEntrySize(1 * 1024 * 1024); // 1 MB - cache.setMaximumCacheSize(50 * 1024 * 1024); // 50 MB - - mbgl::DefaultFileSource fileSource(&cache); + mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db"); + fileSource.setMaximumCacheEntrySize(1 * 1024 * 1024); // 1 MB + fileSource.setMaximumCacheSize(50 * 1024 * 1024); // 50 MB // Set access token if present const char *token = getenv("MAPBOX_ACCESS_TOKEN"); diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm index 7646e524a3..7215963fb3 100644 --- a/platform/osx/src/MGLMapView.mm +++ b/platform/osx/src/MGLMapView.mm @@ -22,7 +22,6 @@ #import #import #import -#import #import #import #import @@ -152,7 +151,6 @@ public: /// Cross-platform map view controller. mbgl::Map *_mbglMap; MGLMapViewImpl *_mbglView; - std::shared_ptr _mbglFileCache; mbgl::DefaultFileSource *_mbglFileSource; NSPanGestureRecognizer *_panGestureRecognizer; @@ -248,8 +246,7 @@ public: error:nil]; NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:@"cache.db"]; NSString *cachePath = cacheURL ? cacheURL.path : @""; - _mbglFileCache = mbgl::SharedSQLiteCache::get(cachePath.UTF8String); - _mbglFileSource = new mbgl::DefaultFileSource(_mbglFileCache.get()); + _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String); _mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous); diff --git a/src/mbgl/storage/sqlite_cache.hpp b/src/mbgl/storage/sqlite_cache.hpp new file mode 100644 index 0000000000..6e79d44a33 --- /dev/null +++ b/src/mbgl/storage/sqlite_cache.hpp @@ -0,0 +1,44 @@ +#ifndef MBGL_STORAGE_DEFAULT_SQLITE_CACHE +#define MBGL_STORAGE_DEFAULT_SQLITE_CACHE + +#include + +#include + +namespace mbgl { + +namespace util { +template class Thread; +} // namespace util + +class SQLiteCache : public FileCache { +public: + SQLiteCache(const std::string &path = ":memory:"); + ~SQLiteCache() override; + + 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; + + class Impl; + +private: + const std::unique_ptr> thread; +}; + +class SharedSQLiteCache : util::noncopyable { +public: + static std::shared_ptr get(const std::string &path = ":memory:"); + +private: + SharedSQLiteCache() {} + + static std::weak_ptr masterPtr; +}; + +} // namespace mbgl + +#endif -- cgit v1.2.1