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

[core] Enable incremental vacuum for Offline DB #15837

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions platform/android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Mapbox welcomes participation and contributions from everyone. If you'd like to do so please see the [`Contributing Guide`](https://github.com/mapbox/mapbox-gl-native/blob/master/CONTRIBUTING.md) first to get started.

## master

### Performance improvements
- Enable incremental vacuum for the offline database in order to make data removal requests faster and to avoid the excessive disk space usage (creating a backup file on VACUUM call) [#15837](https://github.com/mapbox/mapbox-gl-native/pull/15837)

## 8.5.0-alpha.2 - October 10, 2019
[Changes](https://github.com/mapbox/mapbox-gl-native/compare/android-v8.5.0-alpha.1...android-v8.5.0-alpha.2) since [Mapbox Maps SDK for Android v8.5.0-alpha.1](https://github.com/mapbox/mapbox-gl-native/releases/tag/android-v8.5.0-alpha.1):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class OfflineDatabase : private util::noncopyable {
void migrateToVersion6();
void cleanup();
bool disabled();
void vacuum();

mapbox::sqlite::Statement& getStatement(const char *);

Expand Down
20 changes: 15 additions & 5 deletions platform/default/src/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ void OfflineDatabase::removeExisting() {
void OfflineDatabase::removeOldCacheTable() {
assert(db);
db->exec("DROP TABLE IF EXISTS http_cache");
db->exec("VACUUM");
vacuum();
}

void OfflineDatabase::createSchema() {
assert(db);
vacuum();
db->exec("PRAGMA journal_mode = DELETE");
db->exec("PRAGMA synchronous = FULL");
mapbox::sqlite::Transaction transaction(*db);
Expand All @@ -155,7 +156,7 @@ void OfflineDatabase::createSchema() {

void OfflineDatabase::migrateToVersion3() {
assert(db);
db->exec("VACUUM");
vacuum();
db->exec("PRAGMA user_version = 3");
}

Expand All @@ -181,6 +182,15 @@ void OfflineDatabase::migrateToVersion6() {
transaction.commit();
}

void OfflineDatabase::vacuum() {
if (getPragma<int64_t>("PRAGMA auto_vacuum") != 2 /*INCREMENTAL*/) {
db->exec("PRAGMA auto_vacuum = INCREMENTAL");
db->exec("VACUUM");
pozdnyakov marked this conversation as resolved.
Show resolved Hide resolved
} else {
db->exec("PRAGMA incremental_vacuum");
}
}

mapbox::sqlite::Statement& OfflineDatabase::getStatement(const char* sql) {
if (!db) {
initialize();
Expand Down Expand Up @@ -683,7 +693,7 @@ std::exception_ptr OfflineDatabase::clearAmbientCache() try {

resourceQuery.run();

db->exec("VACUUM");
vacuum();

return nullptr;
} catch (const mapbox::sqlite::Exception& ex) {
Expand Down Expand Up @@ -871,7 +881,7 @@ std::exception_ptr OfflineDatabase::deleteRegion(OfflineRegion&& region) try {

evict(0);
assert(db);
db->exec("VACUUM");
vacuum();

// Ensure that the cached offlineTileCount value is recalculated.
offlineMapboxTileCount = {};
Expand Down Expand Up @@ -1218,7 +1228,7 @@ std::exception_ptr OfflineDatabase::setMaximumAmbientCacheSize(uint64_t size) {

if (databaseSize > maximumAmbientCacheSize) {
evict(0);
db->exec("VACUUM");
vacuum();
}

return nullptr;
Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
### Other changes

* Coalesce requests to the client for the same missing image ([#15778](https://github.com/mapbox/mapbox-gl-native/pull/15778))
* Enable incremental vacuum for the offline database in order to make data removal requests faster and to avoid the excessive disk space usage (creating a backup file on VACUUM call). ([#15837](https://github.com/mapbox/mapbox-gl-native/pull/15837))

## 5.5.0

Expand Down
Binary file not shown.
52 changes: 46 additions & 6 deletions test/storage/offline_database.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ static std::vector<std::string> databaseTableColumns(const std::string& path, co
return columns;
}

static int databaseAutoVacuum(const std::string& path) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
mapbox::sqlite::Statement stmt{db, "pragma auto_vacuum"};
mapbox::sqlite::Query query{stmt};
query.run();
return query.get<int>(0);
}

namespace fixture {

const Resource resource{ Resource::Style, "mapbox://test" };
Expand Down Expand Up @@ -1360,13 +1368,45 @@ TEST(OfflineDatabase, MigrateFromV5Schema) {

EXPECT_EQ(6, databaseUserVersion(filename));

EXPECT_EQ((std::vector<std::string>{ "id", "url_template", "pixel_ratio", "z", "x", "y",
"expires", "modified", "etag", "data", "compressed",
"accessed", "must_revalidate" }),
EXPECT_EQ((std::vector<std::string>{"id",
"url_template",
"pixel_ratio",
"z",
"x",
"y",
"expires",
"modified",
"etag",
"data",
"compressed",
"accessed",
"must_revalidate"}),
databaseTableColumns(filename, "tiles"));
EXPECT_EQ((std::vector<std::string>{ "id", "url", "kind", "expires", "modified", "etag", "data",
"compressed", "accessed", "must_revalidate" }),
databaseTableColumns(filename, "resources"));
EXPECT_EQ(
(std::vector<std::string>{
"id", "url", "kind", "expires", "modified", "etag", "data", "compressed", "accessed", "must_revalidate"}),
databaseTableColumns(filename, "resources"));

EXPECT_EQ(0u, log.uncheckedCount());
}

TEST(OfflineDatabase, IncrementalVacuum) {
FixtureLog log;
deleteDatabaseFiles();
util::copyFile(filename, "test/fixtures/offline_database/no_auto_vacuum.db");
EXPECT_EQ(0, databaseAutoVacuum(filename));

{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(0);

auto regions = db.listRegions().value();
for (auto& region : regions) {
db.deleteRegion(std::move(region));
}
}

EXPECT_EQ(2, databaseAutoVacuum(filename));

EXPECT_EQ(0u, log.uncheckedCount());
}
Expand Down