Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5936ff09 authored by Jeff Brown's avatar Jeff Brown
Browse files

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
parent 0e689aba
Loading
Loading
Loading
Loading
+51 −1
Original line number Diff line number Diff line
@@ -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();
+3 −23
Original line number Diff line number Diff line
@@ -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");
    }

    /**
+9 −0
Original line number Diff line number Diff line
@@ -84,6 +84,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.
     */
@@ -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);
    }
+41 −0
Original line number Diff line number Diff line
@@ -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));
    }
}
+0 −9
Original line number Diff line number Diff line
@@ -128,15 +128,6 @@ static jint nativeOpen(JNIEnv* env, jclass clazz, jstring pathStr, jint openFlag
        return 0;
    }

    // Enable WAL auto-checkpointing after a commit whenever at least one frame is in the log.
    // This ensures that a checkpoint will occur after each transaction if needed.
    err = sqlite3_wal_autocheckpoint(db, 1);
    if (err) {
        throw_sqlite3_exception(env, db, "Could not enable auto-checkpointing.");
        sqlite3_close(db);
        return 0;
    }

    // Register custom Android functions.
    err = register_android_functions(db, UTF16_STORAGE);
    if (err) {
Loading