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

Commit 9a10df59 authored by Sameer Thatte's avatar Sameer Thatte Committed by Ricardo Cerqueira
Browse files

Switch Databases to WAL mode. Set autocommit to 100.

Change-Id: I852f439e2565d1732a55548f1519cbd2c77ebd64

frameworks/base:
Add check for READ ONLY databases (WAL can be used with R/W DBs only).

CRs-Fixed: 278062

Change-Id: Iadcbd868c1ef47e75bbdf5343868e542353ac9f2

frameworks/base: Use WAL mode for selected databases. Others use default mode: DELETE.

Change-Id: I4cb3c87a751f83bf47a73c4652ce1d8bf64a60fd

frameworks/base: Disable fsync in selected databases.

As an add-on to the WAL optimization, disable fsync in databases where
corruption and data consistency aren't a problem.

Change-Id: I41a85fa55ea71dc8dd3c4011102c28d2c1db8d3d
parent 4c7f92ca
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include <ctype.h>

#include <stdio.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -98,6 +99,35 @@ static void registerLoggingFunc(const char *path) {
    loggingFuncSet = true;
}

static int use_wal_mode (char const *path8)
{
    char *temp = basename(path8);
    int i;
    const char *wal_dbs[] = {
         "database_test.db","contacts2.db","calendar.db",\
         "telephony.db","launcher.db","user_dict.db",\
         "downloads.db", "mmssms.db", "internal.db", \
         "EmailProvider.db","alarms.db","EmailProviderBody.db",\
         "btopp.db","picasa.db",\
         "webviewCache.db","browser.db", NULL};

    const char *wal_dbs_nosync[] = {
         "webview.db", "quadrant.db", \
         "MyDatabase.db", NULL};

    for (i = 0 ; wal_dbs[i]!= NULL ; i++) {
        if(strcmp(temp, wal_dbs[i]) == 0)
            return 1;
    }

    for (i = 0 ; wal_dbs_nosync[i]!= NULL ; i++) {
        if(strcmp(temp, wal_dbs_nosync[i]) == 0)
            return 2;
    }

    return 0;
}

/* public native void dbopen(String path, int flags, String locale); */
static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags)
{
@@ -106,6 +136,8 @@ static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags)
    sqlite3_stmt * statement = NULL;
    char const * path8 = env->GetStringUTFChars(pathString, NULL);
    int sqliteFlags;
    // Error code handling for SQLite exec
    char* zErrMsg = NULL;

    // register the logging func on sqlite. needs to be done BEFORE any sqlite3 func is called.
    registerLoggingFunc(path8);
@@ -126,6 +158,41 @@ static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags)
        goto done;
    }

    // WAL is a new rollback method available in SQLite v3.7+. WAL speeds up writes to
    // SQLite databases. WAL cannot be used with Read Only databases or databases opened
    // in read only mode.

    // Check if DB can use WAL mode; Open in WAL mode for non-ReadOnly DBs
    if(!(flags & OPEN_READONLY) && (use_wal_mode(path8))) {
        // Configure databases to run in WAL mode.
        err = sqlite3_exec(handle,"PRAGMA journal_mode = WAL;",
                           NULL, NULL,&zErrMsg);
        if (SQLITE_OK != err) {
           LOGE("sqlite3_exec - Failed to set WAL mode for [%s] \n", path8);
           err = sqlite3_exec(handle,"PRAGMA journal_mode = DELETE;",
                           NULL, NULL,&zErrMsg);
           if(SQLITE_OK != err) {
               LOGE("sqlite3_exec - Failed to set DELETE mode for [%s] \n", path8);
               throw_sqlite3_exception(env, handle);
               goto done;
           }
        }
        else {
            // Set autocheckpoint = 100 pages
            err = sqlite3_wal_autocheckpoint(handle,
                                             100);
            if (SQLITE_OK != err) {
               LOGE("sqlite3_exec to set WAL autocheckpoint failed\n");
               throw_sqlite3_exception(env, handle);
               goto done;
            } else if (use_wal_mode(path8) == 2) {
                /* Try to disable fsyncs. We don't care if it fails */
                sqlite3_exec(handle,"PRAGMA synchronous = OFF;",
                           NULL, NULL,&zErrMsg);
            }
        }
    }

    // The soft heap limit prevents the page cache allocations from growing
    // beyond the given limit, no matter what the max page cache sizes are
    // set to. The limit does not, as of 3.5.0, affect any other allocations.