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

Database.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: Database.java,v 12.2 2005/10/19 04:27:44 mjc 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.Dbc;
00016 
00017 public class Database {
00018     Db db;
00019     private int autoCommitFlag;
00020     int rmwFlag;
00021 
00022     /* package */
00023     Database(final Db db)
00024         throws DatabaseException {
00025 
00026         this.db = db;
00027         db.wrapper = this;
00028         this.autoCommitFlag =
00029             db.get_transactional() ? DbConstants.DB_AUTO_COMMIT : 0;
00030         rmwFlag = ((db.get_env().get_open_flags() &
00031                     DbConstants.DB_INIT_LOCK) != 0) ? DbConstants.DB_RMW : 0;
00032     }
00033 
00034     public Database(final String filename,
00035                     final String databaseName,
00036                     final DatabaseConfig config)
00037         throws DatabaseException, java.io.FileNotFoundException {
00038 
00039         this(DatabaseConfig.checkNull(config).openDatabase(null, null,
00040             filename, databaseName));
00041         // Set up dbenv.wrapper
00042         new Environment(db.get_env());
00043     }
00044 
00045     public void close(final boolean noSync)
00046         throws DatabaseException {
00047 
00048         db.close(noSync ? DbConstants.DB_NOSYNC : 0);
00049     }
00050 
00051     public void close()
00052         throws DatabaseException {
00053 
00054         close(false);
00055     }
00056 
00057     public CompactStats compact(final Transaction txn,
00058                                 final DatabaseEntry start,
00059                                 final DatabaseEntry stop,
00060                                 final DatabaseEntry end,
00061                                 CompactConfig config)
00062         throws DatabaseException {
00063 
00064         config = CompactConfig.checkNull(config);
00065         CompactStats compact = new CompactStats(config.getFillPercent(),
00066             config.getTimeout(), config.getMaxPages());
00067         db.compact((txn == null) ? null : txn.txn,
00068             start, stop, compact, config.getFlags(), end);
00069         return compact;
00070     }
00071 
00072     public Cursor openCursor(final Transaction txn, CursorConfig config)
00073         throws DatabaseException {
00074 
00075         return new Cursor(this, CursorConfig.checkNull(config).openCursor(
00076             db, (txn == null) ? null : txn.txn), config);
00077     }
00078 
00079     public Sequence openSequence(final Transaction txn,
00080                                  final DatabaseEntry key,
00081                                  final SequenceConfig config)
00082         throws DatabaseException {
00083 
00084         return new Sequence(SequenceConfig.checkNull(config).openSequence(
00085             db, (txn == null) ? null : txn.txn, key), config);
00086     }
00087 
00088     public void removeSequence(final Transaction txn,
00089                                final DatabaseEntry key,
00090                                SequenceConfig config)
00091         throws DatabaseException {
00092 
00093         config = SequenceConfig.checkNull(config);
00094         final DbSequence seq = config.openSequence(
00095             db, (txn == null) ? null : txn.txn, key);
00096         seq.remove((txn == null) ? null : txn.txn,
00097             (txn == null && db.get_transactional()) ?
00098             DbConstants.DB_AUTO_COMMIT | (config.getAutoCommitNoSync() ?
00099             DbConstants.DB_TXN_NOSYNC : 0) : 0);
00100     }
00101 
00102     public String getDatabaseFile()
00103         throws DatabaseException {
00104 
00105         return db.get_filename();
00106     }
00107 
00108     public String getDatabaseName()
00109         throws DatabaseException {
00110 
00111         return db.get_dbname();
00112     }
00113 
00114     public DatabaseConfig getConfig()
00115         throws DatabaseException {
00116 
00117         return new DatabaseConfig(db);
00118     }
00119 
00120     public void setConfig(DatabaseConfig config)
00121         throws DatabaseException {
00122 
00123         config.configureDatabase(db, getConfig());
00124     }
00125 
00126     public Environment getEnvironment()
00127         throws DatabaseException {
00128 
00129         return db.get_env().wrapper;
00130     }
00131 
00132     public CacheFile getCacheFile()
00133         throws DatabaseException {
00134 
00135         return new CacheFile(db.get_mpf());
00136     }
00137 
00138     public OperationStatus append(final Transaction txn,
00139                                   final DatabaseEntry key,
00140                                   final DatabaseEntry data)
00141         throws DatabaseException {
00142 
00143         return OperationStatus.fromInt(
00144             db.put((txn == null) ? null : txn.txn, key, data,
00145                 DbConstants.DB_APPEND | ((txn == null) ? autoCommitFlag : 0)));
00146     }
00147 
00148     public OperationStatus consume(final Transaction txn,
00149                                    final DatabaseEntry key,
00150                                    final DatabaseEntry data,
00151                                    final boolean wait)
00152         throws DatabaseException {
00153 
00154         return OperationStatus.fromInt(
00155             db.get((txn == null) ? null : txn.txn,
00156                 key, data,
00157                 (wait ? DbConstants.DB_CONSUME_WAIT : DbConstants.DB_CONSUME) |
00158                 ((txn == null) ? autoCommitFlag : 0)));
00159     }
00160 
00161     public OperationStatus delete(final Transaction txn,
00162                                   final DatabaseEntry key)
00163         throws DatabaseException {
00164 
00165         return OperationStatus.fromInt(
00166             db.del((txn == null) ? null : txn.txn, key,
00167                 ((txn == null) ? autoCommitFlag : 0)));
00168     }
00169 
00170     public OperationStatus get(final Transaction txn,
00171                                final DatabaseEntry key,
00172                                final DatabaseEntry data,
00173                                final LockMode lockMode)
00174         throws DatabaseException {
00175 
00176         return OperationStatus.fromInt(
00177             db.get((txn == null) ? null : txn.txn,
00178                 key, data,
00179                 LockMode.getFlag(lockMode) |
00180                 ((data == null) ? 0 : data.getMultiFlag())));
00181     }
00182 
00183     public KeyRange getKeyRange(final Transaction txn,
00184                                 final DatabaseEntry key)
00185         throws DatabaseException {
00186 
00187         final KeyRange range = new KeyRange();
00188         db.key_range((txn == null) ? null : txn.txn, key, range, 0);
00189         return range;
00190     }
00191 
00192     public OperationStatus getSearchBoth(final Transaction txn,
00193                                          final DatabaseEntry key,
00194                                          final DatabaseEntry data,
00195                                          final LockMode lockMode)
00196         throws DatabaseException {
00197 
00198         return OperationStatus.fromInt(
00199             db.get((txn == null) ? null : txn.txn,
00200                 key, data,
00201                 DbConstants.DB_GET_BOTH |
00202                 LockMode.getFlag(lockMode) |
00203                 ((data == null) ? 0 : data.getMultiFlag())));
00204     }
00205 
00206     public OperationStatus getSearchRecordNumber(final Transaction txn,
00207                                            final DatabaseEntry key,
00208                                            final DatabaseEntry data,
00209                                            final LockMode lockMode)
00210         throws DatabaseException {
00211 
00212         return OperationStatus.fromInt(
00213             db.get((txn == null) ? null : txn.txn,
00214                 key, data,
00215                 DbConstants.DB_SET_RECNO |
00216                 LockMode.getFlag(lockMode) |
00217                 ((data == null) ? 0 : data.getMultiFlag())));
00218     }
00219 
00220     public OperationStatus put(final Transaction txn,
00221                                final DatabaseEntry key,
00222                                final DatabaseEntry data)
00223         throws DatabaseException {
00224 
00225         return OperationStatus.fromInt(
00226             db.put((txn == null) ? null : txn.txn,
00227                 key, data,
00228                 ((txn == null) ? autoCommitFlag : 0)));
00229     }
00230 
00231     public OperationStatus putNoDupData(final Transaction txn,
00232                                         final DatabaseEntry key,
00233                                         final DatabaseEntry data)
00234         throws DatabaseException {
00235 
00236         return OperationStatus.fromInt(
00237             db.put((txn == null) ? null : txn.txn,
00238                 key, data,
00239                 DbConstants.DB_NODUPDATA |
00240                 ((txn == null) ? autoCommitFlag : 0)));
00241     }
00242 
00243     public OperationStatus putNoOverwrite(final Transaction txn,
00244                                           final DatabaseEntry key,
00245                                           final DatabaseEntry data)
00246         throws DatabaseException {
00247 
00248         return OperationStatus.fromInt(
00249             db.put((txn == null) ? null : txn.txn,
00250                 key, data,
00251                 DbConstants.DB_NOOVERWRITE |
00252                 ((txn == null) ? autoCommitFlag : 0)));
00253     }
00254 
00255     public JoinCursor join(final Cursor[] cursList, JoinConfig config)
00256         throws DatabaseException {
00257 
00258         config = JoinConfig.checkNull(config);
00259 
00260         final Dbc[] dbcList = new Dbc[cursList.length];
00261         for (int i = 0; i < cursList.length; i++)
00262             dbcList[i] = (cursList[i] == null) ? null : cursList[i].dbc;
00263 
00264         return new JoinCursor(this,
00265             db.join(dbcList, config.getFlags()), config);
00266     }
00267 
00268     public int truncate(final Transaction txn, boolean countRecords)
00269         throws DatabaseException {
00270 
00271         // XXX: implement countRecords in C
00272         int count = db.truncate((txn == null) ? null : txn.txn,
00273             ((txn == null) ? autoCommitFlag : 0));
00274 
00275         return countRecords ? count : -1;
00276     }
00277 
00278     public DatabaseStats getStats(final Transaction txn, StatsConfig config)
00279         throws DatabaseException {
00280 
00281         return (DatabaseStats)db.stat((txn == null) ? null : txn.txn,
00282             StatsConfig.checkNull(config).getFlags());
00283     }
00284 
00285     public static void remove(final String fileName,
00286                               final String databaseName,
00287                               DatabaseConfig config)
00288         throws DatabaseException, java.io.FileNotFoundException {
00289 
00290         final Db db = DatabaseConfig.checkNull(config).createDatabase(null);
00291         db.remove(fileName, databaseName, 0);
00292     }
00293 
00294     public static void rename(final String fileName,
00295                               final String oldDatabaseName,
00296                               final String newDatabaseName,
00297                               DatabaseConfig config)
00298         throws DatabaseException, java.io.FileNotFoundException {
00299 
00300         final Db db = DatabaseConfig.checkNull(config).createDatabase(null);
00301         db.rename(fileName, oldDatabaseName, newDatabaseName, 0);
00302     }
00303 
00304     public void sync()
00305         throws DatabaseException {
00306 
00307         db.sync(0);
00308     }
00309 
00310     public static void upgrade(final String fileName,
00311                         DatabaseConfig config)
00312         throws DatabaseException, java.io.FileNotFoundException {
00313 
00314         final Db db = DatabaseConfig.checkNull(config).createDatabase(null);
00315         db.upgrade(fileName,
00316             config.getSortedDuplicates() ? DbConstants.DB_DUPSORT : 0);
00317         db.close(0);
00318     }
00319 
00320     public boolean verify(final String fileName,
00321                        final String databaseName,
00322                        final java.io.PrintStream dumpStream,
00323                        VerifyConfig config)
00324         throws DatabaseException, java.io.FileNotFoundException {
00325 
00326         return db.verify(fileName, databaseName, dumpStream,
00327             VerifyConfig.checkNull(config).getFlags());
00328     }
00329 }

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