blob: 922d11b6ae65f0687c26918365c52ccf488ff8f5 [file] [log] [blame]
Jeff Browne5360fb2011-10-31 17:48:13 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.database.sqlite;
18
Jeff Brown5936ff02012-02-29 21:03:20 -080019import android.content.res.Resources;
Jeff Browne5360fb2011-10-31 17:48:13 -070020import android.os.StatFs;
Jeff Brown888da152012-05-11 16:36:24 -070021import android.os.SystemProperties;
Jeff Browne5360fb2011-10-31 17:48:13 -070022
23/**
24 * Provides access to SQLite functions that affect all database connection,
25 * such as memory management.
26 *
Jeff Brown4b575532012-01-20 12:34:34 -080027 * The native code associated with SQLiteGlobal is also sets global configuration options
28 * using sqlite3_config() then calls sqlite3_initialize() to ensure that the SQLite
29 * library is properly initialized exactly once before any other framework or application
30 * code has a chance to run.
31 *
32 * Verbose SQLite logging is enabled if the "log.tag.SQLiteLog" property is set to "V".
33 * (per {@link SQLiteDebug#DEBUG_SQL_LOG}).
34 *
Jeff Browne5360fb2011-10-31 17:48:13 -070035 * @hide
36 */
37public final class SQLiteGlobal {
38 private static final String TAG = "SQLiteGlobal";
39
40 private static final Object sLock = new Object();
Jeff Browne5360fb2011-10-31 17:48:13 -070041 private static int sDefaultPageSize;
42
Jeff Brown4b575532012-01-20 12:34:34 -080043 private static native int nativeReleaseMemory();
Jeff Browne5360fb2011-10-31 17:48:13 -070044
45 private SQLiteGlobal() {
46 }
47
48 /**
Jeff Browne5360fb2011-10-31 17:48:13 -070049 * Attempts to release memory by pruning the SQLite page cache and other
50 * internal data structures.
51 *
52 * @return The number of bytes that were freed.
53 */
54 public static int releaseMemory() {
Jeff Brown4b575532012-01-20 12:34:34 -080055 return nativeReleaseMemory();
Jeff Browne5360fb2011-10-31 17:48:13 -070056 }
57
58 /**
59 * Gets the default page size to use when creating a database.
60 */
61 public static int getDefaultPageSize() {
62 synchronized (sLock) {
63 if (sDefaultPageSize == 0) {
Tobias Thierer0ad48dc2016-08-02 16:41:11 +010064 // If there is an issue accessing /data, something is so seriously
65 // wrong that we just let the IllegalArgumentException propagate.
Jeff Browne5360fb2011-10-31 17:48:13 -070066 sDefaultPageSize = new StatFs("/data").getBlockSize();
67 }
Jeff Brown888da152012-05-11 16:36:24 -070068 return SystemProperties.getInt("debug.sqlite.pagesize", sDefaultPageSize);
Jeff Browne5360fb2011-10-31 17:48:13 -070069 }
70 }
Jeff Brown5936ff02012-02-29 21:03:20 -080071
72 /**
73 * Gets the default journal mode when WAL is not in use.
74 */
75 public static String getDefaultJournalMode() {
Jeff Brown888da152012-05-11 16:36:24 -070076 return SystemProperties.get("debug.sqlite.journalmode",
77 Resources.getSystem().getString(
78 com.android.internal.R.string.db_default_journal_mode));
Jeff Brown5936ff02012-02-29 21:03:20 -080079 }
80
81 /**
82 * Gets the journal size limit in bytes.
83 */
84 public static int getJournalSizeLimit() {
Jeff Brown888da152012-05-11 16:36:24 -070085 return SystemProperties.getInt("debug.sqlite.journalsizelimit",
86 Resources.getSystem().getInteger(
87 com.android.internal.R.integer.db_journal_size_limit));
Jeff Brown5936ff02012-02-29 21:03:20 -080088 }
89
90 /**
Jeff Brown8dc3cc22012-03-02 10:33:52 -080091 * Gets the default database synchronization mode when WAL is not in use.
Jeff Brown5936ff02012-02-29 21:03:20 -080092 */
Jeff Brown8dc3cc22012-03-02 10:33:52 -080093 public static String getDefaultSyncMode() {
Jeff Brown888da152012-05-11 16:36:24 -070094 return SystemProperties.get("debug.sqlite.syncmode",
95 Resources.getSystem().getString(
96 com.android.internal.R.string.db_default_sync_mode));
Jeff Brown8dc3cc22012-03-02 10:33:52 -080097 }
98
99 /**
100 * Gets the database synchronization mode when in WAL mode.
101 */
102 public static String getWALSyncMode() {
Jeff Brown888da152012-05-11 16:36:24 -0700103 return SystemProperties.get("debug.sqlite.wal.syncmode",
104 Resources.getSystem().getString(
105 com.android.internal.R.string.db_wal_sync_mode));
Jeff Brown5936ff02012-02-29 21:03:20 -0800106 }
107
108 /**
109 * Gets the WAL auto-checkpoint integer in database pages.
110 */
111 public static int getWALAutoCheckpoint() {
Jeff Brown888da152012-05-11 16:36:24 -0700112 int value = SystemProperties.getInt("debug.sqlite.wal.autocheckpoint",
113 Resources.getSystem().getInteger(
Jeff Brown5936ff02012-02-29 21:03:20 -0800114 com.android.internal.R.integer.db_wal_autocheckpoint));
Jeff Brown888da152012-05-11 16:36:24 -0700115 return Math.max(1, value);
Jeff Brown5936ff02012-02-29 21:03:20 -0800116 }
117
118 /**
Jeff Brown8dc3cc22012-03-02 10:33:52 -0800119 * Gets the connection pool size when in WAL mode.
Jeff Brown5936ff02012-02-29 21:03:20 -0800120 */
121 public static int getWALConnectionPoolSize() {
Jeff Brown888da152012-05-11 16:36:24 -0700122 int value = SystemProperties.getInt("debug.sqlite.wal.poolsize",
123 Resources.getSystem().getInteger(
Jeff Brown5936ff02012-02-29 21:03:20 -0800124 com.android.internal.R.integer.db_connection_pool_size));
Jeff Brown888da152012-05-11 16:36:24 -0700125 return Math.max(2, value);
Jeff Brown5936ff02012-02-29 21:03:20 -0800126 }
Jeff Browne5360fb2011-10-31 17:48:13 -0700127}