Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] move from microsecond precision timestamp to integer second pr…
Browse files Browse the repository at this point in the history
…ecision
  • Loading branch information
kkaefer committed May 13, 2016
1 parent c1dde52 commit cd65a43
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 93 deletions.
6 changes: 3 additions & 3 deletions bin/offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main(int argc, char *argv[]) {
: region(region_),
fileSource(fileSource_),
loop(loop_),
start(SystemClock::now()) {
start(util::now()) {
}

void statusChanged(OfflineRegionStatus status) override {
Expand All @@ -82,7 +82,7 @@ int main(int argc, char *argv[]) {

std::string bytesPerSecond = "-";

auto elapsedSeconds = (SystemClock::now() - start) / 1s;
auto elapsedSeconds = (util::now() - start) / 1s;
if (elapsedSeconds != 0) {
bytesPerSecond = util::toString(status.completedResourceSize / elapsedSeconds);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ int main(int argc, char *argv[]) {
OfflineRegion& region;
DefaultFileSource& fileSource;
util::RunLoop& loop;
SystemTimePoint start;
Timestamp start;
};

static auto stop = [&] {
Expand Down
16 changes: 12 additions & 4 deletions include/mbgl/storage/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ class Resource {
int8_t z;
};

Resource(Kind kind_, const std::string& url_, optional<TileData> tileData_ = {})
enum Necessity : bool {
Optional = false,
Required = true,
};

Resource(Kind kind_, const std::string& url_, optional<TileData> tileData_ = {}, Necessity necessity_ = Required)
: kind(kind_),
necessity(necessity_),
url(url_),
tileData(std::move(tileData_)) {
}
Expand All @@ -41,21 +47,23 @@ class Resource {
float pixelRatio,
int32_t x,
int32_t y,
int8_t z);
int8_t z,
Necessity = Required);
static Resource glyphs(const std::string& urlTemplate,
const FontStack& fontStack,
const std::pair<uint16_t, uint16_t>& glyphRange);
static Resource spriteImage(const std::string& base, float pixelRatio);
static Resource spriteJSON(const std::string& base, float pixelRatio);

Kind kind;
Necessity necessity;
std::string url;

// Includes auxiliary data if this is a tile request.
optional<TileData> tileData;

optional<SystemTimePoint> priorModified = {};
optional<SystemTimePoint> priorExpires = {};
optional<Timestamp> priorModified = {};
optional<Timestamp> priorExpires = {};
optional<std::string> priorEtag = {};
};

Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/storage/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Response {
// The actual data of the response. Present only for non-error, non-notModified responses.
std::shared_ptr<const std::string> data;

optional<SystemTimePoint> modified;
optional<SystemTimePoint> expires;
optional<Timestamp> modified;
optional<Timestamp> expires;
optional<std::string> etag;
};

Expand Down
15 changes: 9 additions & 6 deletions include/mbgl/util/chrono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@
namespace mbgl {

using Clock = std::chrono::steady_clock;
using SystemClock = std::chrono::system_clock;

using Seconds = std::chrono::seconds;
using Milliseconds = std::chrono::milliseconds;

using TimePoint = Clock::time_point;
using Duration = Clock::duration;

using SystemTimePoint = SystemClock::time_point;
using SystemDuration = SystemClock::duration;
// Used to measure second-precision times, such as times gathered from HTTP responses.
using Timestamp = std::chrono::time_point<std::chrono::system_clock, Seconds>;

namespace util {

inline Timestamp now() {
return std::chrono::time_point_cast<Seconds>(std::chrono::system_clock::now());
}

// Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT"
std::string rfc1123(SystemTimePoint);
std::string rfc1123(Timestamp);

// YYYY-mm-dd HH:MM:SS e.g. "2015-11-26 16:11:23"
std::string iso8601(SystemTimePoint);
std::string iso8601(Timestamp);

SystemTimePoint parseTimePoint(const char *);
Timestamp parseTimestamp(const char *);

// C++17 polyfill
template <class Rep, class Period, class = std::enable_if_t<
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/util/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ constexpr double MAX_ZOOM = 25.5;
constexpr uint64_t DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024;;

constexpr Duration DEFAULT_FADE_DURATION = Milliseconds(300);
constexpr SystemDuration CLOCK_SKEW_RETRY_TIMEOUT = Seconds(30);
constexpr Seconds CLOCK_SKEW_RETRY_TIMEOUT { 30 };

} // namespace util

Expand Down
4 changes: 2 additions & 2 deletions platform/android/src/http_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ void HTTPRequest::onResponse(jni::JNIEnv& env, int code,
}

if (modified) {
response.modified = util::parseTimePoint(jni::Make<std::string>(env, modified).c_str());
response.modified = util::parseTimestamp(jni::Make<std::string>(env, modified).c_str());
}

if (cacheControl) {
response.expires = http::CacheControl::parse(jni::Make<std::string>(env, cacheControl).c_str()).toTimePoint();
}

if (expires) {
response.expires = util::parseTimePoint(jni::Make<std::string>(env, expires).c_str());
response.expires = util::parseTimestamp(jni::Make<std::string>(env, expires).c_str());
}

if (code == 200) {
Expand Down
4 changes: 2 additions & 2 deletions platform/darwin/src/http_file_source.mm
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ void cancel() {

NSString *expires = [headers objectForKey:@"Expires"];
if (expires) {
response.expires = util::parseTimePoint([expires UTF8String]);
response.expires = util::parseTimestamp([expires UTF8String]);
}

NSString *last_modified = [headers objectForKey:@"Last-Modified"];
if (last_modified) {
response.modified = util::parseTimePoint([last_modified UTF8String]);
response.modified = util::parseTimestamp([last_modified UTF8String]);
}

NSString *etag = [headers objectForKey:@"ETag"];
Expand Down
4 changes: 2 additions & 2 deletions platform/default/http_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ size_t HTTPRequest::headerCallback(char *const buffer, const size_t size, const
// Always overwrite the modification date; We might already have a value here from the
// Date header, but this one is more accurate.
const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n
baton->response->modified = SystemClock::from_time_t(curl_getdate(value.c_str(), nullptr));
baton->response->modified = Timestamp{ Seconds(curl_getdate(value.c_str(), nullptr)) };
} else if ((begin = headerMatches("etag: ", buffer, length)) != std::string::npos) {
baton->response->etag = std::string(buffer + begin, length - begin - 2); // remove \r\n
} else if ((begin = headerMatches("cache-control: ", buffer, length)) != std::string::npos) {
const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n
baton->response->expires = http::CacheControl::parse(value.c_str()).toTimePoint();
} else if ((begin = headerMatches("expires: ", buffer, length)) != std::string::npos) {
const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n
baton->response->expires = SystemClock::from_time_t(curl_getdate(value.c_str(), nullptr));
baton->response->expires = Timestamp{ Seconds(curl_getdate(value.c_str(), nullptr)) };
}

return length;
Expand Down
24 changes: 12 additions & 12 deletions platform/default/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ optional<std::pair<Response, uint64_t>> OfflineDatabase::getResource(const Resou
Statement accessedStmt = getStatement(
"UPDATE resources SET accessed = ?1 WHERE url = ?2");

accessedStmt->bind(1, SystemClock::now());
accessedStmt->bind(1, util::now());
accessedStmt->bind(2, resource.url);
accessedStmt->run();

Expand All @@ -201,8 +201,8 @@ optional<std::pair<Response, uint64_t>> OfflineDatabase::getResource(const Resou
uint64_t size = 0;

response.etag = stmt->get<optional<std::string>>(0);
response.expires = stmt->get<optional<SystemTimePoint>>(1);
response.modified = stmt->get<optional<SystemTimePoint>>(2);
response.expires = stmt->get<optional<Timestamp>>(1);
response.modified = stmt->get<optional<Timestamp>>(2);

optional<std::string> data = stmt->get<optional<std::string>>(3);
if (!data) {
Expand All @@ -229,7 +229,7 @@ bool OfflineDatabase::putResource(const Resource& resource,
" expires = ?2 "
"WHERE url = ?3 ");

update->bind(1, SystemClock::now());
update->bind(1, util::now());
update->bind(2, response.expires);
update->bind(3, resource.url);
update->run();
Expand Down Expand Up @@ -257,7 +257,7 @@ bool OfflineDatabase::putResource(const Resource& resource,
update->bind(2, response.etag);
update->bind(3, response.expires);
update->bind(4, response.modified);
update->bind(5, SystemClock::now());
update->bind(5, util::now());
update->bind(8, resource.url);

if (response.noContent) {
Expand All @@ -283,7 +283,7 @@ bool OfflineDatabase::putResource(const Resource& resource,
insert->bind(3, response.etag);
insert->bind(4, response.expires);
insert->bind(5, response.modified);
insert->bind(6, SystemClock::now());
insert->bind(6, util::now());

if (response.noContent) {
insert->bind(7, nullptr);
Expand All @@ -309,7 +309,7 @@ optional<std::pair<Response, uint64_t>> OfflineDatabase::getTile(const Resource:
" AND y = ?5 "
" AND z = ?6 ");

accessedStmt->bind(1, SystemClock::now());
accessedStmt->bind(1, util::now());
accessedStmt->bind(2, tile.urlTemplate);
accessedStmt->bind(3, tile.pixelRatio);
accessedStmt->bind(4, tile.x);
Expand Down Expand Up @@ -341,8 +341,8 @@ optional<std::pair<Response, uint64_t>> OfflineDatabase::getTile(const Resource:
uint64_t size = 0;

response.etag = stmt->get<optional<std::string>>(0);
response.expires = stmt->get<optional<SystemTimePoint>>(1);
response.modified = stmt->get<optional<SystemTimePoint>>(2);
response.expires = stmt->get<optional<Timestamp>>(1);
response.modified = stmt->get<optional<Timestamp>>(2);

optional<std::string> data = stmt->get<optional<std::string>>(3);
if (!data) {
Expand Down Expand Up @@ -373,7 +373,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile,
" AND y = ?6 "
" AND z = ?7 ");

update->bind(1, SystemClock::now());
update->bind(1, util::now());
update->bind(2, response.expires);
update->bind(3, tile.urlTemplate);
update->bind(4, tile.pixelRatio);
Expand Down Expand Up @@ -407,7 +407,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile,
update->bind(1, response.modified);
update->bind(2, response.etag);
update->bind(3, response.expires);
update->bind(4, SystemClock::now());
update->bind(4, util::now());
update->bind(7, tile.urlTemplate);
update->bind(8, tile.pixelRatio);
update->bind(9, tile.x);
Expand Down Expand Up @@ -440,7 +440,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile,
insert->bind(6, response.modified);
insert->bind(7, response.etag);
insert->bind(8, response.expires);
insert->bind(9, SystemClock::now());
insert->bind(9, util::now());

if (response.noContent) {
insert->bind(10, nullptr);
Expand Down
22 changes: 11 additions & 11 deletions platform/default/online_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OnlineFileRequest : public AsyncRequest {
~OnlineFileRequest();

void networkIsReachableAgain();
void schedule(optional<SystemTimePoint> expires);
void schedule(optional<Timestamp> expires);
void completed(Response);

OnlineFileSource::Impl& impl;
Expand Down Expand Up @@ -192,7 +192,7 @@ OnlineFileRequest::OnlineFileRequest(const Resource& resource_, Callback callbac
if (resource.priorExpires) {
schedule(resource.priorExpires);
} else {
schedule(SystemClock::now());
schedule(util::now());
}
}

Expand All @@ -214,20 +214,20 @@ static Duration errorRetryTimeout(Response::Error::Reason failedRequestReason, u
}
}

static Duration expirationTimeout(optional<SystemTimePoint> expires, uint32_t expiredRequests) {
static Duration expirationTimeout(optional<Timestamp> expires, uint32_t expiredRequests) {
if (expiredRequests) {
return Seconds(1 << std::min(expiredRequests - 1, 31u));
} else if (expires) {
return std::max(SystemDuration::zero(), *expires - SystemClock::now());
return std::max(Seconds::zero(), *expires - util::now());
} else {
return Duration::max();
}
}

SystemTimePoint interpolateExpiration(const SystemTimePoint& current,
optional<SystemTimePoint> prior,
bool& expired) {
auto now = SystemClock::now();
Timestamp interpolateExpiration(const Timestamp& current,
optional<Timestamp> prior,
bool& expired) {
auto now = util::now();
if (current > now) {
return current;
}
Expand Down Expand Up @@ -256,10 +256,10 @@ SystemTimePoint interpolateExpiration(const SystemTimePoint& current,
// Assume that either the client or server clock is wrong and
// try to interpolate a valid expiration date (from the client POV)
// observing a minimum timeout.
return now + std::max<SystemDuration>(delta, util::CLOCK_SKEW_RETRY_TIMEOUT);
return now + std::max<Seconds>(delta, util::CLOCK_SKEW_RETRY_TIMEOUT);
}

void OnlineFileRequest::schedule(optional<SystemTimePoint> expires) {
void OnlineFileRequest::schedule(optional<Timestamp> expires) {
if (request) {
// There's already a request in progress; don't start another one.
return;
Expand Down Expand Up @@ -339,7 +339,7 @@ void OnlineFileRequest::networkIsReachableAgain() {
// We need all requests to fail at least once before we are going to start retrying
// them, and we only immediately restart request that failed due to connection issues.
if (failedRequestReason == Response::Error::Reason::Connection) {
schedule(SystemClock::now());
schedule(util::now());
}
}

Expand Down
25 changes: 18 additions & 7 deletions platform/default/sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ void Statement::bindBlob(int offset, const std::vector<uint8_t>& value, bool ret
bindBlob(offset, value.data(), value.size(), retain);
}

template <> void Statement::bind(int offset, std::chrono::system_clock::time_point value) {
template <>
void Statement::bind(
int offset, std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> value) {
assert(stmt);
check(sqlite3_bind_int64(stmt, offset, std::chrono::system_clock::to_time_t(value)));
}
Expand All @@ -231,7 +233,10 @@ template <> void Statement::bind(int offset, optional<std::string> value) {
}
}

template <> void Statement::bind(int offset, optional<std::chrono::system_clock::time_point> value) {
template <>
void Statement::bind(
int offset,
optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>> value) {
if (!value) {
bind(offset, nullptr);
} else {
Expand Down Expand Up @@ -283,9 +288,12 @@ template <> std::vector<uint8_t> Statement::get(int offset) {
return { begin, end };
}

template <> std::chrono::system_clock::time_point Statement::get(int offset) {
template <>
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>
Statement::get(int offset) {
assert(stmt);
return std::chrono::system_clock::from_time_t(sqlite3_column_int64(stmt, offset));
return std::chrono::time_point_cast<std::chrono::seconds>(
std::chrono::system_clock::from_time_t(sqlite3_column_int64(stmt, offset)));
}

template <> optional<int64_t> Statement::get(int offset) {
Expand Down Expand Up @@ -315,12 +323,15 @@ template <> optional<std::string> Statement::get(int offset) {
}
}

template <> optional<std::chrono::system_clock::time_point> Statement::get(int offset) {
template <>
optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>>
Statement::get(int offset) {
assert(stmt);
if (sqlite3_column_type(stmt, offset) == SQLITE_NULL) {
return optional<std::chrono::system_clock::time_point>();
return {};
} else {
return get<std::chrono::system_clock::time_point>(offset);
return get<std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>>(
offset);
}
}

Expand Down
Loading

0 comments on commit cd65a43

Please sign in to comment.