00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.db;
00011
00012 import com.sleepycat.db.internal.Db;
00013 import com.sleepycat.db.internal.DbConstants;
00014 import com.sleepycat.db.internal.DbEnv;
00015 import com.sleepycat.db.internal.DbTxn;
00016 import com.sleepycat.db.internal.DbUtil;
00017
00018 public class DatabaseConfig implements Cloneable {
00019
00020
00021
00022
00023 public static final DatabaseConfig DEFAULT = new DatabaseConfig();
00024
00025
00026 static DatabaseConfig checkNull(DatabaseConfig config) {
00027 return (config == null) ? DEFAULT : config;
00028 }
00029
00030
00031 private DatabaseType type = DatabaseType.UNKNOWN;
00032 private int mode = 0644;
00033 private int btMinKey = 0;
00034 private int byteOrder = 0;
00035 private long cacheSize = 0L;
00036 private int cacheCount = 0;
00037 private java.io.OutputStream errorStream = null;
00038 private String errorPrefix = null;
00039 private int hashFillFactor = 0;
00040 private int hashNumElements = 0;
00041 private java.io.OutputStream messageStream = null;
00042 private int pageSize = 0;
00043 private String password = null;
00044 private int queueExtentSize = 0;
00045 private int recordDelimiter = 0;
00046 private int recordLength = 0;
00047 private int recordPad = -1;
00048 private java.io.File recordSource = null;
00049
00050
00051 private boolean allowCreate = false;
00052 private boolean btreeRecordNumbers = false;
00053 private boolean checksum = false;
00054 private boolean readUncommitted = false;
00055 private boolean encrypted = false;
00056 private boolean exclusiveCreate = false;
00057 private boolean noMMap = false;
00058 private boolean queueInOrder = false;
00059 private boolean readOnly = false;
00060 private boolean renumbering = false;
00061 private boolean reverseSplitOff = false;
00062 private boolean sortedDuplicates = false;
00063 private boolean snapshot = false;
00064 private boolean unsortedDuplicates = false;
00065 private boolean transactional = false;
00066 private boolean transactionNotDurable = false;
00067 private boolean truncate = false;
00068 private boolean xaCreate = false;
00069
00070 private java.util.Comparator btreeComparator = null;
00071 private BtreePrefixCalculator btreePrefixCalculator = null;
00072 private java.util.Comparator duplicateComparator = null;
00073 private FeedbackHandler feedbackHandler = null;
00074 private ErrorHandler errorHandler = null;
00075 private MessageHandler messageHandler = null;
00076 private Hasher hasher = null;
00077 private RecordNumberAppender recnoAppender = null;
00078 private PanicHandler panicHandler = null;
00079
00080 public DatabaseConfig() {
00081 }
00082
00083 public void setAllowCreate(final boolean allowCreate) {
00084 this.allowCreate = allowCreate;
00085 }
00086
00087 public boolean getAllowCreate() {
00088 return allowCreate;
00089 }
00090
00091 public void setBtreeComparator(final java.util.Comparator btreeComparator) {
00092 this.btreeComparator = btreeComparator;
00093 }
00094
00095 public java.util.Comparator getBtreeComparator() {
00096 return btreeComparator;
00097 }
00098
00099 public void setBtreeMinKey(final int btMinKey) {
00100 this.btMinKey = btMinKey;
00101 }
00102
00103 public int getBtreeMinKey() {
00104 return btMinKey;
00105 }
00106
00107 public void setByteOrder(final int byteOrder) {
00108 this.byteOrder = byteOrder;
00109 }
00110
00111 public int getByteOrder() {
00112 return byteOrder;
00113 }
00114
00115 public boolean getByteSwapped() {
00116 return byteOrder != 0 && byteOrder != DbUtil.default_lorder();
00117 }
00118
00119 public void setBtreePrefixCalculator(
00120 final BtreePrefixCalculator btreePrefixCalculator) {
00121 this.btreePrefixCalculator = btreePrefixCalculator;
00122 }
00123
00124 public BtreePrefixCalculator getBtreePrefixCalculator() {
00125 return btreePrefixCalculator;
00126 }
00127
00128 public void setCacheSize(final long cacheSize) {
00129 this.cacheSize = cacheSize;
00130 }
00131
00132 public long getCacheSize() {
00133 return cacheSize;
00134 }
00135
00136 public void setCacheCount(final int cacheCount) {
00137 this.cacheCount = cacheCount;
00138 }
00139
00140 public int getCacheCount() {
00141 return cacheCount;
00142 }
00143
00144 public void setChecksum(final boolean checksum) {
00145 this.checksum = checksum;
00146 }
00147
00148 public boolean getChecksum() {
00149 return checksum;
00150 }
00151
00152 public void setReadUncommitted(final boolean readUncommitted) {
00153 this.readUncommitted = readUncommitted;
00154 }
00155
00156 public boolean getReadUncommitted() {
00157 return readUncommitted;
00158 }
00159
00161 public void setDirtyRead(final boolean dirtyRead) {
00162 setReadUncommitted(dirtyRead);
00163 }
00164
00166 public boolean getDirtyRead() {
00167 return getReadUncommitted();
00168 }
00169
00170 public void setDuplicateComparator(final java.util.Comparator duplicateComparator) {
00171 this.duplicateComparator = duplicateComparator;
00172 }
00173
00174 public java.util.Comparator getDuplicateComparator() {
00175 return duplicateComparator;
00176 }
00177
00178 public void setEncrypted(final String password) {
00179 this.password = password;
00180 }
00181
00182 public boolean getEncrypted() {
00183 return (password != null);
00184 }
00185
00186 public void setErrorHandler(final ErrorHandler errorHandler) {
00187 this.errorHandler = errorHandler;
00188 }
00189
00190 public ErrorHandler getErrorHandler() {
00191 return errorHandler;
00192 }
00193
00194 public void setErrorPrefix(final String errorPrefix) {
00195 this.errorPrefix = errorPrefix;
00196 }
00197
00198 public String getErrorPrefix() {
00199 return errorPrefix;
00200 }
00201
00202 public void setErrorStream(final java.io.OutputStream errorStream) {
00203 this.errorStream = errorStream;
00204 }
00205
00206 public java.io.OutputStream getErrorStream() {
00207 return errorStream;
00208 }
00209
00210 public void setExclusiveCreate(final boolean exclusiveCreate) {
00211 this.exclusiveCreate = exclusiveCreate;
00212 }
00213
00214 public boolean getExclusiveCreate() {
00215 return exclusiveCreate;
00216 }
00217
00218 public void setFeedbackHandler(final FeedbackHandler feedbackHandler) {
00219 this.feedbackHandler = feedbackHandler;
00220 }
00221
00222 public FeedbackHandler getFeedbackHandler() {
00223 return feedbackHandler;
00224 }
00225
00226 public void setHashFillFactor(final int hashFillFactor) {
00227 this.hashFillFactor = hashFillFactor;
00228 }
00229
00230 public int getHashFillFactor() {
00231 return hashFillFactor;
00232 }
00233
00234 public void setHasher(final Hasher hasher) {
00235 this.hasher = hasher;
00236 }
00237
00238 public Hasher getHasher() {
00239 return hasher;
00240 }
00241
00242 public void setHashNumElements(final int hashNumElements) {
00243 this.hashNumElements = hashNumElements;
00244 }
00245
00246 public int getHashNumElements() {
00247 return hashNumElements;
00248 }
00249
00250 public void setMessageHandler(final MessageHandler messageHandler) {
00251 this.messageHandler = messageHandler;
00252 }
00253
00254 public MessageHandler getMessageHandler() {
00255 return messageHandler;
00256 }
00257
00258 public void setMessageStream(final java.io.OutputStream messageStream) {
00259 this.messageStream = messageStream;
00260 }
00261
00262 public java.io.OutputStream getMessageStream() {
00263 return messageStream;
00264 }
00265
00266 public void setMode(final int mode) {
00267 this.mode = mode;
00268 }
00269
00270 public long getMode() {
00271 return mode;
00272 }
00273
00274 public void setNoMMap(final boolean noMMap) {
00275 this.noMMap = noMMap;
00276 }
00277
00278 public boolean getNoMMap() {
00279 return noMMap;
00280 }
00281
00282 public void setPageSize(final int pageSize) {
00283 this.pageSize = pageSize;
00284 }
00285
00286 public int getPageSize() {
00287 return pageSize;
00288 }
00289
00290 public void setPanicHandler(final PanicHandler panicHandler) {
00291 this.panicHandler = panicHandler;
00292 }
00293
00294 public PanicHandler getPanicHandler() {
00295 return panicHandler;
00296 }
00297
00298 public void setQueueExtentSize(final int queueExtentSize) {
00299 this.queueExtentSize = queueExtentSize;
00300 }
00301
00302 public int getQueueExtentSize() {
00303 return queueExtentSize;
00304 }
00305
00306 public void setQueueInOrder(final boolean queueInOrder) {
00307 this.queueInOrder = queueInOrder;
00308 }
00309
00310 public boolean getQueueInOrder() {
00311 return queueInOrder;
00312 }
00313
00314 public void setReadOnly(final boolean readOnly) {
00315 this.readOnly = readOnly;
00316 }
00317
00318 public boolean getReadOnly() {
00319 return readOnly;
00320 }
00321
00322 public void setRecordNumberAppender(final RecordNumberAppender recnoAppender) {
00323 this.recnoAppender = recnoAppender;
00324 }
00325
00326 public RecordNumberAppender getRecordNumberAppender() {
00327 return recnoAppender;
00328 }
00329
00330 public void setRecordDelimiter(final int recordDelimiter) {
00331 this.recordDelimiter = recordDelimiter;
00332 }
00333
00334 public int getRecordDelimiter() {
00335 return recordDelimiter;
00336 }
00337
00338 public void setRecordLength(final int recordLength) {
00339 this.recordLength = recordLength;
00340 }
00341
00342 public int getRecordLength() {
00343 return recordLength;
00344 }
00345
00346 public void setBtreeRecordNumbers(final boolean btreeRecordNumbers) {
00347 this.btreeRecordNumbers = btreeRecordNumbers;
00348 }
00349
00350 public boolean getBtreeRecordNumbers() {
00351 return btreeRecordNumbers;
00352 }
00353
00354 public void setRecordPad(final int recordPad) {
00355 this.recordPad = recordPad;
00356 }
00357
00358 public int getRecordPad() {
00359 return recordPad;
00360 }
00361
00362 public void setRecordSource(final java.io.File recordSource) {
00363 this.recordSource = recordSource;
00364 }
00365
00366 public java.io.File getRecordSource() {
00367 return recordSource;
00368 }
00369
00370 public void setRenumbering(final boolean renumbering) {
00371 this.renumbering = renumbering;
00372 }
00373
00374 public boolean getRenumbering() {
00375 return renumbering;
00376 }
00377
00378 public void setReverseSplitOff(final boolean reverseSplitOff) {
00379 this.reverseSplitOff = reverseSplitOff;
00380 }
00381
00382 public boolean getReverseSplitOff() {
00383 return reverseSplitOff;
00384 }
00385
00386 public void setSortedDuplicates(final boolean sortedDuplicates) {
00387 this.sortedDuplicates = sortedDuplicates;
00388 }
00389
00390 public boolean getSortedDuplicates() {
00391 return sortedDuplicates;
00392 }
00393
00394 public void setUnsortedDuplicates(final boolean unsortedDuplicates) {
00395 this.unsortedDuplicates = unsortedDuplicates;
00396 }
00397
00398 public boolean getUnsortedDuplicates() {
00399 return unsortedDuplicates;
00400 }
00401
00402 public void setSnapshot(final boolean snapshot) {
00403 this.snapshot = snapshot;
00404 }
00405
00406 public boolean getSnapshot() {
00407 return snapshot;
00408 }
00409
00410 public boolean getTransactional() {
00411 return transactional;
00412 }
00413
00414 public void setTransactional(final boolean transactional) {
00415 this.transactional = transactional;
00416 }
00417
00418 public void setTransactionNotDurable(final boolean transactionNotDurable) {
00419 this.transactionNotDurable = transactionNotDurable;
00420 }
00421
00422 public boolean getTransactionNotDurable() {
00423 return transactionNotDurable;
00424 }
00425
00426 public void setTruncate(final boolean truncate) {
00427 this.truncate = truncate;
00428 }
00429
00430 public boolean getTruncate() {
00431 return truncate;
00432 }
00433
00434 public void setType(final DatabaseType type) {
00435 this.type = type;
00436 }
00437
00438 public DatabaseType getType() {
00439 return type;
00440 }
00441
00442 public void setXACreate(final boolean xaCreate) {
00443 this.xaCreate = xaCreate;
00444 }
00445
00446 public boolean getXACreate() {
00447 return xaCreate;
00448 }
00449
00450
00451 Db createDatabase(final DbEnv dbenv)
00452 throws DatabaseException {
00453
00454 int createFlags = 0;
00455
00456 createFlags |= xaCreate ? DbConstants.DB_XA_CREATE : 0;
00457 return new Db(dbenv, createFlags);
00458 }
00459
00460
00461 Db openDatabase(final DbEnv dbenv,
00462 final DbTxn txn,
00463 final String fileName,
00464 final String databaseName)
00465 throws DatabaseException, java.io.FileNotFoundException {
00466
00467 final Db db = createDatabase(dbenv);
00468
00469
00470 boolean threaded = (dbenv == null ||
00471 (dbenv.get_open_flags() & DbConstants.DB_THREAD) != 0);
00472
00473 int openFlags = 0;
00474 openFlags |= allowCreate ? DbConstants.DB_CREATE : 0;
00475 openFlags |= readUncommitted ? DbConstants.DB_READ_UNCOMMITTED : 0;
00476 openFlags |= exclusiveCreate ? DbConstants.DB_EXCL : 0;
00477 openFlags |= noMMap ? DbConstants.DB_NOMMAP : 0;
00478 openFlags |= readOnly ? DbConstants.DB_RDONLY : 0;
00479 openFlags |= threaded ? DbConstants.DB_THREAD : 0;
00480 openFlags |= truncate ? DbConstants.DB_TRUNCATE : 0;
00481
00482 if (transactional && txn == null)
00483 openFlags |= DbConstants.DB_AUTO_COMMIT;
00484
00485 boolean succeeded = false;
00486 try {
00487 configureDatabase(db, DEFAULT);
00488 db.open(txn, fileName, databaseName, type.getId(), openFlags, mode);
00489 succeeded = true;
00490 return db;
00491 } finally {
00492 if (!succeeded)
00493 try {
00494 db.close(0);
00495 } catch (Throwable t) {
00496
00497 }
00498 }
00499 }
00500
00501
00502 void configureDatabase(final Db db, final DatabaseConfig oldConfig)
00503 throws DatabaseException {
00504
00505 int dbFlags = 0;
00506 dbFlags |= checksum ? DbConstants.DB_CHKSUM : 0;
00507 dbFlags |= (password != null) ? DbConstants.DB_ENCRYPT : 0;
00508 dbFlags |= btreeRecordNumbers ? DbConstants.DB_RECNUM : 0;
00509 dbFlags |= queueInOrder ? DbConstants.DB_INORDER : 0;
00510 dbFlags |= renumbering ? DbConstants.DB_RENUMBER : 0;
00511 dbFlags |= reverseSplitOff ? DbConstants.DB_REVSPLITOFF : 0;
00512 dbFlags |= sortedDuplicates ? DbConstants.DB_DUPSORT : 0;
00513 dbFlags |= snapshot ? DbConstants.DB_SNAPSHOT : 0;
00514 dbFlags |= unsortedDuplicates ? DbConstants.DB_DUP : 0;
00515 dbFlags |= transactionNotDurable ? DbConstants.DB_TXN_NOT_DURABLE : 0;
00516
00517 if (dbFlags != 0)
00518 db.set_flags(dbFlags);
00519
00520 if (btMinKey != oldConfig.btMinKey)
00521 db.set_bt_minkey(btMinKey);
00522 if (byteOrder != oldConfig.byteOrder)
00523 db.set_lorder(byteOrder);
00524 if (cacheSize != oldConfig.cacheSize ||
00525 cacheCount != oldConfig.cacheCount)
00526 db.set_cachesize(cacheSize, cacheCount);
00527 if (errorStream != oldConfig.errorStream)
00528 db.set_error_stream(errorStream);
00529 if (errorPrefix != oldConfig.errorPrefix)
00530 db.set_errpfx(errorPrefix);
00531 if (hashFillFactor != oldConfig.hashFillFactor)
00532 db.set_h_ffactor(hashFillFactor);
00533 if (hashNumElements != oldConfig.hashNumElements)
00534 db.set_h_nelem(hashNumElements);
00535 if (messageStream != oldConfig.messageStream)
00536 db.set_message_stream(messageStream);
00537 if (pageSize != oldConfig.pageSize)
00538 db.set_pagesize(pageSize);
00539 if (password != oldConfig.password)
00540 db.set_encrypt(password, DbConstants.DB_ENCRYPT_AES);
00541 if (queueExtentSize != oldConfig.queueExtentSize)
00542 db.set_q_extentsize(queueExtentSize);
00543 if (recordDelimiter != oldConfig.recordDelimiter)
00544 db.set_re_delim(recordDelimiter);
00545 if (recordLength != oldConfig.recordLength)
00546 db.set_re_len(recordLength);
00547 if (recordPad != oldConfig.recordPad)
00548 db.set_re_pad(recordPad);
00549 if (recordSource != oldConfig.recordSource)
00550 db.set_re_source(
00551 (recordSource == null) ? null : recordSource.toString());
00552
00553 if (btreeComparator != oldConfig.btreeComparator)
00554 db.set_bt_compare(btreeComparator);
00555 if (btreePrefixCalculator != oldConfig.btreePrefixCalculator)
00556 db.set_bt_prefix(btreePrefixCalculator);
00557 if (duplicateComparator != oldConfig.duplicateComparator)
00558 db.set_dup_compare(duplicateComparator);
00559 if (feedbackHandler != oldConfig.feedbackHandler)
00560 db.set_feedback(feedbackHandler);
00561 if (errorHandler != oldConfig.errorHandler)
00562 db.set_errcall(errorHandler);
00563 if (hasher != oldConfig.hasher)
00564 db.set_h_hash(hasher);
00565 if (messageHandler != oldConfig.messageHandler)
00566 db.set_msgcall(messageHandler);
00567 if (recnoAppender != oldConfig.recnoAppender)
00568 db.set_append_recno(recnoAppender);
00569 if (panicHandler != oldConfig.panicHandler)
00570 db.set_paniccall(panicHandler);
00571 }
00572
00573
00574 DatabaseConfig(final Db db)
00575 throws DatabaseException {
00576
00577 type = DatabaseType.fromInt(db.get_type());
00578
00579 final int openFlags = db.get_open_flags();
00580 allowCreate = (openFlags & DbConstants.DB_CREATE) != 0;
00581 readUncommitted = (openFlags & DbConstants.DB_READ_UNCOMMITTED) != 0;
00582 exclusiveCreate = (openFlags & DbConstants.DB_EXCL) != 0;
00583 noMMap = (openFlags & DbConstants.DB_NOMMAP) != 0;
00584 readOnly = (openFlags & DbConstants.DB_RDONLY) != 0;
00585 truncate = (openFlags & DbConstants.DB_TRUNCATE) != 0;
00586
00587 final int dbFlags = db.get_flags();
00588 checksum = (dbFlags & DbConstants.DB_CHKSUM) != 0;
00589 btreeRecordNumbers = (dbFlags & DbConstants.DB_RECNUM) != 0;
00590 queueInOrder = (dbFlags & DbConstants.DB_INORDER) != 0;
00591 renumbering = (dbFlags & DbConstants.DB_RENUMBER) != 0;
00592 reverseSplitOff = (dbFlags & DbConstants.DB_REVSPLITOFF) != 0;
00593 sortedDuplicates = (dbFlags & DbConstants.DB_DUPSORT) != 0;
00594 snapshot = (dbFlags & DbConstants.DB_SNAPSHOT) != 0;
00595 unsortedDuplicates = (dbFlags & DbConstants.DB_DUP) != 0;
00596 transactionNotDurable = (dbFlags & DbConstants.DB_TXN_NOT_DURABLE) != 0;
00597
00598 if (type == DatabaseType.BTREE) {
00599 btMinKey = db.get_bt_minkey();
00600 }
00601 byteOrder = db.get_lorder();
00602
00603
00604 cacheSize = db.get_env().get_cachesize();
00605 cacheCount = db.get_env().get_cachesize_ncache();
00606 errorStream = db.get_error_stream();
00607 errorPrefix = db.get_errpfx();
00608 if (type == DatabaseType.HASH) {
00609 hashFillFactor = db.get_h_ffactor();
00610 hashNumElements = db.get_h_nelem();
00611 }
00612 messageStream = db.get_message_stream();
00613
00614 password = ((dbFlags & DbConstants.DB_ENCRYPT) != 0) ? "" : null;
00615 if (type == DatabaseType.QUEUE) {
00616 queueExtentSize = db.get_q_extentsize();
00617 }
00618 if (type == DatabaseType.QUEUE || type == DatabaseType.RECNO) {
00619 recordLength = db.get_re_len();
00620 recordPad = db.get_re_pad();
00621 }
00622 if (type == DatabaseType.RECNO) {
00623 recordDelimiter = db.get_re_delim();
00624 recordSource = (db.get_re_source() == null) ? null :
00625 new java.io.File(db.get_re_source());
00626 }
00627 transactional = db.get_transactional();
00628
00629 btreeComparator = db.get_bt_compare();
00630 btreePrefixCalculator = db.get_bt_prefix();
00631 duplicateComparator = db.get_dup_compare();
00632 feedbackHandler = db.get_feedback();
00633 errorHandler = db.get_errcall();
00634 hasher = db.get_h_hash();
00635 messageHandler = db.get_msgcall();
00636 recnoAppender = db.get_append_recno();
00637 panicHandler = db.get_paniccall();
00638 }
00639 }