[core] Privatize SQLiteCache

This commit is contained in:
John Firebaugh
2016-01-06 18:01:31 -08:00
parent 0a8fef1f51
commit e7b0b31d58
9 changed files with 26 additions and 29 deletions

View File

@@ -6,7 +6,6 @@
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#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()) {

View File

@@ -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<FileRequest> request(const Resource&, Callback) override;
private:

View File

@@ -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<mbgl::DefaultFileSource>(fileCache.get());
fileSource = std::make_unique<mbgl::DefaultFileSource>(mbgl::android::cachePath + "/mbgl-cache.db");
map = std::make_unique<mbgl::Map>(*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;

View File

@@ -4,7 +4,6 @@
#include <mbgl/map/map.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <string>
@@ -91,7 +90,6 @@ private:
JNIEnv *renderEnv = nullptr;
// Ensure these are initialised last
std::shared_ptr<mbgl::SQLiteCache> fileCache;
std::unique_ptr<mbgl::DefaultFileSource> fileSource;
std::unique_ptr<mbgl::Map> map;
};

View File

@@ -1,19 +1,22 @@
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
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<SQLiteCache> cache;
OnlineFileSource onlineFileSource;
};
DefaultFileSource::DefaultFileSource(FileCache* cache, const std::string& root)
: impl(std::make_unique<DefaultFileSource::Impl>(cache, root)) {
DefaultFileSource::DefaultFileSource(const std::string& cachePath, const std::string& root)
: impl(std::make_unique<DefaultFileSource::Impl>(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<FileRequest> DefaultFileSource::request(const Resource& resource, Callback callback) {
return impl->onlineFileSource.request(resource, callback);
}

View File

@@ -16,7 +16,6 @@
#include <mbgl/map/mode.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/darwin/reachability.h>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/geo.hpp>
@@ -154,7 +153,6 @@ public:
{
mbgl::Map *_mbglMap;
MBGLView *_mbglView;
std::shared_ptr<mbgl::SQLiteCache> _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);

View File

@@ -5,7 +5,6 @@
#include <mbgl/platform/default/settings_json.hpp>
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <signal.h>
#include <getopt.h>
@@ -106,11 +105,9 @@ int main(int argc, char *argv[]) {
view = std::make_unique<GLFWView>(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");

View File

@@ -22,7 +22,6 @@
#import <mbgl/sprite/sprite_image.hpp>
#import <mbgl/storage/default_file_source.hpp>
#import <mbgl/storage/network_status.hpp>
#import <mbgl/storage/sqlite_cache.hpp>
#import <mbgl/util/constants.hpp>
#import <mbgl/util/math.hpp>
#import <mbgl/util/std.hpp>
@@ -152,7 +151,6 @@ public:
/// Cross-platform map view controller.
mbgl::Map *_mbglMap;
MGLMapViewImpl *_mbglView;
std::shared_ptr<mbgl::SQLiteCache> _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);