Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SecondaryConfig.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: SecondaryConfig.java,v 12.2 2005/06/16 20:23:03 bostic Exp $
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 
00017 public class SecondaryConfig extends DatabaseConfig implements Cloneable {
00018     /*
00019      * For internal use, to allow null as a valid value for
00020      * the config parameter.
00021      */
00022     public static final SecondaryConfig DEFAULT = new SecondaryConfig();
00023 
00024     /* package */
00025     static SecondaryConfig checkNull(SecondaryConfig config) {
00026         return (config == null) ? DEFAULT : config;
00027     }
00028 
00029     private boolean allowPopulate;
00030     private boolean immutableSecondaryKey;
00031     private SecondaryKeyCreator keyCreator;
00032 
00033     public SecondaryConfig() {
00034     }
00035 
00036     public void setKeyCreator(final SecondaryKeyCreator keyCreator) {
00037         this.keyCreator = keyCreator;
00038     }
00039 
00040     public SecondaryKeyCreator getKeyCreator() {
00041         return keyCreator;
00042     }
00043 
00044     public void setAllowPopulate(final boolean allowPopulate) {
00045         this.allowPopulate = allowPopulate;
00046     }
00047 
00048     public boolean getAllowPopulate() {
00049         return allowPopulate;
00050     }
00051 
00052     public void setImmutableSecondaryKey(final boolean immutableSecondaryKey) {
00053         this.immutableSecondaryKey = immutableSecondaryKey;
00054     }
00055 
00056     public boolean getImmutableSecondaryKey() {
00057         return immutableSecondaryKey;
00058     }
00059 
00060     /* package */
00061     Db openSecondaryDatabase(final DbEnv dbenv,
00062                              final DbTxn txn,
00063                              final String fileName,
00064                              final String databaseName,
00065                              final Db primary)
00066         throws DatabaseException, java.io.FileNotFoundException {
00067 
00068         int associateFlags = 0;
00069         associateFlags |= allowPopulate ? DbConstants.DB_CREATE : 0;
00070         if (getTransactional() && txn == null)
00071             associateFlags |= DbConstants.DB_AUTO_COMMIT;
00072         if (immutableSecondaryKey)
00073             associateFlags |= DbConstants.DB_IMMUTABLE_KEY;
00074 
00075         final Db db = super.openDatabase(dbenv, txn, fileName, databaseName);
00076         boolean succeeded = false;
00077         try {
00078             primary.associate(txn, db, keyCreator, associateFlags);
00079             succeeded = true;
00080             return db;
00081         } finally {
00082             if (!succeeded)
00083                 try {
00084                     db.close(0);
00085                 } catch (Throwable t) {
00086                     // Ignore it -- there is already an exception in flight.
00087                 }
00088         }
00089     }
00090 
00091     /* package */
00092     SecondaryConfig(final Db db)
00093         throws DatabaseException {
00094 
00095         super(db);
00096 
00097         // XXX: There is no way to find out whether allowPopulate was set.
00098         allowPopulate = false;
00099         keyCreator = db.get_seckey_create();
00100     }
00101 }
00102 

Generated on Sun Dec 25 12:14:33 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2