summaryrefslogtreecommitdiff
path: root/platform/default/mbgl
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-03-30 17:01:54 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-03-30 17:43:37 -0700
commit5eda74a514964d1cac684483bafa08d458175f9a (patch)
treea38f886f5742d52a915c3c72959a5eba50b9fa0e /platform/default/mbgl
parentb6a181097c3e7c3168be3575d5d7e95820fc74ba (diff)
parent7b5a1ca1670a0346cdbf2af689fabde4e70ed561 (diff)
downloadqtlocation-mapboxgl-5eda74a514964d1cac684483bafa08d458175f9a.tar.gz
Merge branch 'release-ios-3.2.0-android-4.0.0'
Diffstat (limited to 'platform/default/mbgl')
-rw-r--r--platform/default/mbgl/storage/offline_database.cpp67
-rw-r--r--platform/default/mbgl/storage/offline_database.hpp2
-rw-r--r--platform/default/mbgl/storage/offline_download.cpp24
-rw-r--r--platform/default/mbgl/storage/offline_download.hpp3
4 files changed, 65 insertions, 31 deletions
diff --git a/platform/default/mbgl/storage/offline_database.cpp b/platform/default/mbgl/storage/offline_database.cpp
index a42591d60e..0b8dec01bf 100644
--- a/platform/default/mbgl/storage/offline_database.cpp
+++ b/platform/default/mbgl/storage/offline_database.cpp
@@ -14,9 +14,6 @@ namespace mbgl {
using namespace mapbox::sqlite;
-// If you change the schema you must write a migration from the previous version.
-static const uint32_t schemaVersion = 2;
-
OfflineDatabase::Statement::~Statement() {
stmt.reset();
stmt.clearBindings();
@@ -50,34 +47,53 @@ void OfflineDatabase::ensureSchema() {
try {
connect(ReadWrite);
- {
- auto userVersionStmt = db->prepare("PRAGMA user_version");
- userVersionStmt.run();
- switch (userVersionStmt.get<int>(0)) {
- case 0: break; // cache-only database; ok to delete
- case 1: break; // cache-only database; ok to delete
- case 2: return;
- default: throw std::runtime_error("unknown schema version");
- }
+ switch (userVersion()) {
+ case 0: break; // cache-only database; ok to delete
+ case 1: break; // cache-only database; ok to delete
+ case 2: migrateToVersion3(); // fall through
+ case 3: return;
+ default: throw std::runtime_error("unknown schema version");
}
removeExisting();
connect(ReadWrite | Create);
} catch (mapbox::sqlite::Exception& ex) {
- if (ex.code == SQLITE_CANTOPEN) {
- connect(ReadWrite | Create);
- } else if (ex.code == SQLITE_NOTADB) {
- removeExisting();
+ if (ex.code != SQLITE_CANTOPEN && ex.code != SQLITE_NOTADB) {
+ Log::Error(Event::Database, "Unexpected error connecting to database: %s", ex.what());
+ throw;
+ }
+
+ try {
+ if (ex.code == SQLITE_NOTADB) {
+ removeExisting();
+ }
connect(ReadWrite | Create);
+ } catch (...) {
+ Log::Error(Event::Database, "Unexpected error creating database: %s", util::toString(std::current_exception()).c_str());
+ throw;
}
}
}
- #include "offline_schema.cpp.include"
+ try {
+ #include "offline_schema.cpp.include"
+
+ connect(ReadWrite | Create);
- connect(ReadWrite | Create);
- db->exec(schema);
- db->exec("PRAGMA user_version = " + util::toString(schemaVersion));
+ // If you change the schema you must write a migration from the previous version.
+ db->exec("PRAGMA auto_vacuum = INCREMENTAL");
+ db->exec(schema);
+ db->exec("PRAGMA user_version = 3");
+ } catch (...) {
+ Log::Error(Event::Database, "Unexpected error creating database schema: %s", util::toString(std::current_exception()).c_str());
+ throw;
+ }
+}
+
+int OfflineDatabase::userVersion() {
+ auto stmt = db->prepare("PRAGMA user_version");
+ stmt.run();
+ return stmt.get<int>(0);
}
void OfflineDatabase::removeExisting() {
@@ -92,6 +108,12 @@ void OfflineDatabase::removeExisting() {
}
}
+void OfflineDatabase::migrateToVersion3() {
+ db->exec("PRAGMA auto_vacuum = INCREMENTAL");
+ db->exec("VACUUM");
+ db->exec("PRAGMA user_version = 3");
+}
+
OfflineDatabase::Statement OfflineDatabase::getStatement(const char * sql) {
auto it = statements.find(sql);
@@ -458,6 +480,7 @@ void OfflineDatabase::deleteRegion(OfflineRegion&& region) {
stmt->run();
evict(0);
+ db->exec("PRAGMA incremental_vacuum");
// Ensure that the cached offlineTileCount value is recalculated.
offlineMapboxTileCount = {};
@@ -614,10 +637,6 @@ bool OfflineDatabase::evict(uint64_t neededFreeSize) {
uint64_t pageSize = getPragma<int64_t>("PRAGMA page_size");
uint64_t pageCount = getPragma<int64_t>("PRAGMA page_count");
- if (pageSize * pageCount > maximumCacheSize) {
- Log::Warning(mbgl::Event::Database, "Current size is larger than the maximum size. Database won't get truncated.");
- }
-
auto usedSize = [&] {
return pageSize * (pageCount - getPragma<int64_t>("PRAGMA freelist_count"));
};
diff --git a/platform/default/mbgl/storage/offline_database.hpp b/platform/default/mbgl/storage/offline_database.hpp
index eb18cc18d2..1e77d560d4 100644
--- a/platform/default/mbgl/storage/offline_database.hpp
+++ b/platform/default/mbgl/storage/offline_database.hpp
@@ -58,8 +58,10 @@ public:
private:
void connect(int flags);
+ int userVersion();
void ensureSchema();
void removeExisting();
+ void migrateToVersion3();
class Statement {
public:
diff --git a/platform/default/mbgl/storage/offline_download.cpp b/platform/default/mbgl/storage/offline_download.cpp
index 0dcffbd9cb..e3fd10503d 100644
--- a/platform/default/mbgl/storage/offline_download.cpp
+++ b/platform/default/mbgl/storage/offline_download.cpp
@@ -228,12 +228,8 @@ void OfflineDownload::ensureResource(const Resource& resource, std::function<voi
return;
}
-
- if (resource.kind == Resource::Kind::Tile
- && util::mapbox::isMapboxURL(resource.url)
- && offlineDatabase.offlineMapboxTileCountLimitExceeded()) {
- observer->mapboxTileCountLimitExceeded(offlineDatabase.getOfflineMapboxTileCountLimit());
- setState(OfflineRegionDownloadState::Inactive);
+
+ if (checkTileCountLimit(resource)) {
return;
}
@@ -255,6 +251,10 @@ void OfflineDownload::ensureResource(const Resource& resource, std::function<voi
observer->statusChanged(status);
+ if (checkTileCountLimit(resource)) {
+ return;
+ }
+
if (status.complete()) {
setState(OfflineRegionDownloadState::Inactive);
}
@@ -262,4 +262,16 @@ void OfflineDownload::ensureResource(const Resource& resource, std::function<voi
});
}
+bool OfflineDownload::checkTileCountLimit(const Resource& resource) {
+ if (resource.kind == Resource::Kind::Tile
+ && util::mapbox::isMapboxURL(resource.url)
+ && offlineDatabase.offlineMapboxTileCountLimitExceeded()) {
+ observer->mapboxTileCountLimitExceeded(offlineDatabase.getOfflineMapboxTileCountLimit());
+ setState(OfflineRegionDownloadState::Inactive);
+ return true;
+ }
+
+ return false;
+}
+
} // namespace mbgl
diff --git a/platform/default/mbgl/storage/offline_download.hpp b/platform/default/mbgl/storage/offline_download.hpp
index 70c486d945..706cdf98d8 100644
--- a/platform/default/mbgl/storage/offline_download.hpp
+++ b/platform/default/mbgl/storage/offline_download.hpp
@@ -48,7 +48,8 @@ private:
*/
void ensureResource(const Resource&, std::function<void (Response)> = {});
void ensureTiles(SourceType, uint16_t, const SourceInfo&);
-
+ bool checkTileCountLimit(const Resource& resource);
+
int64_t id;
OfflineRegionDefinition definition;
OfflineDatabase& offlineDatabase;