diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-03-15 15:23:25 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-03-17 14:17:59 -0700 |
commit | 5130ca8843a342a1b708bb63263fd44c6c908120 (patch) | |
tree | 125ce7af31e47b603459977ddea0e8aaf9146bf1 /platform/default/mbgl | |
parent | aa60d295346871bad0884238d13dcd9995d97151 (diff) | |
download | qtlocation-mapboxgl-5130ca8843a342a1b708bb63263fd44c6c908120.tar.gz |
[core] Implement a vacuum strategy for the offline database
Enable `PRAGMA auto_vacuum = INCREMENTAL`, and perform a `PRAGMA incremental_vacuum` when deleting an offline region.
Diffstat (limited to 'platform/default/mbgl')
-rw-r--r-- | platform/default/mbgl/storage/offline_database.cpp | 39 | ||||
-rw-r--r-- | platform/default/mbgl/storage/offline_database.hpp | 2 |
2 files changed, 24 insertions, 17 deletions
diff --git a/platform/default/mbgl/storage/offline_database.cpp b/platform/default/mbgl/storage/offline_database.cpp index a42591d60e..e1cd693662 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,15 +47,12 @@ 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(); @@ -76,8 +70,17 @@ void OfflineDatabase::ensureSchema() { #include "offline_schema.cpp.include" connect(ReadWrite | Create); + + // 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 = " + util::toString(schemaVersion)); + db->exec("PRAGMA user_version = 3"); +} + +int OfflineDatabase::userVersion() { + auto stmt = db->prepare("PRAGMA user_version"); + stmt.run(); + return stmt.get<int>(0); } void OfflineDatabase::removeExisting() { @@ -92,6 +95,11 @@ void OfflineDatabase::removeExisting() { } } +void OfflineDatabase::migrateToVersion3() { + db->exec("PRAGMA auto_vacuum = INCREMENTAL"); + db->exec("VACUUM"); +} + OfflineDatabase::Statement OfflineDatabase::getStatement(const char * sql) { auto it = statements.find(sql); @@ -458,6 +466,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 +623,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: |