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 | |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 19 | import android.content.res.Resources; |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 20 | import android.os.StatFs; |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 21 | import android.os.SystemProperties; |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Provides access to SQLite functions that affect all database connection, |
| 25 | * such as memory management. |
| 26 | * |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 27 | * 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 Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 35 | * @hide |
| 36 | */ |
| 37 | public final class SQLiteGlobal { |
| 38 | private static final String TAG = "SQLiteGlobal"; |
| 39 | |
| 40 | private static final Object sLock = new Object(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 41 | private static int sDefaultPageSize; |
| 42 | |
Jeff Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 43 | private static native int nativeReleaseMemory(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 44 | |
| 45 | private SQLiteGlobal() { |
| 46 | } |
| 47 | |
| 48 | /** |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 49 | * 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 Brown | 4b57553 | 2012-01-20 12:34:34 -0800 | [diff] [blame] | 55 | return nativeReleaseMemory(); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 56 | } |
| 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 Thierer | 0ad48dc | 2016-08-02 16:41:11 +0100 | [diff] [blame] | 64 | // If there is an issue accessing /data, something is so seriously |
| 65 | // wrong that we just let the IllegalArgumentException propagate. |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 66 | sDefaultPageSize = new StatFs("/data").getBlockSize(); |
| 67 | } |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 68 | return SystemProperties.getInt("debug.sqlite.pagesize", sDefaultPageSize); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Gets the default journal mode when WAL is not in use. |
| 74 | */ |
| 75 | public static String getDefaultJournalMode() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 76 | return SystemProperties.get("debug.sqlite.journalmode", |
| 77 | Resources.getSystem().getString( |
| 78 | com.android.internal.R.string.db_default_journal_mode)); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the journal size limit in bytes. |
| 83 | */ |
| 84 | public static int getJournalSizeLimit() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 85 | return SystemProperties.getInt("debug.sqlite.journalsizelimit", |
| 86 | Resources.getSystem().getInteger( |
| 87 | com.android.internal.R.integer.db_journal_size_limit)); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 91 | * Gets the default database synchronization mode when WAL is not in use. |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 92 | */ |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 93 | public static String getDefaultSyncMode() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 94 | return SystemProperties.get("debug.sqlite.syncmode", |
| 95 | Resources.getSystem().getString( |
| 96 | com.android.internal.R.string.db_default_sync_mode)); |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the database synchronization mode when in WAL mode. |
| 101 | */ |
| 102 | public static String getWALSyncMode() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 103 | return SystemProperties.get("debug.sqlite.wal.syncmode", |
| 104 | Resources.getSystem().getString( |
| 105 | com.android.internal.R.string.db_wal_sync_mode)); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets the WAL auto-checkpoint integer in database pages. |
| 110 | */ |
| 111 | public static int getWALAutoCheckpoint() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 112 | int value = SystemProperties.getInt("debug.sqlite.wal.autocheckpoint", |
| 113 | Resources.getSystem().getInteger( |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 114 | com.android.internal.R.integer.db_wal_autocheckpoint)); |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 115 | return Math.max(1, value); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Jeff Brown | 8dc3cc2 | 2012-03-02 10:33:52 -0800 | [diff] [blame] | 119 | * Gets the connection pool size when in WAL mode. |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 120 | */ |
| 121 | public static int getWALConnectionPoolSize() { |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 122 | int value = SystemProperties.getInt("debug.sqlite.wal.poolsize", |
| 123 | Resources.getSystem().getInteger( |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 124 | com.android.internal.R.integer.db_connection_pool_size)); |
Jeff Brown | 888da15 | 2012-05-11 16:36:24 -0700 | [diff] [blame] | 125 | return Math.max(2, value); |
Jeff Brown | 5936ff0 | 2012-02-29 21:03:20 -0800 | [diff] [blame] | 126 | } |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 127 | } |