1 8 9 package com.sleepycat.je; 10 11 import java.util.Arrays ; 12 import java.util.Comparator ; 13 14 import com.sleepycat.je.dbi.DatabaseImpl; 15 16 20 public class DatabaseConfig implements Cloneable { 21 22 26 public static final DatabaseConfig DEFAULT = new DatabaseConfig(); 27 28 private boolean allowCreate = false; 29 private boolean exclusiveCreate = false; 30 private boolean transactional = false; 31 private boolean readOnly = false; 32 private boolean duplicatesAllowed = false; 33 private boolean deferredWrite = false; 34 35 36 private int nodeMax; 37 private int nodeMaxDupTree; 38 private Comparator btreeComparator = null; 39 private Comparator duplicateComparator = null; 40 private boolean btreeComparatorByClassName = false; 41 private boolean duplicateComparatorByClassName = false; 42 private boolean overrideBtreeComparator = false; 43 private boolean overrideDupComparator = false; 44 private boolean useExistingConfig = false; 45 46 50 public DatabaseConfig() { 51 } 52 53 57 public void setAllowCreate(boolean allowCreate) { 58 this.allowCreate = allowCreate; 59 } 60 61 65 public boolean getAllowCreate() { 66 return allowCreate; 67 } 68 69 73 public void setExclusiveCreate(boolean exclusiveCreate) { 74 this.exclusiveCreate = exclusiveCreate; 75 } 76 77 81 public boolean getExclusiveCreate() { 82 return exclusiveCreate; 83 } 84 85 89 public void setSortedDuplicates(boolean duplicatesAllowed) { 90 this.duplicatesAllowed = duplicatesAllowed; 91 } 92 93 97 public boolean getSortedDuplicates() { 98 return duplicatesAllowed; 99 } 100 101 105 public void setTransactional(boolean transactional) { 106 this.transactional = transactional; 107 } 108 112 public boolean getTransactional() { 113 return transactional; 114 } 115 116 120 public void setReadOnly(boolean readOnly) { 121 this.readOnly = readOnly; 122 } 123 124 128 public boolean getReadOnly() { 129 return readOnly; 130 } 131 132 136 public void setNodeMaxEntries(int nodeMaxEntries) { 137 this.nodeMax = nodeMaxEntries; 138 } 139 140 144 public void setNodeMaxDupTreeEntries(int nodeMaxDupTreeEntries) { 145 this.nodeMaxDupTree = nodeMaxDupTreeEntries; 146 } 147 148 152 public int getNodeMaxEntries() { 153 return nodeMax; 154 } 155 156 160 public int getNodeMaxDupTreeEntries() { 161 return nodeMaxDupTree; 162 } 163 164 168 public void setBtreeComparator(Comparator btreeComparator) { 169 170 this.btreeComparator = validateComparator(btreeComparator, "Btree"); 171 this.btreeComparatorByClassName = false; 172 } 173 174 178 public void setBtreeComparator(Class btreeComparator) { 179 180 this.btreeComparator = validateComparator(btreeComparator, "Btree"); 181 this.btreeComparatorByClassName = true; 182 } 183 184 188 public Comparator getBtreeComparator() { 189 return btreeComparator; 190 } 191 192 196 public boolean getBtreeComparatorByClassName() { 197 return btreeComparatorByClassName; 198 } 199 200 204 public void setOverrideBtreeComparator(boolean override) { 205 overrideBtreeComparator = override; 206 } 207 208 212 public boolean getOverrideBtreeComparator() { 213 return overrideBtreeComparator; 214 } 215 216 220 public void setDuplicateComparator(Comparator duplicateComparator) { 221 222 this.duplicateComparator = 223 validateComparator(duplicateComparator, "Duplicate"); 224 this.duplicateComparatorByClassName = false; 225 } 226 227 231 public void setDuplicateComparator(Class duplicateComparator) { 232 233 this.duplicateComparator = 234 validateComparator(duplicateComparator, "Duplicate"); 235 this.duplicateComparatorByClassName = true; 236 } 237 238 242 public Comparator getDuplicateComparator() { 243 return duplicateComparator; 244 } 245 246 250 public boolean getDuplicateComparatorByClassName() { 251 return duplicateComparatorByClassName; 252 } 253 254 258 public void setOverrideDuplicateComparator(boolean override) { 259 overrideDupComparator = override; 260 } 261 262 266 public boolean getOverrideDuplicateComparator() { 267 return overrideDupComparator; 268 } 269 270 274 public void setDeferredWrite(boolean deferredWrite) { 275 this.deferredWrite = deferredWrite; 276 } 277 278 282 public boolean getDeferredWrite() { 283 return deferredWrite; 284 } 285 286 290 void setBtreeComparatorInternal(Comparator comparator, 291 boolean byClassName) { 292 btreeComparator = comparator; 293 btreeComparatorByClassName = byClassName; 294 } 295 296 300 void setDuplicateComparatorInternal(Comparator comparator, 301 boolean byClassName) { 302 duplicateComparator = comparator; 303 duplicateComparatorByClassName = byClassName; 304 } 305 306 309 void setUseExistingConfig(boolean useExistingConfig) { 310 this.useExistingConfig = useExistingConfig; 311 } 312 313 316 boolean getUseExistingConfig() { 317 return useExistingConfig; 318 } 319 320 324 public DatabaseConfig cloneConfig() { 325 try { 326 return (DatabaseConfig) super.clone(); 327 } catch (CloneNotSupportedException willNeverOccur) { 328 return null; 329 } 330 } 331 332 335 void validate(DatabaseConfig config) 336 throws DatabaseException { 337 338 if (config == null) { 339 config = DatabaseConfig.DEFAULT; 340 } 341 342 boolean txnMatch = (config.transactional == transactional); 343 boolean roMatch = (config.readOnly == readOnly); 344 boolean sdMatch = (config.duplicatesAllowed == duplicatesAllowed); 345 boolean dwMatch = (config.getDeferredWrite() == deferredWrite); 346 boolean btCmpMatch = true; 347 if (config.overrideBtreeComparator) { 348 if (btreeComparator == null) { 349 btCmpMatch = (config.btreeComparator == null); 350 } else if (config.btreeComparatorByClassName != 351 btreeComparatorByClassName) { 352 btCmpMatch = false; 353 } else if (btreeComparatorByClassName) { 354 btCmpMatch = btreeComparator.getClass() == 355 config.btreeComparator.getClass(); 356 } else { 357 btCmpMatch = Arrays.equals 358 (DatabaseImpl.objectToBytes 359 (btreeComparator, "Btree"), 360 DatabaseImpl.objectToBytes 361 (config.btreeComparator, "Btree")); 362 } 363 } 364 boolean dtCmpMatch = true; 365 if (config.overrideDupComparator) { 366 if (duplicateComparator == null) { 367 dtCmpMatch = (config.duplicateComparator == null); 368 } else if (config.duplicateComparatorByClassName != 369 duplicateComparatorByClassName) { 370 dtCmpMatch = false; 371 } else if (duplicateComparatorByClassName) { 372 dtCmpMatch = duplicateComparator.getClass() == 373 config.duplicateComparator.getClass(); 374 } else { 375 dtCmpMatch = Arrays.equals 376 (DatabaseImpl.objectToBytes 377 (duplicateComparator, "Duplicate"), 378 DatabaseImpl.objectToBytes 379 (config.duplicateComparator, "Duplicate")); 380 } 381 } 382 383 if (txnMatch && 384 roMatch && 385 sdMatch && 386 dwMatch && 387 btCmpMatch && 388 dtCmpMatch) { 389 return; 390 } else { 391 String message = 392 genDatabaseConfigMismatchMessage 393 (config, txnMatch, roMatch, sdMatch, dwMatch, 394 btCmpMatch, dtCmpMatch); 395 throw new DatabaseException(message); 396 } 397 } 398 399 private String genDatabaseConfigMismatchMessage(DatabaseConfig config, 400 boolean txnMatch, 401 boolean roMatch, 402 boolean sdMatch, 403 boolean dwMatch, 404 boolean btCmpMatch, 405 boolean dtCmpMatch) { 406 StringBuffer ret = new StringBuffer 407 ("The following DatabaseConfig parameters for the\n" + 408 "cached Database do not match the parameters for the\n" + 409 "requested Database:\n"); 410 if (!txnMatch) { 411 ret.append(" Transactional\n"); 412 } 413 414 if (!roMatch) { 415 ret.append(" Read-Only\n"); 416 } 417 418 if (!sdMatch) { 419 ret.append(" Sorted Duplicates\n"); 420 } 421 422 if (!dwMatch) { 423 ret.append(" Deferred Write"); 424 } 425 426 if (!btCmpMatch) { 427 ret.append(" Btree Comparator\n"); 428 } 429 430 if (!dtCmpMatch) { 431 ret.append(" Duplicate Comparator\n"); 432 } 433 434 return ret.toString(); 435 } 436 437 440 private Comparator validateComparator(Comparator comparator, String type) 441 throws IllegalArgumentException { 442 443 if (comparator == null) { 444 return null; 445 } 446 447 try { 448 return DatabaseImpl.instantiateComparator(comparator, type); 449 } catch (DatabaseException e) { 450 throw new IllegalArgumentException 451 (type + 452 " comparator is not valid: " + 453 e.getMessage() + 454 "\nThe comparator object must be serializable."); 455 } 456 } 457 458 461 private Comparator validateComparator(Class comparator, String type) 462 throws IllegalArgumentException { 463 464 if (comparator == null) { 465 return null; 466 } 467 468 if (!Comparator .class.isAssignableFrom(comparator)) { 469 throw new IllegalArgumentException 470 (comparator.getName() + 471 " is is not valid as a " + type + 472 " comparator because it does not " + 473 " implement java.util.Comparator."); 474 } 475 476 try { 477 return DatabaseImpl.instantiateComparator(comparator, type); 478 } catch (DatabaseException e) { 479 throw new IllegalArgumentException 480 (type + 481 " comparator is not valid: " + 482 e.getMessage() + 483 "\nPerhaps you have not implemented a zero-parameter " + 484 "constructor for the comparator or the comparator class " + 485 "cannot be found."); 486 } 487 } 488 489 493 void validateForNewDb() 494 throws DatabaseException { 495 496 if (readOnly) { 497 throw new DatabaseException 498 ("DatabaseConfig.setReadOnly() must be set to false " + 499 "when creating a Database"); 500 } 501 502 if (transactional && deferredWrite) { 503 throw new DatabaseException("deferredWrite mode is not yet " + 504 "supported for transactional " + 505 "databases"); 506 } 507 } 508 } 509 | Popular Tags |