blob: e6b6acf7b8eecd528e35338bc1104420661a2650 [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
Fyodor Kupolov5d53e442017-09-01 18:33:13 -070019import android.annotation.TestApi;
Jeff Brown5936ff02012-02-29 21:03:20 -080020import android.content.res.Resources;
Jeff Browne5360fb2011-10-31 17:48:13 -070021import android.os.StatFs;
Jeff Brown888da152012-05-11 16:36:24 -070022import android.os.SystemProperties;
Jeff Browne5360fb2011-10-31 17:48:13 -070023
24/**
25 * Provides access to SQLite functions that affect all database connection,
26 * such as memory management.
27 *
Jeff Brown4b575532012-01-20 12:34:34 -080028 * The native code associated with SQLiteGlobal is also sets global configuration options
29 * using sqlite3_config() then calls sqlite3_initialize() to ensure that the SQLite
30 * library is properly initialized exactly once before any other framework or application
31 * code has a chance to run.
32 *
33 * Verbose SQLite logging is enabled if the "log.tag.SQLiteLog" property is set to "V".
34 * (per {@link SQLiteDebug#DEBUG_SQL_LOG}).
35 *
Jeff Browne5360fb2011-10-31 17:48:13 -070036 * @hide
37 */
Fyodor Kupolov5d53e442017-09-01 18:33:13 -070038@TestApi
Jeff Browne5360fb2011-10-31 17:48:13 -070039public final class SQLiteGlobal {
40 private static final String TAG = "SQLiteGlobal";
41
Makoto Onuki962f7862018-08-07 15:57:13 -070042 /** @hide */
43 public static final String SYNC_MODE_FULL = "FULL";
44
Jeff Browne5360fb2011-10-31 17:48:13 -070045 private static final Object sLock = new Object();
Makoto Onuki962f7862018-08-07 15:57:13 -070046
Jeff Browne5360fb2011-10-31 17:48:13 -070047 private static int sDefaultPageSize;
48
Jeff Brown4b575532012-01-20 12:34:34 -080049 private static native int nativeReleaseMemory();
Jeff Browne5360fb2011-10-31 17:48:13 -070050
Makoto Onuki962f7862018-08-07 15:57:13 -070051 /** @hide */
52 public static volatile String sDefaultSyncMode;
53
Jeff Browne5360fb2011-10-31 17:48:13 -070054 private SQLiteGlobal() {
55 }
56
57 /**
Jeff Browne5360fb2011-10-31 17:48:13 -070058 * Attempts to release memory by pruning the SQLite page cache and other
59 * internal data structures.
60 *
61 * @return The number of bytes that were freed.
62 */
63 public static int releaseMemory() {
Jeff Brown4b575532012-01-20 12:34:34 -080064 return nativeReleaseMemory();
Jeff Browne5360fb2011-10-31 17:48:13 -070065 }
66
67 /**
68 * Gets the default page size to use when creating a database.
69 */
70 public static int getDefaultPageSize() {
71 synchronized (sLock) {
72 if (sDefaultPageSize == 0) {
Tobias Thierer0ad48dc2016-08-02 16:41:11 +010073 // If there is an issue accessing /data, something is so seriously
74 // wrong that we just let the IllegalArgumentException propagate.
Jeff Browne5360fb2011-10-31 17:48:13 -070075 sDefaultPageSize = new StatFs("/data").getBlockSize();
76 }
Jeff Brown888da152012-05-11 16:36:24 -070077 return SystemProperties.getInt("debug.sqlite.pagesize", sDefaultPageSize);
Jeff Browne5360fb2011-10-31 17:48:13 -070078 }
79 }
Jeff Brown5936ff02012-02-29 21:03:20 -080080
81 /**
82 * Gets the default journal mode when WAL is not in use.
83 */
84 public static String getDefaultJournalMode() {
Jeff Brown888da152012-05-11 16:36:24 -070085 return SystemProperties.get("debug.sqlite.journalmode",
86 Resources.getSystem().getString(
87 com.android.internal.R.string.db_default_journal_mode));
Jeff Brown5936ff02012-02-29 21:03:20 -080088 }
89
90 /**
Fyodor Kupolov5bd43ad2017-10-25 16:09:35 -070091 * Returns true if compatibility WAL mode is supported. In this mode, only
92 * database journal mode is changed. Connection pool will use at most one connection.
Fyodor Kupolov5bd43ad2017-10-25 16:09:35 -070093 */
94 public static boolean isCompatibilityWalSupported() {
95 return SystemProperties.getBoolean("debug.sqlite.compatibility_wal_supported",
96 Resources.getSystem().getBoolean(
97 com.android.internal.R.bool.db_compatibility_wal_supported));
98 }
99
100 /**
Jeff Brown5936ff02012-02-29 21:03:20 -0800101 * Gets the journal size limit in bytes.
102 */
103 public static int getJournalSizeLimit() {
Jeff Brown888da152012-05-11 16:36:24 -0700104 return SystemProperties.getInt("debug.sqlite.journalsizelimit",
105 Resources.getSystem().getInteger(
106 com.android.internal.R.integer.db_journal_size_limit));
Jeff Brown5936ff02012-02-29 21:03:20 -0800107 }
108
109 /**
Jeff Brown8dc3cc22012-03-02 10:33:52 -0800110 * Gets the default database synchronization mode when WAL is not in use.
Jeff Brown5936ff02012-02-29 21:03:20 -0800111 */
Jeff Brown8dc3cc22012-03-02 10:33:52 -0800112 public static String getDefaultSyncMode() {
Makoto Onuki962f7862018-08-07 15:57:13 -0700113 // Use the FULL synchronous mode for system processes by default.
114 String defaultMode = sDefaultSyncMode;
115 if (defaultMode != null) {
116 return defaultMode;
117 }
Jeff Brown888da152012-05-11 16:36:24 -0700118 return SystemProperties.get("debug.sqlite.syncmode",
119 Resources.getSystem().getString(
120 com.android.internal.R.string.db_default_sync_mode));
Jeff Brown8dc3cc22012-03-02 10:33:52 -0800121 }
122
123 /**
124 * Gets the database synchronization mode when in WAL mode.
125 */
126 public static String getWALSyncMode() {
Makoto Onuki962f7862018-08-07 15:57:13 -0700127 // Use the FULL synchronous mode for system processes by default.
128 String defaultMode = sDefaultSyncMode;
129 if (defaultMode != null) {
130 return defaultMode;
131 }
Jeff Brown888da152012-05-11 16:36:24 -0700132 return SystemProperties.get("debug.sqlite.wal.syncmode",
133 Resources.getSystem().getString(
134 com.android.internal.R.string.db_wal_sync_mode));
Jeff Brown5936ff02012-02-29 21:03:20 -0800135 }
136
137 /**
138 * Gets the WAL auto-checkpoint integer in database pages.
139 */
140 public static int getWALAutoCheckpoint() {
Jeff Brown888da152012-05-11 16:36:24 -0700141 int value = SystemProperties.getInt("debug.sqlite.wal.autocheckpoint",
142 Resources.getSystem().getInteger(
Jeff Brown5936ff02012-02-29 21:03:20 -0800143 com.android.internal.R.integer.db_wal_autocheckpoint));
Jeff Brown888da152012-05-11 16:36:24 -0700144 return Math.max(1, value);
Jeff Brown5936ff02012-02-29 21:03:20 -0800145 }
146
147 /**
Jeff Brown8dc3cc22012-03-02 10:33:52 -0800148 * Gets the connection pool size when in WAL mode.
Jeff Brown5936ff02012-02-29 21:03:20 -0800149 */
150 public static int getWALConnectionPoolSize() {
Jeff Brown888da152012-05-11 16:36:24 -0700151 int value = SystemProperties.getInt("debug.sqlite.wal.poolsize",
152 Resources.getSystem().getInteger(
Jeff Brown5936ff02012-02-29 21:03:20 -0800153 com.android.internal.R.integer.db_connection_pool_size));
Jeff Brown888da152012-05-11 16:36:24 -0700154 return Math.max(2, value);
Jeff Brown5936ff02012-02-29 21:03:20 -0800155 }
Fyodor Kupolovcf97b6b2017-07-25 14:17:33 -0700156
157 /**
158 * The default number of milliseconds that SQLite connection is allowed to be idle before it
159 * is closed and removed from the pool.
160 */
161 public static int getIdleConnectionTimeout() {
162 return SystemProperties.getInt("debug.sqlite.idle_connection_timeout",
163 Resources.getSystem().getInteger(
164 com.android.internal.R.integer.db_default_idle_connection_timeout));
165 }
166
Jeff Browne5360fb2011-10-31 17:48:13 -0700167}