1e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown/*
2e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * Copyright (C) 2011 The Android Open Source Project
3e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *
4e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * you may not use this file except in compliance with the License.
6e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * You may obtain a copy of the License at
7e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *
8e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown *
10e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * Unless required by applicable law or agreed to in writing, software
11e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * See the License for the specific language governing permissions and
14e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown * limitations under the License.
15e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown */
16e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
17e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#define LOG_TAG "SQLiteGlobal"
18e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
19e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <jni.h>
20e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <JNIHelp.h>
21ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.h"
22e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
23e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <sqlite3.h>
24e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include <sqlite3_android.h>
25e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
26e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown#include "android_database_SQLiteCommon.h"
274b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown#include "android_util_Log.h"
28e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
29e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownnamespace android {
30e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
314b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown// Limit heap to 8MB for now.  This is 4 times the maximum cursor window
324b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown// size, as has been used by the original code in SQLiteDatabase for
334b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown// a long time.
344b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brownstatic const int SOFT_HEAP_LIMIT = 8 * 1024 * 1024;
354b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown
364b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown
37e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown// Called each time a message is logged.
385dd67d6993aebb30445d637583501546523a16d2Jeff Brownstatic void sqliteLogCallback(void* data, int err, const char* msg) {
39e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    bool verboseLog = !!data;
405dd67d6993aebb30445d637583501546523a16d2Jeff Brown    int errType = err & 255;
415dd67d6993aebb30445d637583501546523a16d2Jeff Brown    if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA
425dd67d6993aebb30445d637583501546523a16d2Jeff Brown            || errType == SQLITE_NOTICE || err == SQLITE_WARNING_AUTOINDEX) {
43e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        if (verboseLog) {
445dd67d6993aebb30445d637583501546523a16d2Jeff Brown            ALOG(LOG_VERBOSE, SQLITE_LOG_TAG, "(%d) %s\n", err, msg);
45e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown        }
465dd67d6993aebb30445d637583501546523a16d2Jeff Brown    } else if (errType == SQLITE_WARNING) {
475dd67d6993aebb30445d637583501546523a16d2Jeff Brown        ALOG(LOG_WARN, SQLITE_LOG_TAG, "(%d) %s\n", err, msg);
48e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    } else {
495dd67d6993aebb30445d637583501546523a16d2Jeff Brown        ALOG(LOG_ERROR, SQLITE_LOG_TAG, "(%d) %s\n", err, msg);
50e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    }
51e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown}
52e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
53e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown// Sets the global SQLite configuration.
544b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown// This must be called before any other SQLite functions are called.
554b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brownstatic void sqliteInitialize() {
56e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // Enable multi-threaded mode.  In this mode, SQLite is safe to use by multiple
57e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // threads as long as no two threads use the same database connection at the same
58e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // time (which we guarantee in the SQLite database wrappers).
59e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
60e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
61e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // Redirect SQLite log messages to the Android log.
624b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown    bool verboseLog = android_util_Log_isVerboseLogEnabled(SQLITE_LOG_TAG);
63e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, verboseLog ? (void*)1 : NULL);
64e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
65e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // The soft heap limit prevents the page cache allocations from growing
66e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // beyond the given limit, no matter what the max page cache sizes are
67e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    // set to. The limit does not, as of 3.5.0, affect any other allocations.
684b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown    sqlite3_soft_heap_limit(SOFT_HEAP_LIMIT);
694b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown
704b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown    // Initialize SQLite.
714b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown    sqlite3_initialize();
72e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown}
73e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
744b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brownstatic jint nativeReleaseMemory(JNIEnv* env, jclass clazz) {
754b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown    return sqlite3_release_memory(SOFT_HEAP_LIMIT);
76e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown}
77e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
7876f6a86de25e1bf74717e047e55fd44b089673f3Daniel Micaystatic const JNINativeMethod sMethods[] =
79e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown{
80e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown    /* name, signature, funcPtr */
81ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    { "nativeReleaseMemory", "()I", (void*)nativeReleaseMemory },
82e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown};
83e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
84e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brownint register_android_database_SQLiteGlobal(JNIEnv *env)
85e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown{
864b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown    sqliteInitialize();
874b57553e693c9705e8363d3e0e9d881261b3e6faJeff Brown
88ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    return RegisterMethodsOrDie(env, "android/database/sqlite/SQLiteGlobal",
89e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown            sMethods, NELEM(sMethods));
90e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown}
91e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown
92e5360fbf3efe85427f7e7f59afe7bbeddb4949acJeff Brown} // namespace android
93