Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.database.sqlite; |
| 18 | |
Fyodor Kupolov | 5d53e44 | 2017-09-01 18:33:13 -0700 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 21 | import android.os.StatFs; |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 22 | import android.os.SystemProperties; |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Provides access to SQLite functions that affect all database connection, |
| 26 | * such as memory management. |
| 27 | * |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 28 | * 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 Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 36 | * @hide |
| 37 | */ |
Fyodor Kupolov | 5d53e44 | 2017-09-01 18:33:13 -0700 | [diff] [blame] | 38 | @TestApi |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 39 | public final class SQLiteGlobal { |
| 40 | private static final String TAG = "SQLiteGlobal"; |
| 41 | |
Makoto Onuki | 962f786 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 42 | /** @hide */ |
| 43 | public static final String SYNC_MODE_FULL = "FULL"; |
| 44 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 45 | private static final Object sLock = new Object(); |
Makoto Onuki | 962f786 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 46 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 47 | private static int sDefaultPageSize; |
| 48 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 49 | private static native int nativeReleaseMemory(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 50 | |
Makoto Onuki | 962f786 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 51 | /** @hide */ |
| 52 | public static volatile String sDefaultSyncMode; |
| 53 | |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 54 | private SQLiteGlobal() { |
| 55 | } |
| 56 | |
| 57 | /** |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 58 | * 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 Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 64 | return nativeReleaseMemory(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 65 | } |
| 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 Thierer | 0ad48dc | 2016-08-02 16:41:11 +0100 | [diff] [blame] | 73 | // If there is an issue accessing /data, something is so seriously |
| 74 | // wrong that we just let the IllegalArgumentException propagate. |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 75 | sDefaultPageSize = new StatFs("/data").getBlockSize(); |
| 76 | } |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 77 | return SystemProperties.getInt("debug.sqlite.pagesize", sDefaultPageSize); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Gets the default journal mode when WAL is not in use. |
| 83 | */ |
| 84 | public static String getDefaultJournalMode() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 85 | return SystemProperties.get("debug.sqlite.journalmode", |
| 86 | Resources.getSystem().getString( |
| 87 | com.android.internal.R.string.db_default_journal_mode)); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Fyodor Kupolov | 5bd43ad | 2017-10-25 16:09:35 -0700 | [diff] [blame] | 91 | * 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 Kupolov | 5bd43ad | 2017-10-25 16:09:35 -0700 | [diff] [blame] | 93 | */ |
| 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 Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 101 | * Gets the journal size limit in bytes. |
| 102 | */ |
| 103 | public static int getJournalSizeLimit() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 104 | return SystemProperties.getInt("debug.sqlite.journalsizelimit", |
| 105 | Resources.getSystem().getInteger( |
| 106 | com.android.internal.R.integer.db_journal_size_limit)); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 110 | * Gets the default database synchronization mode when WAL is not in use. |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 111 | */ |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 112 | public static String getDefaultSyncMode() { |
Makoto Onuki | 962f786 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 113 | // Use the FULL synchronous mode for system processes by default. |
| 114 | String defaultMode = sDefaultSyncMode; |
| 115 | if (defaultMode != null) { |
| 116 | return defaultMode; |
| 117 | } |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 118 | return SystemProperties.get("debug.sqlite.syncmode", |
| 119 | Resources.getSystem().getString( |
| 120 | com.android.internal.R.string.db_default_sync_mode)); |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Gets the database synchronization mode when in WAL mode. |
| 125 | */ |
| 126 | public static String getWALSyncMode() { |
Makoto Onuki | 962f786 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 127 | // Use the FULL synchronous mode for system processes by default. |
| 128 | String defaultMode = sDefaultSyncMode; |
| 129 | if (defaultMode != null) { |
| 130 | return defaultMode; |
| 131 | } |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 132 | return SystemProperties.get("debug.sqlite.wal.syncmode", |
| 133 | Resources.getSystem().getString( |
| 134 | com.android.internal.R.string.db_wal_sync_mode)); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Gets the WAL auto-checkpoint integer in database pages. |
| 139 | */ |
| 140 | public static int getWALAutoCheckpoint() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 141 | int value = SystemProperties.getInt("debug.sqlite.wal.autocheckpoint", |
| 142 | Resources.getSystem().getInteger( |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 143 | com.android.internal.R.integer.db_wal_autocheckpoint)); |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 144 | return Math.max(1, value); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 148 | * Gets the connection pool size when in WAL mode. |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 149 | */ |
| 150 | public static int getWALConnectionPoolSize() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 151 | int value = SystemProperties.getInt("debug.sqlite.wal.poolsize", |
| 152 | Resources.getSystem().getInteger( |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 153 | com.android.internal.R.integer.db_connection_pool_size)); |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 154 | return Math.max(2, value); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 155 | } |
Fyodor Kupolov | cf97b6b | 2017-07-25 14:17:33 -0700 | [diff] [blame] | 156 | |
| 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 Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 167 | } |