1 30 31 32 package org.hsqldb.persist; 33 34 import java.util.Enumeration ; 35 36 import org.hsqldb.Database; 37 import org.hsqldb.DatabaseURL; 38 import org.hsqldb.HsqlException; 39 import org.hsqldb.Trace; 40 import org.hsqldb.lib.HashMap; 41 import org.hsqldb.lib.HashSet; 42 import org.hsqldb.lib.Iterator; 43 import org.hsqldb.lib.Set; 44 import org.hsqldb.lib.SimpleLog; 45 import org.hsqldb.lib.java.JavaSystem; 46 import org.hsqldb.store.ValuePool; 47 48 55 public class HsqlDatabaseProperties extends HsqlProperties { 56 57 public static final int indexName = 0; 59 public static final int indexAccess = 1; 60 public static final int indexClass = 2; 61 public static final int indexIsRange = 3; 62 public static final int indexDefaultValue = 4; 63 public static final int indexRangeLow = 5; 64 public static final int indexRangeHigh = 6; 65 public static final int indexValues = 7; 66 public static final int indexLimit = 8; 67 68 private static final int SET_PROPERTY = 0; 70 private static final int SQL_PROPERTY = 1; 71 private static final int FILE_PROPERTY = 2; 72 73 public static final int FILES_NOT_MODIFIED = 0; 75 public static final int FILES_MODIFIED = 1; 76 public static final int FILES_NEW = 2; 77 private static final String MODIFIED_NO = "no"; 78 private static final String MODIFIED_YES = "yes"; 79 private static final String MODIFIED_NEW = "no-new-files"; 80 81 private static HashMap meta = new HashMap(); 83 84 public static final String VERSION_STRING_1_7_0 = "1.7.0"; 86 public static final String VERSION_STRING_1_8_0 = "1.8.0"; 87 public static final String FIRST_COMPATIBLE_VERSION = "1.8.0"; 88 public static final String THIS_VERSION = "1.8.0"; 89 public static final String THIS_FULL_VERSION = "1.8.0.7"; 90 public static final String PRODUCT_NAME = "HSQL Database Engine"; 91 public static final int MAJOR = 1, 92 MINOR = 8, 93 REVISION = 0; 94 95 public static final String db_version = "version"; 97 private static final String db_readonly = "readonly"; 98 private static final String db_modified = "modified"; 99 100 private static final String runtime_gc_interval = "runtime.gc_interval"; 102 public static final String hsqldb_applog = "hsqldb.applog"; 103 public static final String hsqldb_cache_file_scale = 104 "hsqldb.cache_file_scale"; 105 public static final String hsqldb_cache_free_count_scale = 106 "hsqldb.cache_free_count_scale"; 107 public static final String hsqldb_cache_scale = "hsqldb.cache_scale"; 108 public static final String hsqldb_cache_size_scale = 109 "hsqldb.cache_size_scale"; 110 public static final String hsqldb_cache_version = "hsqldb.cache_version"; 111 private static final String hsqldb_catalogs = "hsqldb.catalogs"; 112 public static final String hsqldb_compatible_version = 113 "hsqldb.compatible_version"; 114 public static final String hsqldb_default_table_type = 115 "hsqldb.default_table_type"; 116 public static final String hsqldb_defrag_limit = "hsqldb.defrag_limit"; 117 private static final String hsqldb_files_readonly = 118 "hsqldb.files_readonly"; 119 public static final String hsqldb_log_size = "hsqldb.log_size"; 120 public static final String hsqldb_nio_data_file = "hsqldb.nio_data_file"; 121 public static final String hsqldb_max_nio_scale = "hsqldb.max_nio_scale"; 122 public static final String hsqldb_raf_buffer_scale = 123 "hsqldb.raf_buffer_scale"; 124 private static final String hsqldb_original_version = 125 "hsqldb.original_version"; 126 public static final String hsqldb_script_format = "hsqldb.script_format"; 127 128 private static final String sql_compare_in_locale = 130 "sql.compare_in_locale"; 131 private static final String sql_enforce_strict_size = 132 "sql.enforce_strict_size"; 133 public static final String sql_tx_no_multi_write = 134 "sql.tx_no_multi_rewrite"; 135 136 public static final String textdb_cache_scale = "textdb.cache_scale"; 138 public static final String textdb_cache_size_scale = 139 "textdb.cache_size_scale"; 140 public static final String textdb_all_quoted = "textdb.all_quoted"; 141 public static final String textdb_allow_full_path = 142 "textdb.allow_full_path"; 143 public static final String textdb_encoding = "textdb.encoding"; 144 public static final String textdb_ignore_first = "textdb.ignore_first"; 145 public static final String textdb_quoted = "textdb.quoted"; 146 public static final String textdb_fs = "textdb.fs"; 147 public static final String textdb_vs = "textdb.vs"; 148 public static final String textdb_lvs = "textdb.lvs"; 149 150 static { 151 152 meta.put(db_version, getMeta(db_version, FILE_PROPERTY, null)); 154 meta.put(hsqldb_compatible_version, 155 getMeta(hsqldb_compatible_version, FILE_PROPERTY, null)); 156 meta.put(hsqldb_cache_version, 157 getMeta(hsqldb_cache_version, FILE_PROPERTY, null)); 158 meta.put(hsqldb_original_version, 159 getMeta(hsqldb_original_version, FILE_PROPERTY, null)); 160 meta.put(db_modified, getMeta(db_modified, FILE_PROPERTY, null)); 161 162 meta.put(hsqldb_default_table_type, 164 getMeta(hsqldb_default_table_type, SET_PROPERTY, "memory")); 165 meta.put(textdb_fs, getMeta(textdb_fs, SET_PROPERTY, ",")); 166 meta.put(textdb_vs, getMeta(textdb_vs, SET_PROPERTY, null)); 167 meta.put(textdb_lvs, getMeta(textdb_lvs, SET_PROPERTY, null)); 168 meta.put(textdb_encoding, 169 getMeta(textdb_encoding, SET_PROPERTY, null)); 170 171 meta.put(db_readonly, getMeta(db_readonly, FILE_PROPERTY, false)); 173 meta.put(hsqldb_files_readonly, 174 getMeta(hsqldb_files_readonly, FILE_PROPERTY, false)); 175 meta.put(textdb_allow_full_path, 176 getMeta(textdb_allow_full_path, FILE_PROPERTY, false)); 177 178 meta.put(hsqldb_nio_data_file, 180 getMeta(hsqldb_nio_data_file, SET_PROPERTY, false)); 181 meta.put(hsqldb_catalogs, 182 getMeta(hsqldb_catalogs, SET_PROPERTY, false)); 183 meta.put(sql_enforce_strict_size, 184 getMeta(sql_enforce_strict_size, SET_PROPERTY, false)); 185 meta.put(sql_tx_no_multi_write, 186 getMeta(sql_tx_no_multi_write, SET_PROPERTY, false)); 187 meta.put(textdb_quoted, getMeta(textdb_quoted, SET_PROPERTY, false)); 188 meta.put(textdb_all_quoted, 189 getMeta(textdb_all_quoted, SET_PROPERTY, false)); 190 meta.put(textdb_ignore_first, 191 getMeta(textdb_ignore_first, SET_PROPERTY, false)); 192 193 meta.put(hsqldb_applog, 195 getMeta(hsqldb_applog, SET_PROPERTY, 0, new byte[] { 196 0, 1, 2 197 })); 198 meta.put(hsqldb_cache_file_scale, 199 getMeta(hsqldb_cache_file_scale, SET_PROPERTY, 1, 200 new byte[] { 201 1, 8 202 })); 203 meta.put(hsqldb_script_format, 204 getMeta(hsqldb_script_format, SET_PROPERTY, 0, new byte[] { 205 0, 1, 3 206 })); 207 208 meta.put(hsqldb_log_size, 210 getMeta(hsqldb_log_size, SQL_PROPERTY, 0, 0, 16000)); 211 meta.put(hsqldb_defrag_limit, 212 getMeta(hsqldb_defrag_limit, SQL_PROPERTY, 200, 0, 16000)); 213 214 meta.put(runtime_gc_interval, 216 getMeta(runtime_gc_interval, SET_PROPERTY, 0, 0, 1000000)); 217 meta.put(hsqldb_cache_free_count_scale, 218 getMeta(hsqldb_cache_free_count_scale, SET_PROPERTY, 9, 6, 219 12)); 220 meta.put(hsqldb_cache_scale, 221 getMeta(hsqldb_cache_scale, SET_PROPERTY, 14, 8, 18)); 222 meta.put(hsqldb_cache_size_scale, 223 getMeta(hsqldb_cache_size_scale, SET_PROPERTY, 10, 6, 20)); 224 meta.put(hsqldb_max_nio_scale, 225 getMeta(hsqldb_max_nio_scale, SET_PROPERTY, 28, 24, 31)); 226 meta.put(hsqldb_raf_buffer_scale, 227 getMeta(hsqldb_raf_buffer_scale, SET_PROPERTY, 12, 8, 13)); 228 meta.put(textdb_cache_scale, 229 getMeta(textdb_cache_scale, SET_PROPERTY, 10, 8, 16)); 230 meta.put(textdb_cache_size_scale, 231 getMeta(textdb_cache_size_scale, SET_PROPERTY, 10, 6, 20)); 232 } 233 234 private Database database; 235 236 public HsqlDatabaseProperties(Database db) { 237 238 super(db.getPath(), db.getFileAccess(), db.isFilesInJar()); 239 240 database = db; 241 242 setProperty(sql_enforce_strict_size, false); 244 245 setProperty(db_version, THIS_VERSION); 260 261 setProperty(hsqldb_compatible_version, FIRST_COMPATIBLE_VERSION); 264 265 setProperty(hsqldb_cache_version, VERSION_STRING_1_7_0); 268 269 setProperty(hsqldb_original_version, THIS_VERSION); 272 292 293 setProperty(runtime_gc_interval, 0); 298 299 setProperty(hsqldb_cache_file_scale, 1); 301 302 setProperty(hsqldb_cache_size_scale, 8); 304 305 setProperty(hsqldb_cache_scale, 14); 312 313 setProperty(hsqldb_log_size, 200); 315 316 setProperty(hsqldb_script_format, 0); 318 setProperty(db_readonly, false); 319 setProperty(db_modified, "no-new-files"); 320 321 setProperty(hsqldb_nio_data_file, true); 323 324 setProperty(hsqldb_default_table_type, "memory"); 326 327 if (db.isStoredFileAccess()) { 343 setProperty(hsqldb_default_table_type, "cached"); 344 setProperty(hsqldb_cache_scale, 13); 345 setProperty(hsqldb_log_size, 10); 346 setProperty(sql_enforce_strict_size, true); 347 setProperty(hsqldb_nio_data_file, false); 348 } 349 } 350 351 355 public boolean load() throws HsqlException { 356 357 boolean exists; 358 359 if (!DatabaseURL.isFileBasedDatabaseType(database.getType())) { 360 return true; 361 } 362 363 try { 364 exists = super.load(); 365 } catch (Exception e) { 366 throw Trace.error(Trace.FILE_IO_ERROR, 367 Trace.LOAD_SAVE_PROPERTIES, new Object [] { 368 fileName, e 369 }); 370 } 371 372 if (!exists) { 373 return false; 374 } 375 376 filterLoadedProperties(); 377 378 String version = getProperty(hsqldb_compatible_version); 379 380 int check = version.substring(0, 5).compareTo(THIS_VERSION); 382 383 Trace.check(check <= 0, Trace.WRONG_DATABASE_FILE_VERSION); 384 385 version = getProperty(db_version); 386 387 if (version.charAt(2) == '6') { 388 setProperty(hsqldb_cache_version, "1.6.0"); 389 } 390 391 JavaSystem.gcFrequency = getIntegerProperty(runtime_gc_interval, 0); 392 393 return true; 394 } 395 396 400 public void setDatabaseVariables() { 401 402 if (isPropertyTrue(db_readonly)) { 403 database.setReadOnly(); 404 } 405 406 if (isPropertyTrue(hsqldb_files_readonly)) { 407 database.setFilesReadOnly(); 408 } 409 410 database.sqlEnforceStrictSize = 411 isPropertyTrue(sql_enforce_strict_size); 412 413 if (isPropertyTrue(sql_compare_in_locale)) { 414 stringProps.remove(sql_compare_in_locale); 415 database.collation.setCollationAsLocale(); 416 } 417 418 database.txManager.setReWriteProtection( 419 isPropertyTrue(sql_tx_no_multi_write)); 420 database.setMetaDirty(false); 421 } 422 423 public void save() throws HsqlException { 424 425 if (!DatabaseURL.isFileBasedDatabaseType(database.getType()) 426 || database.isFilesReadOnly() || database.isFilesInJar()) { 427 return; 428 } 429 430 try { 431 super.save(fileName + ".properties" + ".new"); 432 fa.renameElement(fileName + ".properties" + ".new", 433 fileName + ".properties"); 434 } catch (Exception e) { 435 database.logger.appLog.logContext(SimpleLog.LOG_ERROR, "failed"); 436 437 throw Trace.error(Trace.FILE_IO_ERROR, 438 Trace.LOAD_SAVE_PROPERTIES, new Object [] { 439 fileName, e 440 }); 441 } 442 } 443 444 void filterLoadedProperties() { 445 446 Enumeration en = stringProps.propertyNames(); 447 448 while (en.hasMoreElements()) { 449 String key = (String ) en.nextElement(); 450 boolean accept = meta.containsKey(key); 451 452 if (!accept) { 453 stringProps.remove(key); 454 } 455 } 456 } 457 458 462 public void setURLProperties(HsqlProperties p) { 463 464 if (p != null) { 465 for (Enumeration e = p.propertyNames(); e.hasMoreElements(); ) { 466 String propertyName = (String ) e.nextElement(); 467 Object [] row = (Object []) meta.get(propertyName); 468 469 if (row != null 470 && (db_readonly.equals(propertyName) 471 || ((Integer ) row[indexAccess]).intValue() 472 == SET_PROPERTY)) { 473 474 setProperty(propertyName, p.getProperty(propertyName)); 476 } 477 } 478 } 479 } 480 481 public Set getUserDefinedPropertyData() { 482 483 Set set = new HashSet(); 484 Iterator it = meta.values().iterator(); 485 486 while (it.hasNext()) { 487 Object [] row = (Object []) it.next(); 488 489 if (((Integer ) row[indexAccess]).intValue() == SET_PROPERTY) { 490 set.add(row); 491 } 492 } 493 494 return set; 495 } 496 497 public boolean isUserDefinedProperty(String key) { 498 499 Object [] row = (Object []) meta.get(key); 500 501 return row != null 502 && ((Integer ) row[indexAccess]).intValue() == SET_PROPERTY; 503 } 504 505 public boolean isBoolean(String key) { 506 507 Object [] row = (Object []) meta.get(key); 508 509 return row != null && row[indexClass].equals("boolean") 510 && ((Integer ) row[indexAccess]).intValue() == SET_PROPERTY; 511 } 512 513 public boolean isIntegral(String key) { 514 515 Object [] row = (Object []) meta.get(key); 516 517 return row != null && row[indexClass].equals("int") 518 && ((Integer ) row[indexAccess]).intValue() == SET_PROPERTY; 519 } 520 521 public boolean isString(String key) { 522 523 Object [] row = (Object []) meta.get(key); 524 525 return row != null && row[indexClass].equals("java.lang.String") 526 && ((Integer ) row[indexAccess]).intValue() == SET_PROPERTY; 527 } 528 529 public String setDatabaseProperty(String key, 530 String value) throws HsqlException { 531 532 Object [] row = (Object []) meta.get(key); 533 534 value = super.setProperty(key, value); 536 537 return value; 538 } 539 540 public int getDefaultWriteDelay() { 541 return database.isStoredFileAccess() ? 2000 542 : 10000; 543 } 544 545 public void setDBModified(int mode) throws HsqlException { 546 547 String value = MODIFIED_NO; 548 549 if (mode == FILES_MODIFIED) { 550 value = MODIFIED_YES; 551 } else if (mode == FILES_NEW) { 552 value = MODIFIED_NEW; 553 } 554 555 setProperty(db_modified, value); 556 save(); 557 } 558 559 public int getDBModified() throws HsqlException { 560 561 String value = getProperty("modified"); 562 563 if (MODIFIED_YES.equals(value)) { 564 return FILES_MODIFIED; 565 } else if (MODIFIED_NEW.equals(value)) { 566 return FILES_NEW; 567 } 568 569 return FILES_NOT_MODIFIED; 570 } 571 572 private static Object [] getMeta(String name, int accessLevel, 573 String defaultValue) { 574 575 Object [] row = new Object [indexLimit]; 576 577 row[indexName] = name; 578 row[indexAccess] = ValuePool.getInt(accessLevel); 579 row[indexClass] = "java.lang.String"; 580 row[indexDefaultValue] = defaultValue; 581 582 return row; 583 } 584 585 private static Object [] getMeta(String name, int accessLevel, 586 boolean defaultValue) { 587 588 Object [] row = new Object [indexLimit]; 589 590 row[indexName] = name; 591 row[indexAccess] = ValuePool.getInt(accessLevel); 592 row[indexClass] = "boolean"; 593 row[indexDefaultValue] = defaultValue ? Boolean.TRUE 594 : Boolean.FALSE; 595 596 return row; 597 } 598 599 private static Object [] getMeta(String name, int accessLevel, 600 int defaultValue, byte[] values) { 601 602 Object [] row = new Object [indexLimit]; 603 604 row[indexName] = name; 605 row[indexAccess] = ValuePool.getInt(accessLevel); 606 row[indexClass] = "int"; 607 row[indexDefaultValue] = ValuePool.getInt(defaultValue); 608 row[indexValues] = values; 609 610 return row; 611 } 612 613 private static Object [] getMeta(String name, int accessLevel, 614 int defaultValue, int rangeLow, 615 int rangeHigh) { 616 617 Object [] row = new Object [indexLimit]; 618 619 row[indexName] = name; 620 row[indexAccess] = ValuePool.getInt(accessLevel); 621 row[indexClass] = "int"; 622 row[indexDefaultValue] = ValuePool.getInt(defaultValue); 623 row[indexIsRange] = Boolean.TRUE; 624 row[indexRangeLow] = ValuePool.getInt(rangeLow); 625 row[indexRangeHigh] = ValuePool.getInt(rangeHigh); 626 627 return row; 628 } 629 } 630 | Popular Tags |