diff options
author | Jeff Brown <jeffbrown@google.com> | 2012-02-29 21:03:20 -0800 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2012-02-29 21:03:20 -0800 |
commit | 5936ff097eff2c736af2e43fd4a8f7db0ddcfb5a (patch) | |
tree | 6a9646ecfce3c34de538f912af969be648df3ee0 /core/java/android/database | |
parent | 0e689abaec67ad5f0c485ca8387d843bf55ab10f (diff) | |
download | frameworks_base-5936ff097eff2c736af2e43fd4a8f7db0ddcfb5a.zip frameworks_base-5936ff097eff2c736af2e43fd4a8f7db0ddcfb5a.tar.gz frameworks_base-5936ff097eff2c736af2e43fd4a8f7db0ddcfb5a.tar.bz2 |
Externalize more SQLite configuration options.
Moved more configuration into config.xml so we can tweak settings
like the default journal mode, WAL auto-checkpoint interval and
so on.
This change itself should not introduce any functional differences.
Change-Id: Id6c95fa25b116ce47e8ae49cd8a80d52b1c0dd80
Diffstat (limited to 'core/java/android/database')
4 files changed, 104 insertions, 24 deletions
diff --git a/core/java/android/database/sqlite/SQLiteConnection.java b/core/java/android/database/sqlite/SQLiteConnection.java index b5cef81..1900301 100644 --- a/core/java/android/database/sqlite/SQLiteConnection.java +++ b/core/java/android/database/sqlite/SQLiteConnection.java @@ -208,6 +208,11 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen mConfiguration.label, SQLiteDebug.DEBUG_SQL_STATEMENTS, SQLiteDebug.DEBUG_SQL_TIME); + setSyncMode(); + setPageSize(); + setAutoCheckpointInterval(); + setJournalSizeLimit(); + setJournalModeFromConfiguration(); setLocaleFromConfiguration(); } @@ -231,6 +236,44 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen } } + private void setSyncMode() { + if (!mConfiguration.isInMemoryDb()) { + execute("PRAGMA synchronous=" + SQLiteGlobal.getSyncMode(), null, null); + } + } + + private void setPageSize() { + if (!mConfiguration.isInMemoryDb()) { + execute("PRAGMA page_size=" + SQLiteGlobal.getDefaultPageSize(), null, null); + } + } + + private void setAutoCheckpointInterval() { + if (!mConfiguration.isInMemoryDb()) { + executeForLong("PRAGMA wal_autocheckpoint=" + SQLiteGlobal.getWALAutoCheckpoint(), + null, null); + } + } + + private void setJournalSizeLimit() { + if (!mConfiguration.isInMemoryDb()) { + executeForLong("PRAGMA journal_size_limit=" + SQLiteGlobal.getJournalSizeLimit(), + null, null); + } + } + + private void setJournalModeFromConfiguration() { + if (!mConfiguration.isInMemoryDb()) { + String result = executeForString("PRAGMA journal_mode=" + mConfiguration.journalMode, + null, null); + if (!result.equalsIgnoreCase(mConfiguration.journalMode)) { + Log.e(TAG, "setting journal_mode to " + mConfiguration.journalMode + + " failed for db: " + mConfiguration.label + + " (on pragma set journal_mode, sqlite returned:" + result); + } + } + } + private void setLocaleFromConfiguration() { nativeSetLocale(mConnectionPtr, mConfiguration.locale.toString()); } @@ -246,7 +289,9 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen } } - // Remember whether locale has changed. + // Remember what changed. + boolean journalModeChanged = !configuration.journalMode.equalsIgnoreCase( + mConfiguration.journalMode); boolean localeChanged = !configuration.locale.equals(mConfiguration.locale); // Update configuration parameters. @@ -255,6 +300,11 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen // Update prepared statement cache size. mPreparedStatementCache.resize(configuration.maxSqlCacheSize); + // Update journal mode. + if (journalModeChanged) { + setJournalModeFromConfiguration(); + } + // Update locale. if (localeChanged) { setLocaleFromConfiguration(); diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 36f678d..515658f 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -717,9 +717,6 @@ public class SQLiteDatabase extends SQLiteClosable { onCorruption(); openInner(); } - - // Disable WAL if it was previously enabled. - setJournalMode("TRUNCATE"); } catch (SQLiteException ex) { Log.e(TAG, "Failed to open database '" + getLabel() + "'.", ex); close(); @@ -739,19 +736,6 @@ public class SQLiteDatabase extends SQLiteClosable { } } - private void setJournalMode(String mode) { - // Journal mode can be set only for non-memory databases - // AND can't be set for readonly databases - if (isInMemoryDatabase() || isReadOnly()) { - return; - } - String s = DatabaseUtils.stringForQuery(this, "PRAGMA journal_mode=" + mode, null); - if (!s.equalsIgnoreCase(mode)) { - Log.e(TAG, "setting journal_mode to " + mode + " failed for db: " + getLabel() - + " (on pragma set journal_mode, sqlite returned:" + s); - } - } - /** * Create a memory backed SQLite database. Its contents will be destroyed * when the database is closed. @@ -1761,13 +1745,10 @@ public class SQLiteDatabase extends SQLiteClosable { } mIsWALEnabledLocked = true; - mConfigurationLocked.maxConnectionPoolSize = Math.max(2, - Resources.getSystem().getInteger( - com.android.internal.R.integer.db_connection_pool_size)); + mConfigurationLocked.maxConnectionPoolSize = SQLiteGlobal.getWALConnectionPoolSize(); + mConfigurationLocked.journalMode = "WAL"; mConnectionPoolLocked.reconfigure(mConfigurationLocked); } - - setJournalMode("WAL"); return true; } @@ -1785,10 +1766,9 @@ public class SQLiteDatabase extends SQLiteClosable { mIsWALEnabledLocked = false; mConfigurationLocked.maxConnectionPoolSize = 1; + mConfigurationLocked.journalMode = SQLiteGlobal.getDefaultJournalMode(); mConnectionPoolLocked.reconfigure(mConfigurationLocked); } - - setJournalMode("TRUNCATE"); } /** diff --git a/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java b/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java index 02ef671..32a1bcb 100644 --- a/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java +++ b/core/java/android/database/sqlite/SQLiteDatabaseConfiguration.java @@ -85,6 +85,13 @@ public final class SQLiteDatabaseConfiguration { public Locale locale; /** + * The database journal mode. + * + * Default is {@link SQLiteGlobal#getDefaultJournalMode()}. + */ + public String journalMode; + + /** * The custom functions to register. */ public final ArrayList<SQLiteCustomFunction> customFunctions = @@ -110,6 +117,7 @@ public final class SQLiteDatabaseConfiguration { maxConnectionPoolSize = 1; maxSqlCacheSize = 25; locale = Locale.getDefault(); + journalMode = SQLiteGlobal.getDefaultJournalMode(); } /** @@ -146,6 +154,7 @@ public final class SQLiteDatabaseConfiguration { maxConnectionPoolSize = other.maxConnectionPoolSize; maxSqlCacheSize = other.maxSqlCacheSize; locale = other.locale; + journalMode = other.journalMode; customFunctions.clear(); customFunctions.addAll(other.customFunctions); } diff --git a/core/java/android/database/sqlite/SQLiteGlobal.java b/core/java/android/database/sqlite/SQLiteGlobal.java index dbefd63..af0cf45 100644 --- a/core/java/android/database/sqlite/SQLiteGlobal.java +++ b/core/java/android/database/sqlite/SQLiteGlobal.java @@ -16,6 +16,7 @@ package android.database.sqlite; +import android.content.res.Resources; import android.os.StatFs; /** @@ -64,4 +65,44 @@ public final class SQLiteGlobal { return sDefaultPageSize; } } + + /** + * Gets the default journal mode when WAL is not in use. + */ + public static String getDefaultJournalMode() { + return Resources.getSystem().getString( + com.android.internal.R.string.db_default_journal_mode); + } + + /** + * Gets the journal size limit in bytes. + */ + public static int getJournalSizeLimit() { + return Resources.getSystem().getInteger( + com.android.internal.R.integer.db_journal_size_limit); + } + + /** + * Gets the database synchronization mode. + */ + public static String getSyncMode() { + return Resources.getSystem().getString( + com.android.internal.R.string.db_sync_mode); + } + + /** + * Gets the WAL auto-checkpoint integer in database pages. + */ + public static int getWALAutoCheckpoint() { + return Math.max(1, Resources.getSystem().getInteger( + com.android.internal.R.integer.db_wal_autocheckpoint)); + } + + /** + * Gets the default connection pool size when in WAL mode. + */ + public static int getWALConnectionPoolSize() { + return Math.max(2, Resources.getSystem().getInteger( + com.android.internal.R.integer.db_connection_pool_size)); + } } |