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 19 import android.annotation.TestApi; 20 import android.content.res.Resources; 21 import android.os.StatFs; 22 import android.os.SystemProperties; 23 24 /** 25 * Provides access to SQLite functions that affect all database connection, 26 * such as memory management. 27 * 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 * 36 * @hide 37 */ 38 @TestApi 39 public final class SQLiteGlobal { 40 private static final String TAG = "SQLiteGlobal"; 41 42 private static final Object sLock = new Object(); 43 private static int sDefaultPageSize; 44 nativeReleaseMemory()45 private static native int nativeReleaseMemory(); 46 SQLiteGlobal()47 private SQLiteGlobal() { 48 } 49 50 /** 51 * Attempts to release memory by pruning the SQLite page cache and other 52 * internal data structures. 53 * 54 * @return The number of bytes that were freed. 55 */ releaseMemory()56 public static int releaseMemory() { 57 return nativeReleaseMemory(); 58 } 59 60 /** 61 * Gets the default page size to use when creating a database. 62 */ getDefaultPageSize()63 public static int getDefaultPageSize() { 64 synchronized (sLock) { 65 if (sDefaultPageSize == 0) { 66 // If there is an issue accessing /data, something is so seriously 67 // wrong that we just let the IllegalArgumentException propagate. 68 sDefaultPageSize = new StatFs("/data").getBlockSize(); 69 } 70 return SystemProperties.getInt("debug.sqlite.pagesize", sDefaultPageSize); 71 } 72 } 73 74 /** 75 * Gets the default journal mode when WAL is not in use. 76 */ getDefaultJournalMode()77 public static String getDefaultJournalMode() { 78 return SystemProperties.get("debug.sqlite.journalmode", 79 Resources.getSystem().getString( 80 com.android.internal.R.string.db_default_journal_mode)); 81 } 82 83 /** 84 * Returns true if compatibility WAL mode is supported. In this mode, only 85 * database journal mode is changed. Connection pool will use at most one connection. 86 */ isCompatibilityWalSupported()87 public static boolean isCompatibilityWalSupported() { 88 return SystemProperties.getBoolean("debug.sqlite.compatibility_wal_supported", 89 Resources.getSystem().getBoolean( 90 com.android.internal.R.bool.db_compatibility_wal_supported)); 91 } 92 93 /** 94 * Gets the journal size limit in bytes. 95 */ getJournalSizeLimit()96 public static int getJournalSizeLimit() { 97 return SystemProperties.getInt("debug.sqlite.journalsizelimit", 98 Resources.getSystem().getInteger( 99 com.android.internal.R.integer.db_journal_size_limit)); 100 } 101 102 /** 103 * Gets the default database synchronization mode when WAL is not in use. 104 */ getDefaultSyncMode()105 public static String getDefaultSyncMode() { 106 return SystemProperties.get("debug.sqlite.syncmode", 107 Resources.getSystem().getString( 108 com.android.internal.R.string.db_default_sync_mode)); 109 } 110 111 /** 112 * Gets the database synchronization mode when in WAL mode. 113 */ getWALSyncMode()114 public static String getWALSyncMode() { 115 return SystemProperties.get("debug.sqlite.wal.syncmode", 116 Resources.getSystem().getString( 117 com.android.internal.R.string.db_wal_sync_mode)); 118 } 119 120 /** 121 * Gets the WAL auto-checkpoint integer in database pages. 122 */ getWALAutoCheckpoint()123 public static int getWALAutoCheckpoint() { 124 int value = SystemProperties.getInt("debug.sqlite.wal.autocheckpoint", 125 Resources.getSystem().getInteger( 126 com.android.internal.R.integer.db_wal_autocheckpoint)); 127 return Math.max(1, value); 128 } 129 130 /** 131 * Gets the connection pool size when in WAL mode. 132 */ getWALConnectionPoolSize()133 public static int getWALConnectionPoolSize() { 134 int value = SystemProperties.getInt("debug.sqlite.wal.poolsize", 135 Resources.getSystem().getInteger( 136 com.android.internal.R.integer.db_connection_pool_size)); 137 return Math.max(2, value); 138 } 139 140 /** 141 * The default number of milliseconds that SQLite connection is allowed to be idle before it 142 * is closed and removed from the pool. 143 */ getIdleConnectionTimeout()144 public static int getIdleConnectionTimeout() { 145 return SystemProperties.getInt("debug.sqlite.idle_connection_timeout", 146 Resources.getSystem().getInteger( 147 com.android.internal.R.integer.db_default_idle_connection_timeout)); 148 } 149 150 } 151