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

SequenceConfig.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: SequenceConfig.java,v 12.1 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.DbSequence;
00015 import com.sleepycat.db.internal.DbTxn;
00016 
00017 public class SequenceConfig implements Cloneable {
00018     /*
00019      * For internal use, final to allow null as a valid value for
00020      * the config parameter.
00021      */
00022     public static final SequenceConfig DEFAULT = new SequenceConfig();
00023 
00024     /* package */
00025     static SequenceConfig checkNull(SequenceConfig config) {
00026         return (config == null) ? DEFAULT : config;
00027     }
00028 
00029     /* Parameters */
00030     private int cacheSize = 0;
00031     private long rangeMin = Long.MIN_VALUE;
00032     private long rangeMax = Long.MAX_VALUE;
00033     private long initialValue = 0L;
00034 
00035     /* Flags */
00036     private boolean allowCreate = false;
00037     private boolean decrement = false;
00038     private boolean exclusiveCreate = false;
00039     private boolean autoCommitNoSync = false;
00040     private boolean wrap = false;
00041 
00042     public SequenceConfig() {
00043     }
00044 
00045     public void setAllowCreate(final boolean allowCreate) {
00046         this.allowCreate = allowCreate;
00047     }
00048 
00049     public boolean getAllowCreate() {
00050         return allowCreate;
00051     }
00052 
00053     public void setCacheSize(final int cacheSize) {
00054         this.cacheSize = cacheSize;
00055     }
00056 
00057     public int getCacheSize() {
00058         return cacheSize;
00059     }
00060 
00061     public void setDecrement(boolean decrement) {
00062         this.decrement = decrement;
00063     }
00064 
00065     public boolean getDecrement() {
00066          return decrement;
00067     }
00068 
00069     public void setExclusiveCreate(final boolean exclusiveCreate) {
00070         this.exclusiveCreate = exclusiveCreate;
00071     }
00072 
00073     public boolean getExclusiveCreate() {
00074         return exclusiveCreate;
00075     }
00076 
00077     public void setInitialValue(long initialValue) {
00078         this.initialValue = initialValue;
00079     }
00080 
00081     public long getInitialValue() {
00082         return initialValue;
00083     }
00084 
00085     public void setAutoCommitNoSync(final boolean autoCommitNoSync) {
00086         this.autoCommitNoSync = autoCommitNoSync;
00087     }
00088 
00089     public boolean getAutoCommitNoSync() {
00090         return autoCommitNoSync;
00091     }
00092 
00093     public void setRange(final long min, final long max) {
00094         this.rangeMin = min;
00095         this.rangeMax = max;
00096     }
00097 
00098     public long getRangeMin() {
00099         return rangeMin;
00100     }
00101 
00102     public long getRangeMax() {
00103         return rangeMax;
00104     }
00105 
00106     public void setWrap(final boolean wrap) {
00107         this.wrap = wrap;
00108     }
00109 
00110     public boolean getWrap() {
00111         return wrap;
00112     }
00113 
00114     /* package */
00115     DbSequence createSequence(final Db db)
00116         throws DatabaseException {
00117 
00118         int createFlags = 0;
00119 
00120         return new DbSequence(db, createFlags);
00121     }
00122 
00123     /* package */
00124     DbSequence openSequence(final Db db,
00125                     final DbTxn txn,
00126                     final DatabaseEntry key)
00127         throws DatabaseException {
00128 
00129         final DbSequence seq = createSequence(db);
00130         // The DB_THREAD flag is inherited from the database
00131         boolean threaded = ((db.get_open_flags() & DbConstants.DB_THREAD) != 0);
00132 
00133         int openFlags = 0;
00134         openFlags |= allowCreate ? DbConstants.DB_CREATE : 0;
00135         openFlags |= exclusiveCreate ? DbConstants.DB_EXCL : 0;
00136         openFlags |= threaded ? DbConstants.DB_THREAD : 0;
00137 
00138         if (db.get_transactional() && txn == null)
00139             openFlags |= DbConstants.DB_AUTO_COMMIT;
00140 
00141         configureSequence(seq, DEFAULT);
00142         boolean succeeded = false;
00143         try {
00144             seq.open(txn, key, openFlags);
00145             succeeded = true;
00146             return seq;
00147         } finally {
00148             if (!succeeded)
00149                 try {
00150                     seq.close(0);
00151                 } catch (Throwable t) {
00152                     // Ignore it -- an exception is already in flight.
00153                 }
00154         }
00155     }
00156 
00157     /* package */
00158     void configureSequence(final DbSequence seq, final SequenceConfig oldConfig)
00159         throws DatabaseException {
00160 
00161         int seqFlags = 0;
00162         seqFlags |= decrement ? DbConstants.DB_SEQ_DEC : DbConstants.DB_SEQ_INC;
00163         seqFlags |= wrap ? DbConstants.DB_SEQ_WRAP : 0;
00164 
00165         if (seqFlags != 0)
00166             seq.set_flags(seqFlags);
00167 
00168         if (rangeMin != oldConfig.rangeMin || rangeMax != oldConfig.rangeMax)
00169             seq.set_range(rangeMin, rangeMax);
00170 
00171         if (initialValue != oldConfig.initialValue)
00172             seq.initial_value(initialValue);
00173 
00174         if (cacheSize != oldConfig.cacheSize)
00175             seq.set_cachesize(cacheSize);
00176     }
00177 
00178     /* package */
00179     SequenceConfig(final DbSequence seq)
00180         throws DatabaseException {
00181 
00182         // XXX: can't get open flags
00183         final int openFlags = 0;
00184         allowCreate = (openFlags & DbConstants.DB_CREATE) != 0;
00185         exclusiveCreate = (openFlags & DbConstants.DB_EXCL) != 0;
00186 
00187         final int seqFlags = seq.get_flags();
00188         decrement = (seqFlags & DbConstants.DB_SEQ_DEC) != 0;
00189         wrap = (seqFlags & DbConstants.DB_SEQ_WRAP) != 0;
00190 
00191         // XXX: can't get initial value
00192         final long initialValue = 0;
00193 
00194         cacheSize = seq.get_cachesize();
00195 
00196         rangeMin = seq.get_range_min();
00197         rangeMax = seq.get_range_max();
00198     }
00199 }

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