[core] std::shared_ptr<const Response> ⇢ const Response&

This commit is contained in:
John Firebaugh
2016-01-13 16:31:54 -08:00
parent 7fb5e973ed
commit 2ee1fe4178
7 changed files with 46 additions and 51 deletions

View File

@@ -256,7 +256,7 @@ void OnlineFileRequestImpl::scheduleRealRequest(OnlineFileSource::Impl& impl, bo
if (response && response_->data == response->data) {
hint = SQLiteCache::Hint::Refresh;
}
impl.cache->put(resource, response_, hint);
impl.cache->put(resource, *response_, hint);
}
response = response_;

View File

@@ -367,24 +367,24 @@ void SQLiteCache::Impl::get(const Resource &resource, Callback callback) {
}
}
void SQLiteCache::put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) {
void SQLiteCache::put(const Resource &resource, const Response& response, Hint hint) {
// Can be called from any thread, but most likely from the file source thread. We are either
// storing a new response or updating the currently stored response, potentially setting a new
// expiry date.
if (hint == Hint::Full) {
thread->invoke(&Impl::put, resource, response);
} else if (hint == Hint::Refresh) {
thread->invoke(&Impl::refresh, resource, response->expires);
thread->invoke(&Impl::refresh, resource, response.expires);
}
}
void SQLiteCache::Impl::put(const Resource& resource, std::shared_ptr<const Response> response) {
void SQLiteCache::Impl::put(const Resource& resource, const Response& response) {
try {
initializeDatabase();
pruneEntries();
if (response->data) {
auto entrySize = response->data->size();
if (response.data) {
auto entrySize = response.data->size();
if (entrySize > maximumCacheEntrySize) {
Log::Warning(Event::Database, "Entry too big for caching.");
@@ -408,30 +408,30 @@ void SQLiteCache::Impl::put(const Resource& resource, std::shared_ptr<const Resp
const auto canonicalURL = util::mapbox::canonicalURL(resource.url);
putStmt->bind(1 /* url */, canonicalURL.c_str());
if (response->error) {
putStmt->bind(2 /* status */, int(response->error->reason));
if (response.error) {
putStmt->bind(2 /* status */, int(response.error->reason));
} else {
putStmt->bind(2 /* status */, 1 /* success */);
}
putStmt->bind(3 /* kind */, int(resource.kind));
putStmt->bind(4 /* modified */, int64_t(response->modified.count()));
putStmt->bind(5 /* etag */, response->etag.c_str());
putStmt->bind(6 /* expires */, int64_t(response->expires.count()));
putStmt->bind(4 /* modified */, int64_t(response.modified.count()));
putStmt->bind(5 /* etag */, response.etag.c_str());
putStmt->bind(6 /* expires */, int64_t(response.expires.count()));
putStmt->bind(7 /* accessed */, int64_t(toSeconds(SystemClock::now()).count()));
std::string data;
if (resource.kind != Resource::SpriteImage && response->data) {
if (resource.kind != Resource::SpriteImage && response.data) {
// Do not compress images, since they are typically compressed already.
data = util::compress(*response->data);
data = util::compress(*response.data);
}
if (!data.empty() && data.size() < response->data->size()) {
if (!data.empty() && data.size() < response.data->size()) {
// Store the compressed data when it is smaller than the original
// uncompressed data.
putStmt->bind(8 /* data */, data, false); // do not retain the string internally.
putStmt->bind(9 /* compressed */, true);
} else if (response->data) {
putStmt->bind(8 /* data */, *response->data, false); // do not retain the string internally.
} else if (response.data) {
putStmt->bind(8 /* data */, *response.data, false); // do not retain the string internally.
putStmt->bind(9 /* compressed */, false);
} else {
putStmt->bind(8 /* data */, "", false);

View File

@@ -22,8 +22,8 @@ public:
void setMaximumCacheEntrySize(uint64_t size);
void get(const Resource&, Callback);
void put(const Resource& resource, std::shared_ptr<const Response> response);
void refresh(const Resource& resource, Seconds expires);
void put(const Resource&, const Response&);
void refresh(const Resource&, Seconds expires);
private:
void initializeDatabase();

View File

@@ -30,7 +30,7 @@ public:
using Callback = std::function<void(std::unique_ptr<Response>)>;
std::unique_ptr<WorkRequest> get(const Resource&, Callback);
void put(const Resource&, std::shared_ptr<const Response> response, Hint hint);
void put(const Resource&, const Response&, Hint hint);
class Impl;

View File

@@ -64,8 +64,8 @@ TEST_F(Storage, CacheNotFound) {
const Resource resource{ Resource::Unknown, "http://127.0.0.1:3000/not-found" };
// Insert existing data into the cache that will be marked as stale.
auto response = std::make_shared<Response>();
response->data = std::make_shared<const std::string>("existing data");
Response response;
response.data = std::make_shared<const std::string>("existing data");
cache.put(resource, response, SQLiteCache::Hint::Full);
std::unique_ptr<FileRequest> req1;
@@ -122,8 +122,8 @@ TEST_F(Storage, DontCacheConnectionErrors) {
const Resource resource{ Resource::Unknown, "http://127.0.0.1:3001" };
// Insert existing data into the cache that will be marked as stale.
auto response = std::make_shared<Response>();
response->data = std::make_shared<const std::string>("existing data");
Response response;
response.data = std::make_shared<const std::string>("existing data");
cache.put(resource, response, SQLiteCache::Hint::Full);
std::unique_ptr<FileRequest> req1;
@@ -178,8 +178,8 @@ TEST_F(Storage, DontCacheServerErrors) {
const Resource resource{ Resource::Unknown, "http://127.0.0.1:3000/permanent-error" };
// Insert existing data into the cache that will be marked as stale.
auto response = std::make_shared<Response>();
response->data = std::make_shared<const std::string>("existing data");
Response response;
response.data = std::make_shared<const std::string>("existing data");
cache.put(resource, response, SQLiteCache::Hint::Full);
std::unique_ptr<FileRequest> req1;

View File

@@ -38,10 +38,10 @@ void insertTile(mbgl::SQLiteCache* cache, unsigned id, uint64_t size) {
auto url = std::string("http://tile") + mbgl::util::toString(id);
auto response = std::make_shared<Response>();
response->modified = toSeconds(SystemClock::now());
response->expires = toSeconds(SystemClock::now()) + Seconds(5000);
response->etag = url;
Response response;
response.modified = toSeconds(SystemClock::now());
response.expires = toSeconds(SystemClock::now()) + Seconds(5000);
response.etag = url;
auto data = std::make_shared<std::string>(size, 0);
@@ -50,7 +50,7 @@ void insertTile(mbgl::SQLiteCache* cache, unsigned id, uint64_t size) {
static std::mt19937 generator;
std::generate_n(data->begin(), size, generator);
response->data = data;
response.data = data;
Resource resource{ Resource::Kind::Tile, url };
cache->put(resource, response, SQLiteCache::Hint::Full);
@@ -61,9 +61,9 @@ void refreshTile(mbgl::SQLiteCache* cache, unsigned id) {
auto url = std::string("http://tile") + mbgl::util::toString(id);
auto response = std::make_shared<Response>();
response->modified = toSeconds(SystemClock::now());
response->expires = toSeconds(SystemClock::now()) + Seconds(5000);
Response response;
response.modified = toSeconds(SystemClock::now());
response.expires = toSeconds(SystemClock::now()) + Seconds(5000);
Resource resource{ Resource::Kind::Tile, url };
cache->put(resource, response, SQLiteCache::Hint::Refresh);

View File

@@ -75,9 +75,7 @@ TEST_F(Storage, DatabaseVersion) {
{
SQLiteCache::Impl cache(path);
auto response = std::make_shared<Response>();
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.put({ Resource::Unknown, "mapbox://test" }, Response());
}
sqlite3* db;
@@ -193,8 +191,7 @@ TEST_F(Storage, DatabaseLockedWrite) {
// Adds a file (which should fail).
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.put({ Resource::Unknown, "mapbox://test" }, Response());
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
EXPECT_EQ(nullptr, res.get());
});
@@ -211,8 +208,8 @@ TEST_F(Storage, DatabaseLockedWrite) {
// Then, set a file and obtain it again.
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
response->data = std::make_shared<std::string>("Demo");
Response response;
response.data = std::make_shared<std::string>("Demo");
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
ASSERT_NE(nullptr, res.get());
@@ -244,8 +241,8 @@ TEST_F(Storage, DatabaseLockedRefresh) {
// Adds a file.
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
response->data = std::make_shared<std::string>("Demo");
Response response;
response.data = std::make_shared<std::string>("Demo");
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
EXPECT_EQ(nullptr, res.get());
@@ -260,9 +257,7 @@ TEST_F(Storage, DatabaseLockedRefresh) {
// Then, try to refresh it.
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
response->data = std::make_shared<std::string>("Demo");
cache.refresh({ Resource::Unknown, "mapbox://test" }, response->expires);
cache.refresh({ Resource::Unknown, "mapbox://test" }, Seconds::zero());
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
EXPECT_EQ(nullptr, res.get());
});
@@ -289,8 +284,8 @@ TEST_F(Storage, DatabaseDeleted) {
// Adds a file.
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
response->data = std::make_shared<std::string>("Demo");
Response response;
response.data = std::make_shared<std::string>("Demo");
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
ASSERT_NE(nullptr, res.get());
@@ -307,8 +302,8 @@ TEST_F(Storage, DatabaseDeleted) {
// Adds a file.
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
response->data = std::make_shared<std::string>("Demo");
Response response;
response.data = std::make_shared<std::string>("Demo");
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
ASSERT_NE(nullptr, res.get());
@@ -338,8 +333,8 @@ TEST_F(Storage, DatabaseInvalid) {
// Adds a file.
Log::setObserver(std::make_unique<FixtureLogObserver>());
auto response = std::make_shared<Response>();
response->data = std::make_shared<std::string>("Demo");
Response response;
response.data = std::make_shared<std::string>("Demo");
cache.put({ Resource::Unknown, "mapbox://test" }, response);
cache.get({ Resource::Unknown, "mapbox://test" }, [] (std::unique_ptr<Response> res) {
ASSERT_NE(nullptr, res.get());