| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.engine; |
| 7 |
|
| 8 |
import java.util.HashMap; |
| 9 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.message.DbException; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class contains various database-level settings. To override the |
| 15 |
* documented default value for a database, append the setting in the database |
| 16 |
* URL: "jdbc:h2:./test;ANALYZE_SAMPLE=1000" when opening the first connection |
| 17 |
* to the database. The settings can not be changed once the database is open. |
| 18 |
* <p> |
| 19 |
* Some settings are a last resort and temporary solution to work around a |
| 20 |
* problem in the application or database engine. Also, there are system |
| 21 |
* properties to enable features that are not yet fully tested or that are not |
| 22 |
* backward compatible. |
| 23 |
* </p> |
| 24 |
*/ |
| 25 |
public class DbSettings extends SettingsBase { |
| 26 |
|
| 27 |
/** |
| 28 |
* The initial size of the hash table. |
| 29 |
*/ |
| 30 |
static final int TABLE_SIZE = 64; |
| 31 |
|
| 32 |
/** |
| 33 |
* INTERNAL. |
| 34 |
* The default settings. Those must not be modified. |
| 35 |
*/ |
| 36 |
public static final DbSettings DEFAULT = new DbSettings(new HashMap<>(TABLE_SIZE)); |
| 37 |
|
| 38 |
/** |
| 39 |
* Database setting <code>ANALYZE_AUTO</code> (default: 2000). |
| 40 |
* After changing this many rows, ANALYZE is automatically run for a table. |
| 41 |
* Automatically running ANALYZE is disabled if set to 0. If set to 1000, |
| 42 |
* then ANALYZE will run against each user table after about 1000 changes to |
| 43 |
* that table. The time between running ANALYZE doubles each time since |
| 44 |
* starting the database. It is not run on local temporary tables, and |
| 45 |
* tables that have a trigger on SELECT. |
| 46 |
*/ |
| 47 |
public final int analyzeAuto = get("ANALYZE_AUTO", 2000); |
| 48 |
|
| 49 |
/** |
| 50 |
* Database setting <code>ANALYZE_SAMPLE</code> (default: 10000). |
| 51 |
* The default sample size when analyzing a table. |
| 52 |
*/ |
| 53 |
public final int analyzeSample = get("ANALYZE_SAMPLE", 10_000); |
| 54 |
|
| 55 |
/** |
| 56 |
* Database setting <code>AUTO_COMPACT_FILL_RATE</code> |
| 57 |
* (default: 90, which means 90%, 0 disables auto-compacting). |
| 58 |
* Set the auto-compact target fill rate. If the average fill rate (the |
| 59 |
* percentage of the storage space that contains active data) of the |
| 60 |
* chunks is lower, then the chunks with a low fill rate are re-written. |
| 61 |
* Also, if the percentage of empty space between chunks is higher than |
| 62 |
* this value, then chunks at the end of the file are moved. Compaction |
| 63 |
* stops if the target fill rate is reached. |
| 64 |
* This setting only affects MVStore engine. |
| 65 |
*/ |
| 66 |
public final int autoCompactFillRate = get("AUTO_COMPACT_FILL_RATE", 90); |
| 67 |
|
| 68 |
/** |
| 69 |
* Database setting <code>DATABASE_TO_LOWER</code> (default: false). |
| 70 |
* When set to true unquoted identifiers and short name of database are |
| 71 |
* converted to lower case. Value of this setting should not be changed |
| 72 |
* after creation of database. Setting this to "true" is experimental. |
| 73 |
*/ |
| 74 |
public final boolean databaseToLower; |
| 75 |
|
| 76 |
/** |
| 77 |
* Database setting <code>DATABASE_TO_UPPER</code> (default: true). |
| 78 |
* When set to true unquoted identifiers and short name of database are |
| 79 |
* converted to upper case. |
| 80 |
*/ |
| 81 |
public final boolean databaseToUpper; |
| 82 |
|
| 83 |
/** |
| 84 |
* Database setting <code>CASE_INSENSITIVE_IDENTIFIERS</code> (default: |
| 85 |
* false). |
| 86 |
* When set to true, all identifier names (table names, column names) are |
| 87 |
* case insensitive. Setting this to "true" is experimental. |
| 88 |
*/ |
| 89 |
public final boolean caseInsensitiveIdentifiers = get("CASE_INSENSITIVE_IDENTIFIERS", false); |
| 90 |
|
| 91 |
/** |
| 92 |
* Database setting <code>DB_CLOSE_ON_EXIT</code> (default: true). |
| 93 |
* Close the database when the virtual machine exits normally, using a |
| 94 |
* shutdown hook. |
| 95 |
*/ |
| 96 |
public final boolean dbCloseOnExit = get("DB_CLOSE_ON_EXIT", true); |
| 97 |
|
| 98 |
/** |
| 99 |
* Database setting <code>DEFAULT_CONNECTION</code> (default: false). |
| 100 |
* Whether Java functions can use |
| 101 |
* <code>DriverManager.getConnection("jdbc:default:connection")</code> to |
| 102 |
* get a database connection. This feature is disabled by default for |
| 103 |
* performance reasons. Please note the Oracle JDBC driver will try to |
| 104 |
* resolve this database URL if it is loaded before the H2 driver. |
| 105 |
*/ |
| 106 |
public final boolean defaultConnection = get("DEFAULT_CONNECTION", false); |
| 107 |
|
| 108 |
/** |
| 109 |
* Database setting <code>DEFAULT_ESCAPE</code> (default: \). |
| 110 |
* The default escape character for LIKE comparisons. To select no escape |
| 111 |
* character, use an empty string. |
| 112 |
*/ |
| 113 |
public final String defaultEscape = get("DEFAULT_ESCAPE", "\\"); |
| 114 |
|
| 115 |
/** |
| 116 |
* Database setting <code>DEFRAG_ALWAYS</code> (default: false) |
| 117 |
* Each time the database is closed normally, it is fully defragmented (the |
| 118 |
* same as SHUTDOWN DEFRAG). If you execute SHUTDOWN COMPACT, then this |
| 119 |
* setting is ignored. |
| 120 |
*/ |
| 121 |
public final boolean defragAlways = get("DEFRAG_ALWAYS", false); |
| 122 |
|
| 123 |
/** |
| 124 |
* Database setting <code>DROP_RESTRICT</code> (default: true) |
| 125 |
* Whether the default action for DROP TABLE, DROP VIEW, DROP SCHEMA, DROP |
| 126 |
* DOMAIN, and DROP CONSTRAINT is RESTRICT. |
| 127 |
*/ |
| 128 |
public final boolean dropRestrict = get("DROP_RESTRICT", true); |
| 129 |
|
| 130 |
/** |
| 131 |
* Database setting <code>ESTIMATED_FUNCTION_TABLE_ROWS</code> (default: |
| 132 |
* 1000). |
| 133 |
* The estimated number of rows in a function table (for example, CSVREAD or |
| 134 |
* FTL_SEARCH). This value is used by the optimizer. |
| 135 |
*/ |
| 136 |
public final int estimatedFunctionTableRows = get( |
| 137 |
"ESTIMATED_FUNCTION_TABLE_ROWS", 1000); |
| 138 |
|
| 139 |
/** |
| 140 |
* Database setting <code>LOB_TIMEOUT</code> (default: 300000, |
| 141 |
* which means 5 minutes). |
| 142 |
* The number of milliseconds a temporary LOB reference is kept until it |
| 143 |
* times out. After the timeout, the LOB is no longer accessible using this |
| 144 |
* reference. |
| 145 |
*/ |
| 146 |
public final int lobTimeout = get("LOB_TIMEOUT", 300_000); |
| 147 |
|
| 148 |
/** |
| 149 |
* Database setting <code>MAX_COMPACT_TIME</code> (default: 200). |
| 150 |
* The maximum time in milliseconds used to compact a database when closing. |
| 151 |
*/ |
| 152 |
public final int maxCompactTime = get("MAX_COMPACT_TIME", 200); |
| 153 |
|
| 154 |
/** |
| 155 |
* Database setting <code>MAX_QUERY_TIMEOUT</code> (default: 0). |
| 156 |
* The maximum timeout of a query in milliseconds. The default is 0, meaning |
| 157 |
* no limit. Please note the actual query timeout may be set to a lower |
| 158 |
* value. |
| 159 |
*/ |
| 160 |
public final int maxQueryTimeout = get("MAX_QUERY_TIMEOUT", 0); |
| 161 |
|
| 162 |
/** |
| 163 |
* Database setting <code>OPTIMIZE_DISTINCT</code> (default: true). |
| 164 |
* Improve the performance of simple DISTINCT queries if an index is |
| 165 |
* available for the given column. The optimization is used if: |
| 166 |
* <ul> |
| 167 |
* <li>The select is a single column query without condition </li> |
| 168 |
* <li>The query contains only one table, and no group by </li> |
| 169 |
* <li>There is only one table involved </li> |
| 170 |
* <li>There is an ascending index on the column </li> |
| 171 |
* <li>The selectivity of the column is below 20 </li> |
| 172 |
* </ul> |
| 173 |
*/ |
| 174 |
public final boolean optimizeDistinct = get("OPTIMIZE_DISTINCT", true); |
| 175 |
|
| 176 |
/** |
| 177 |
* Database setting <code>OPTIMIZE_EVALUATABLE_SUBQUERIES</code> (default: |
| 178 |
* true). |
| 179 |
* Optimize subqueries that are not dependent on the outer query. |
| 180 |
*/ |
| 181 |
public final boolean optimizeEvaluatableSubqueries = get( |
| 182 |
"OPTIMIZE_EVALUATABLE_SUBQUERIES", true); |
| 183 |
|
| 184 |
/** |
| 185 |
* Database setting <code>OPTIMIZE_INSERT_FROM_SELECT</code> |
| 186 |
* (default: true). |
| 187 |
* Insert into table from query directly bypassing temporary disk storage. |
| 188 |
* This also applies to create table as select. |
| 189 |
*/ |
| 190 |
public final boolean optimizeInsertFromSelect = get( |
| 191 |
"OPTIMIZE_INSERT_FROM_SELECT", true); |
| 192 |
|
| 193 |
/** |
| 194 |
* Database setting <code>OPTIMIZE_IN_LIST</code> (default: true). |
| 195 |
* Optimize IN(...) and IN(SELECT ...) comparisons. This includes |
| 196 |
* optimization for SELECT, DELETE, and UPDATE. |
| 197 |
*/ |
| 198 |
public final boolean optimizeInList = get("OPTIMIZE_IN_LIST", true); |
| 199 |
|
| 200 |
/** |
| 201 |
* Database setting <code>OPTIMIZE_IN_SELECT</code> (default: true). |
| 202 |
* Optimize IN(SELECT ...) comparisons. This includes |
| 203 |
* optimization for SELECT, DELETE, and UPDATE. |
| 204 |
*/ |
| 205 |
public final boolean optimizeInSelect = get("OPTIMIZE_IN_SELECT", true); |
| 206 |
|
| 207 |
/** |
| 208 |
* Database setting <code>OPTIMIZE_OR</code> (default: true). |
| 209 |
* Convert (C=? OR C=?) to (C IN(?, ?)). |
| 210 |
*/ |
| 211 |
public final boolean optimizeOr = get("OPTIMIZE_OR", true); |
| 212 |
|
| 213 |
/** |
| 214 |
* Database setting <code>OPTIMIZE_TWO_EQUALS</code> (default: true). |
| 215 |
* Optimize expressions of the form A=B AND B=1. In this case, AND A=1 is |
| 216 |
* added so an index on A can be used. |
| 217 |
*/ |
| 218 |
public final boolean optimizeTwoEquals = get("OPTIMIZE_TWO_EQUALS", true); |
| 219 |
|
| 220 |
/** |
| 221 |
* Database setting <code>OPTIMIZE_SIMPLE_SINGLE_ROW_SUBQUERIES</code> (default: true). |
| 222 |
* Optimize expressions of the form (SELECT A) to A. |
| 223 |
*/ |
| 224 |
public final boolean optimizeSimpleSingleRowSubqueries = get("OPTIMIZE_SIMPLE_SINGLE_ROW_SUBQUERIES", true); |
| 225 |
|
| 226 |
/** |
| 227 |
* Database setting <code>QUERY_CACHE_SIZE</code> (default: 8). |
| 228 |
* The size of the query cache, in number of cached statements. Each session |
| 229 |
* has it's own cache with the given size. The cache is only used if the SQL |
| 230 |
* statement and all parameters match. Only the last returned result per |
| 231 |
* query is cached. The following statement types are cached: SELECT |
| 232 |
* statements are cached (excluding UNION and FOR UPDATE statements), CALL |
| 233 |
* if it returns a single value, DELETE, INSERT, MERGE, UPDATE, and |
| 234 |
* transactional statements such as COMMIT. This works for both statements |
| 235 |
* and prepared statement. |
| 236 |
*/ |
| 237 |
public final int queryCacheSize = get("QUERY_CACHE_SIZE", 8); |
| 238 |
|
| 239 |
/** |
| 240 |
* Database setting <code>RECOMPILE_ALWAYS</code> (default: false). |
| 241 |
* Always recompile prepared statements. |
| 242 |
*/ |
| 243 |
public final boolean recompileAlways = get("RECOMPILE_ALWAYS", false); |
| 244 |
|
| 245 |
/** |
| 246 |
* Database setting <code>REUSE_SPACE</code> (default: true). |
| 247 |
* If disabled, all changes are appended to the database file, and existing |
| 248 |
* content is never overwritten. This setting has no effect if the database |
| 249 |
* is already open. |
| 250 |
*/ |
| 251 |
public final boolean reuseSpace = get("REUSE_SPACE", true); |
| 252 |
|
| 253 |
/** |
| 254 |
* Database setting <code>SHARE_LINKED_CONNECTIONS</code> |
| 255 |
* (default: true). |
| 256 |
* Linked connections should be shared, that means connections to the same |
| 257 |
* database should be used for all linked tables that connect to the same |
| 258 |
* database. |
| 259 |
*/ |
| 260 |
public final boolean shareLinkedConnections = get( |
| 261 |
"SHARE_LINKED_CONNECTIONS", true); |
| 262 |
|
| 263 |
/** |
| 264 |
* Database setting <code>DEFAULT_TABLE_ENGINE</code> |
| 265 |
* (default: null). |
| 266 |
* The default table engine to use for new tables. |
| 267 |
*/ |
| 268 |
public final String defaultTableEngine = get("DEFAULT_TABLE_ENGINE", null); |
| 269 |
|
| 270 |
/** |
| 271 |
* Database setting <code>MV_STORE</code> |
| 272 |
* (default: true). |
| 273 |
* Use the MVStore storage engine. |
| 274 |
*/ |
| 275 |
public final boolean mvStore = get("MV_STORE", true); |
| 276 |
|
| 277 |
/** |
| 278 |
* Database setting <code>COMPRESS</code> |
| 279 |
* (default: false). |
| 280 |
* Compress data when storing. |
| 281 |
*/ |
| 282 |
public final boolean compressData = get("COMPRESS", false); |
| 283 |
|
| 284 |
/** |
| 285 |
* Database setting <code>IGNORE_CATALOGS</code> |
| 286 |
* (default: false). |
| 287 |
* If set, all catalog names in identifiers are silently accepted |
| 288 |
* without comparing them with the short name of the database. |
| 289 |
*/ |
| 290 |
public final boolean ignoreCatalogs = get("IGNORE_CATALOGS", false); |
| 291 |
|
| 292 |
/** |
| 293 |
* Database setting <code>ZERO_BASED_ENUMS</code> |
| 294 |
* (default: false). |
| 295 |
* If set, ENUM ordinal values are 0-based. |
| 296 |
*/ |
| 297 |
public final boolean zeroBasedEnums = get("ZERO_BASED_ENUMS", false); |
| 298 |
|
| 299 |
private DbSettings(HashMap<String, String> s) { |
| 300 |
super(s); |
| 301 |
boolean lower = get("DATABASE_TO_LOWER", false); |
| 302 |
boolean upperSet = containsKey("DATABASE_TO_UPPER"); |
| 303 |
boolean upper = get("DATABASE_TO_UPPER", true); |
| 304 |
if (lower && upper) { |
| 305 |
if (upperSet) { |
| 306 |
throw DbException.get(ErrorCode.UNSUPPORTED_SETTING_COMBINATION, |
| 307 |
"DATABASE_TO_LOWER & DATABASE_TO_UPPER"); |
| 308 |
} |
| 309 |
upper = false; |
| 310 |
} |
| 311 |
databaseToLower = lower; |
| 312 |
databaseToUpper = upper; |
| 313 |
HashMap<String, String> settings = getSettings(); |
| 314 |
settings.put("DATABASE_TO_LOWER", Boolean.toString(lower)); |
| 315 |
settings.put("DATABASE_TO_UPPER", Boolean.toString(upper)); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* INTERNAL. |
| 320 |
* Get the settings for the given properties (may not be null). |
| 321 |
* |
| 322 |
* @param s the settings |
| 323 |
* @return the settings |
| 324 |
*/ |
| 325 |
static DbSettings getInstance(HashMap<String, String> s) { |
| 326 |
return new DbSettings(s); |
| 327 |
} |
| 328 |
|
| 329 |
} |