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

Environment.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: Environment.java,v 12.7 2005/10/31 05:08:14 mjc Exp $
00008  */
00009 
00010 package com.sleepycat.db;
00011 
00012 import com.sleepycat.db.internal.DbConstants;
00013 import com.sleepycat.db.internal.DbEnv;
00014 
00015 public class Environment {
00016     private DbEnv dbenv;
00017     private int autoCommitFlag;
00018 
00019     /* package */
00020     Environment(final DbEnv dbenv)
00021         throws DatabaseException {
00022 
00023         this.dbenv = dbenv;
00024         dbenv.wrapper = this;
00025     }
00026 
00027     public Environment(final java.io.File home, EnvironmentConfig config)
00028         throws DatabaseException, java.io.FileNotFoundException {
00029 
00030         this(EnvironmentConfig.checkNull(config).openEnvironment(home));
00031         this.autoCommitFlag =
00032             ((dbenv.get_open_flags() & DbConstants.DB_INIT_TXN) == 0) ? 0 :
00033                 DbConstants.DB_AUTO_COMMIT;
00034     }
00035 
00036     public void close()
00037         throws DatabaseException {
00038 
00039         dbenv.close(0);
00040     }
00041 
00042     /* package */
00043     DbEnv unwrap() {
00044         return dbenv;
00045     }
00046 
00047     public static void remove(final java.io.File home,
00048                               final boolean force,
00049                               EnvironmentConfig config)
00050         throws DatabaseException, java.io.FileNotFoundException {
00051 
00052         config = EnvironmentConfig.checkNull(config);
00053         int flags = force ? DbConstants.DB_FORCE : 0;
00054         flags |= config.getUseEnvironment() ?
00055             DbConstants.DB_USE_ENVIRON : 0;
00056         flags |= config.getUseEnvironmentRoot() ?
00057             DbConstants.DB_USE_ENVIRON_ROOT : 0;
00058         final DbEnv dbenv = config.createEnvironment();
00059         dbenv.remove((home == null) ? null : home.toString(), flags);
00060     }
00061 
00062     public void setConfig(final EnvironmentConfig config)
00063         throws DatabaseException {
00064 
00065         config.configureEnvironment(dbenv, new EnvironmentConfig(dbenv));
00066     }
00067 
00068     public EnvironmentConfig getConfig()
00069         throws DatabaseException {
00070 
00071         return new EnvironmentConfig(dbenv);
00072     }
00073 
00074     /* Manage databases. */
00075     public Database openDatabase(final Transaction txn,
00076                                  final String fileName,
00077                                  final String databaseName,
00078                                  DatabaseConfig config)
00079         throws DatabaseException, java.io.FileNotFoundException {
00080 
00081         return new Database(
00082             DatabaseConfig.checkNull(config).openDatabase(dbenv,
00083                 (txn == null) ? null : txn.txn,
00084                 fileName, databaseName));
00085     }
00086 
00087     public SecondaryDatabase openSecondaryDatabase(
00088             final Transaction txn,
00089             final String fileName,
00090             final String databaseName,
00091             final Database primaryDatabase,
00092             SecondaryConfig config)
00093         throws DatabaseException, java.io.FileNotFoundException {
00094 
00095         return new SecondaryDatabase(
00096             SecondaryConfig.checkNull(config).openSecondaryDatabase(
00097                 dbenv, (txn == null) ? null : txn.txn,
00098                 fileName, databaseName, primaryDatabase.db),
00099             primaryDatabase);
00100     }
00101 
00102     public void removeDatabase(final Transaction txn,
00103                                final String fileName,
00104                                final String databaseName)
00105         throws DatabaseException, java.io.FileNotFoundException {
00106 
00107         dbenv.dbremove((txn == null) ? null : txn.txn,
00108             fileName, databaseName,
00109             (txn == null) ? autoCommitFlag : 0);
00110     }
00111 
00112     public void renameDatabase(final Transaction txn,
00113                                final String fileName,
00114                                final String databaseName,
00115                                final String newName)
00116         throws DatabaseException, java.io.FileNotFoundException {
00117 
00118         dbenv.dbrename((txn == null) ? null : txn.txn,
00119             fileName, databaseName, newName,
00120             (txn == null) ? autoCommitFlag : 0);
00121     }
00122 
00123     public java.io.File getHome()
00124         throws DatabaseException {
00125 
00126         String home = dbenv.get_home();
00127         return (home == null) ? null : new java.io.File(home);
00128     }
00129 
00130     /* Cache management. */
00131     public int trickleCacheWrite(int percent)
00132         throws DatabaseException {
00133 
00134         return dbenv.memp_trickle(percent);
00135     }
00136 
00137     /* Locking */
00138     public int detectDeadlocks(LockDetectMode mode)
00139         throws DatabaseException {
00140 
00141         return dbenv.lock_detect(0, mode.getFlag());
00142     }
00143 
00144     public Lock getLock(int locker,
00145                         boolean noWait,
00146                         DatabaseEntry object,
00147                         LockRequestMode mode)
00148         throws DatabaseException {
00149 
00150         return Lock.wrap(
00151             dbenv.lock_get(locker, noWait ? DbConstants.DB_LOCK_NOWAIT : 0,
00152                 object, mode.getFlag()));
00153     }
00154 
00155     public void putLock(Lock lock)
00156         throws DatabaseException {
00157 
00158         dbenv.lock_put(lock.unwrap());
00159     }
00160 
00161     public int createLockerID()
00162         throws DatabaseException {
00163 
00164         return dbenv.lock_id();
00165     }
00166 
00167     public void freeLockerID(int id)
00168         throws DatabaseException {
00169 
00170         dbenv.lock_id_free(id);
00171     }
00172 
00173     public void lockVector(int locker, boolean noWait, LockRequest[] list)
00174         throws DatabaseException {
00175 
00176         dbenv.lock_vec(locker, noWait ? DbConstants.DB_LOCK_NOWAIT : 0,
00177             list, 0, list.length);
00178     }
00179 
00180     /* Logging */
00181     public LogCursor openLogCursor()
00182         throws DatabaseException {
00183 
00184         return LogCursor.wrap(dbenv.log_cursor(0));
00185     }
00186 
00187     public String getLogFileName(LogSequenceNumber lsn)
00188         throws DatabaseException {
00189 
00190         return dbenv.log_file(lsn);
00191     }
00192 
00193     /* Replication support */
00194     public void startReplication(DatabaseEntry cdata, boolean master)
00195         throws DatabaseException {
00196 
00197         dbenv.rep_start(cdata,
00198             master ? DbConstants.DB_REP_MASTER : DbConstants.DB_REP_CLIENT);
00199     }
00200 
00201     public int electReplicationMaster(int nsites,
00202                                       int nvotes,
00203                                       int priority,
00204                                       int timeout)
00205         throws DatabaseException {
00206 
00207         return dbenv.rep_elect(nsites, nvotes, priority, timeout,
00208             0 /* unused flags */);
00209     }
00210 
00211     public void flushReplication()
00212         throws DatabaseException {
00213 
00214         dbenv.rep_flush();
00215     }
00216 
00217     public ReplicationStatus processReplicationMessage(DatabaseEntry control,
00218                                                        DatabaseEntry rec,
00219                                                        int envid)
00220         throws DatabaseException {
00221 
00222         final DbEnv.RepProcessMessage wrappedID = new DbEnv.RepProcessMessage();
00223         wrappedID.envid = envid;
00224         // Create a new entry so that rec isn't overwritten
00225         final DatabaseEntry cdata =
00226             new DatabaseEntry(rec.getData(), rec.getOffset(), rec.getSize());
00227         final LogSequenceNumber lsn = new LogSequenceNumber();
00228         final int ret =
00229             dbenv.rep_process_message(control, cdata, wrappedID, lsn);
00230         return ReplicationStatus.getStatus(ret, cdata, wrappedID.envid, lsn);
00231     }
00232 
00233     public void setReplicationConfig(ReplicationConfig config, boolean onoff)
00234         throws DatabaseException {
00235 
00236         dbenv.rep_set_config(config.getFlag(), onoff);
00237     }
00238 
00239     public boolean getReplicationConfig(ReplicationConfig config)
00240         throws DatabaseException {
00241 
00242         return dbenv.rep_get_config(config.getFlag());
00243     }
00244 
00245     public void syncReplication() throws DatabaseException {
00246         dbenv.rep_sync(0);
00247     }
00248 
00249     /* Statistics */
00250     public CacheStats getCacheStats(StatsConfig config)
00251         throws DatabaseException {
00252 
00253         return dbenv.memp_stat(StatsConfig.checkNull(config).getFlags());
00254     }
00255 
00256     public CacheFileStats[] getCacheFileStats(StatsConfig config)
00257         throws DatabaseException {
00258 
00259         return dbenv.memp_fstat(StatsConfig.checkNull(config).getFlags());
00260     }
00261 
00262     public LogStats getLogStats(StatsConfig config)
00263         throws DatabaseException {
00264 
00265         return dbenv.log_stat(StatsConfig.checkNull(config).getFlags());
00266     }
00267 
00268     public ReplicationStats getReplicationStats(StatsConfig config)
00269         throws DatabaseException {
00270 
00271         return dbenv.rep_stat(StatsConfig.checkNull(config).getFlags());
00272     }
00273 
00274     public LockStats getLockStats(StatsConfig config)
00275         throws DatabaseException {
00276 
00277         return dbenv.lock_stat(StatsConfig.checkNull(config).getFlags());
00278     }
00279 
00280     public MutexStats getMutexStats(StatsConfig config)
00281         throws DatabaseException {
00282 
00283         return dbenv.mutex_stat(StatsConfig.checkNull(config).getFlags());
00284     }
00285 
00286     public TransactionStats getTransactionStats(StatsConfig config)
00287         throws DatabaseException {
00288 
00289         return dbenv.txn_stat(StatsConfig.checkNull(config).getFlags());
00290     }
00291 
00292     /* Transaction management */
00293     public Transaction beginTransaction(final Transaction parent,
00294                                         TransactionConfig config)
00295         throws DatabaseException {
00296 
00297         return new Transaction(
00298             TransactionConfig.checkNull(config).beginTransaction(dbenv,
00299                 (parent == null) ? null : parent.txn));
00300     }
00301 
00302     public void checkpoint(CheckpointConfig config)
00303         throws DatabaseException {
00304 
00305         CheckpointConfig.checkNull(config).runCheckpoint(dbenv);
00306     }
00307 
00308     public void logFlush(LogSequenceNumber lsn)
00309         throws DatabaseException {
00310 
00311         dbenv.log_flush(lsn);
00312     }
00313 
00314     public LogSequenceNumber logPut(DatabaseEntry data, boolean flush)
00315         throws DatabaseException {
00316 
00317         final LogSequenceNumber lsn = new LogSequenceNumber();
00318         dbenv.log_put(lsn, data, flush ? DbConstants.DB_FLUSH : 0);
00319         return lsn;
00320     }
00321 
00322     public void logPrint(Transaction txn, String message)
00323         throws DatabaseException {
00324 
00325         dbenv.log_print((txn == null) ? null : txn.txn, message);
00326     }
00327 
00328     public java.io.File[] getArchiveLogFiles(boolean includeInUse)
00329         throws DatabaseException {
00330 
00331         final String[] logNames = dbenv.log_archive(DbConstants.DB_ARCH_ABS |
00332                 (includeInUse ? DbConstants.DB_ARCH_LOG : 0));
00333         final int len = (logNames == null) ? 0 : logNames.length;
00334         final java.io.File[] logFiles = new java.io.File[len];
00335         for (int i = 0; i < len; i++)
00336             logFiles[i] = new java.io.File(logNames[i]);
00337         return logFiles;
00338     }
00339 
00340     public java.io.File[] getArchiveDatabases()
00341         throws DatabaseException {
00342 
00343         final String home = dbenv.get_home();
00344         final String[] dbNames = dbenv.log_archive(DbConstants.DB_ARCH_DATA);
00345         final int len = (dbNames == null) ? 0 : dbNames.length;
00346         final java.io.File[] dbFiles = new java.io.File[len];
00347         for (int i = 0; i < len; i++)
00348             dbFiles[i] = new java.io.File(home, dbNames[i]);
00349         return dbFiles;
00350     }
00351 
00352     public void removeOldLogFiles()
00353         throws DatabaseException {
00354 
00355         dbenv.log_archive(DbConstants.DB_ARCH_REMOVE);
00356     }
00357 
00358     public PreparedTransaction[] recover(final int count,
00359                                          final boolean continued)
00360         throws DatabaseException {
00361 
00362         return dbenv.txn_recover(count,
00363             continued ? DbConstants.DB_NEXT : DbConstants.DB_FIRST);
00364     }
00365 
00366     /* Panic the environment, or stop a panic. */
00367     public void panic(boolean onoff)
00368         throws DatabaseException {
00369 
00370         dbenv.set_flags(DbConstants.DB_PANIC_ENVIRONMENT, onoff);
00371     }
00372 
00373     /* Version information */
00374     public static String getVersionString() {
00375         return DbEnv.get_version_string();
00376     }
00377 
00378     public static int getVersionMajor() {
00379         return DbEnv.get_version_major();
00380     }
00381 
00382     public static int getVersionMinor() {
00383         return DbEnv.get_version_minor();
00384     }
00385 
00386     public static int getVersionPatch() {
00387         return DbEnv.get_version_patch();
00388     }
00389 }

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