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.content.res.Resources;
20 import android.os.StatFs;
21 import android.os.SystemProperties;
22 
23 /**
24  * Provides access to SQLite functions that affect all database connection,
25  * such as memory management.
26  *
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  *
35  * @hide
36  */
37 public final class SQLiteGlobal {
38     private static final String TAG = "SQLiteGlobal";
39 
40     private static final Object sLock = new Object();
41     private static int sDefaultPageSize;
42 
nativeReleaseMemory()43     private static native int nativeReleaseMemory();
44 
SQLiteGlobal()45     private SQLiteGlobal() {
46     }
47 
48     /**
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      */
releaseMemory()54     public static int releaseMemory() {
55         return nativeReleaseMemory();
56     }
57 
58     /**
59      * Gets the default page size to use when creating a database.
60      */
getDefaultPageSize()61     public static int getDefaultPageSize() {
62         synchronized (sLock) {
63             if (sDefaultPageSize == 0) {
64                 sDefaultPageSize = new StatFs("/data").getBlockSize();
65             }
66             return SystemProperties.getInt("debug.sqlite.pagesize", sDefaultPageSize);
67         }
68     }
69 
70     /**
71      * Gets the default journal mode when WAL is not in use.
72      */
getDefaultJournalMode()73     public static String getDefaultJournalMode() {
74         return SystemProperties.get("debug.sqlite.journalmode",
75                 Resources.getSystem().getString(
76                 com.android.internal.R.string.db_default_journal_mode));
77     }
78 
79     /**
80      * Gets the journal size limit in bytes.
81      */
getJournalSizeLimit()82     public static int getJournalSizeLimit() {
83         return SystemProperties.getInt("debug.sqlite.journalsizelimit",
84                 Resources.getSystem().getInteger(
85                 com.android.internal.R.integer.db_journal_size_limit));
86     }
87 
88     /**
89      * Gets the default database synchronization mode when WAL is not in use.
90      */
getDefaultSyncMode()91     public static String getDefaultSyncMode() {
92         return SystemProperties.get("debug.sqlite.syncmode",
93                 Resources.getSystem().getString(
94                 com.android.internal.R.string.db_default_sync_mode));
95     }
96 
97     /**
98      * Gets the database synchronization mode when in WAL mode.
99      */
getWALSyncMode()100     public static String getWALSyncMode() {
101         return SystemProperties.get("debug.sqlite.wal.syncmode",
102                 Resources.getSystem().getString(
103                 com.android.internal.R.string.db_wal_sync_mode));
104     }
105 
106     /**
107      * Gets the WAL auto-checkpoint integer in database pages.
108      */
getWALAutoCheckpoint()109     public static int getWALAutoCheckpoint() {
110         int value = SystemProperties.getInt("debug.sqlite.wal.autocheckpoint",
111                 Resources.getSystem().getInteger(
112                 com.android.internal.R.integer.db_wal_autocheckpoint));
113         return Math.max(1, value);
114     }
115 
116     /**
117      * Gets the connection pool size when in WAL mode.
118      */
getWALConnectionPoolSize()119     public static int getWALConnectionPoolSize() {
120         int value = SystemProperties.getInt("debug.sqlite.wal.poolsize",
121                 Resources.getSystem().getInteger(
122                 com.android.internal.R.integer.db_connection_pool_size));
123         return Math.max(2, value);
124     }
125 }
126