[core] Update SQLite schema with WAL journal mode and normal sync

Offers 2×-4× performance, depending on device vintage.
This commit is contained in:
Jason Wray
2016-07-25 17:31:37 -07:00
parent eb9fe89612
commit 74f094faab
3 changed files with 17 additions and 7 deletions

View File

@@ -48,7 +48,8 @@ void OfflineDatabase::ensureSchema() {
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;
case 3: migrateToVersion4(); // fall through
case 4: return;
default: throw std::runtime_error("unknown schema version");
}
@@ -79,8 +80,10 @@ void OfflineDatabase::ensureSchema() {
// If you change the schema you must write a migration from the previous version.
db->exec("PRAGMA auto_vacuum = INCREMENTAL");
db->exec("PRAGMA synchronous = NORMAL");
db->exec("PRAGMA journal_mode = WAL");
db->exec(schema);
db->exec("PRAGMA user_version = 3");
db->exec("PRAGMA user_version = 4");
} catch (...) {
Log::Error(Event::Database, "Unexpected error creating database schema: %s", util::toString(std::current_exception()).c_str());
throw;
@@ -111,6 +114,12 @@ void OfflineDatabase::migrateToVersion3() {
db->exec("PRAGMA user_version = 3");
}
void OfflineDatabase::migrateToVersion4() {
db->exec("PRAGMA synchronous = NORMAL");
db->exec("PRAGMA journal_mode = WAL");
db->exec("PRAGMA user_version = 4");
}
OfflineDatabase::Statement OfflineDatabase::getStatement(const char * sql) {
auto it = statements.find(sql);

View File

@@ -60,6 +60,7 @@ private:
void ensureSchema();
void removeExisting();
void migrateToVersion3();
void migrateToVersion4();
class Statement {
public:

View File

@@ -559,18 +559,18 @@ TEST(OfflineDatabase, MigrateFromV2Schema) {
// v2.db is a v2 database containing a single offline region with a small number of resources.
deleteFile("test/fixtures/offline_database/v3.db");
writeFile("test/fixtures/offline_database/v3.db", util::read_file("test/fixtures/offline_database/v2.db"));
deleteFile("test/fixtures/offline_database/v4.db");
writeFile("test/fixtures/offline_database/v4.db", util::read_file("test/fixtures/offline_database/v2.db"));
{
OfflineDatabase db("test/fixtures/offline_database/v3.db", 0);
OfflineDatabase db("test/fixtures/offline_database/v4.db", 0);
auto regions = db.listRegions();
for (auto& region : regions) {
db.deleteRegion(std::move(region));
}
}
EXPECT_EQ(3, databaseUserVersion("test/fixtures/offline_database/v3.db"));
EXPECT_LT(databasePageCount("test/fixtures/offline_database/v3.db"),
EXPECT_EQ(4, databaseUserVersion("test/fixtures/offline_database/v4.db"));
EXPECT_LT(databasePageCount("test/fixtures/offline_database/v4.db"),
databasePageCount("test/fixtures/offline_database/v2.db"));
}