/* * Copyright 2006-2012 Amazon Technologies, Inc. or its affiliates. * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks * of Amazon Technologies, Inc. or its affiliates. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.amazon.carbonado.repo.sleepycat; import java.lang.reflect.Constructor; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import org.cojen.util.ThrowUnchecked; import com.amazon.carbonado.Repository; import com.amazon.carbonado.RepositoryException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.repo.indexed.IndexedRepositoryBuilder; import com.amazon.carbonado.raw.CompressionType; import com.amazon.carbonado.raw.CompressedStorableCodecFactory; import com.amazon.carbonado.raw.StorableCodecFactory; import com.amazon.carbonado.spi.AbstractRepositoryBuilder; import com.amazon.carbonado.ConfigurationException; /** * Builder and configuration options for BDBRepository. * *
* BDBRepositoryBuilder builder = new BDBRepositoryBuilder(); * * builder.setProduct("JE"); * builder.setName("test"); * builder.setEnvironmentHome("/tmp/testRepo"); * builder.setTransactionWriteNoSync(true); * * Repository repo = builder.build(); ** *
* The following extra capabilities are supported: *
Note: When setting this option, the storable codec factory must also
* be changed, since the default storable codec factory is unable to
* distinguish storable types that reside in a single database file. Call
* setFileName instead to use built-in BDB feature for supporting multiple
* databases in one file.
*/
public void setSingleFileName(String filename) {
mSingleFileName = filename;
mFileNames = null;
}
/**
* Returns the single file that all BDB databases should reside in.
*/
public String getSingleFileName() {
return mSingleFileName;
}
/**
* Specify the file that a BDB database should reside in, except for log
* files and caches. The filename is relative to the environment home,
* unless data directories have been specified. For BDBRepositories that
* are log files only, this configuration is ignored.
*
* @param filename BDB database filename
* @param typeName type to store in file; if null, the file is used by default
* for all types
*/
public void setFileName(String filename, String typeName) {
mSingleFileName = null;
if (mFileNames == null) {
mFileNames = new HashMap When index repair is disabled, the Storable is immediately
* available. This does have consequences, however. The set of indexes
* available for queries is defined by the intersection of the old
* and new index sets. The set of indexes that are kept up-to-date is
* defined by the union of the old and new index sets.
*
* While index repair is disabled, another process can safely repair the
* indexes in the background. When it is complete, index repair can be
* enabled for this repository too.
*/
public void setIndexRepairEnabled(boolean enabled) {
mIndexRepairEnabled = enabled;
}
/**
* Returns the throttle parameter used when indexes are added, dropped or
* bulk repaired. By default this value is 1.0, or maximum speed.
*/
public double getIndexRepairThrottle() {
return mIndexThrottle;
}
/**
* Sets the throttle parameter used when indexes are added, dropped or bulk
* repaired. By default this value is 1.0, or maximum speed.
*
* @param desiredSpeed 1.0 = perform work at full speed,
* 0.5 = perform work at half speed, 0.0 = fully suspend work
*/
public void setIndexRepairThrottle(double desiredSpeed) {
mIndexThrottle = desiredSpeed;
}
/**
* Sets the repository to read-only mode. By default, repository is opened
* for reads and writes.
*/
public void setReadOnly(boolean readOnly) {
mReadOnly = readOnly;
}
/**
* Returns true if repository should be opened read-only.
*/
public boolean getReadOnly() {
return mReadOnly;
}
/**
* Set the repository cache size, in bytes. Actual BDB implementation will
* select a suitable default if this is not set.
*/
public void setCacheSize(long cacheSize) {
mCacheSize = cacheSize;
}
/**
* Set the repository cache size, in bytes. Actual BDB implementation will
* select a suitable default if this is not set.
*
* @param cacheSize cache size to use, or null for default
*/
public void setCacheSize(Long cacheSize) {
mCacheSize = cacheSize;
}
/**
* Returns the repository cache size, or null if default should be
* selected.
*/
public Long getCacheSize() {
return mCacheSize;
}
/**
* Set the repository log region size, in bytes.
*/
public void setLogRegionSize(int logRegionSize) {
mLogRegionSize = logRegionSize;
}
/**
* Set the repository log region size, in bytes.
*/
public void setLogRegionSize(Integer logRegionSize) {
mLogRegionSize = logRegionSize;
}
/**
* Returns the repository log region size, or null if the default
* should be selected.
*/
public Integer getLogRegionSize() {
return mLogRegionSize;
}
/**
* Set the percent of JVM heap used by the repository cache. Actual
* BDB implementation will select a suitable default if this is not
* set. This is overridden by setting an explicit cacheSize.
*/
public void setCachePercent(int cachePercent) {
mCachePercent = cachePercent;
}
/**
* Set the percent of JVM heap used by the repository cache. Actual
* BDB implementation will select a suitable default if this is not
* set. This is overridden by setting an explicit cacheSize.
*
* @param cachePercent percent of JVM heap to use, or null for default
*/
public void setCachePercent(Integer cachePercent) {
mCachePercent = cachePercent;
}
/**
* Returns the percent of JVM heap used by the repository cache, or
* null if default should be selected.
*/
public Integer getCachePercent() {
return mCachePercent;
}
/**
* Set the lock timeout, in seconds. Default value is 0.5 seconds.
*/
public void setLockTimeout(double lockTimeout) {
mLockTimeout = lockTimeout;
}
/**
* Returns the lock timeout, in seconds.
*/
public double getLockTimeout() {
return mLockTimeout;
}
/**
* Returns the lock timeout, in microseconds, limited to max long value.
*/
public long getLockTimeoutInMicroseconds() {
return inMicros(mLockTimeout);
}
public void setMaxLocks(Integer max) {
mMaxLocks = max;
}
public Integer getMaxLocks() {
return mMaxLocks;
}
/**
* Set the transaction timeout, in seconds. Default value is 300 seconds.
*/
public void setTransactionTimeout(double txnTimeout) {
mTxnTimeout = txnTimeout;
}
/**
* Returns the repository transaction timeout, in seconds.
*/
public double getTransactionTimeout() {
return mTxnTimeout;
}
/**
* Returns the repository transaction timeout, in microseconds, limited to
* max long value.
*/
public long getTransactionTimeoutInMicroseconds() {
return inMicros(mTxnTimeout);
}
/**
* When true, commits are not immediately written or flushed to disk. This
* improves performance, but there is a chance of losing the most recent
* commits if the process is killed or if the machine crashes.
*/
public void setTransactionNoSync(boolean noSync) {
mTxnNoSync = noSync;
}
/**
* Returns true if transactions are not written or flushed to disk.
*/
public boolean getTransactionNoSync() {
return mTxnNoSync;
}
/**
* When true, commits are written, but they are not flushed to disk. This
* improves performance, but there is a chance of losing the most recent
* commits if the machine crashes.
*/
public void setTransactionWriteNoSync(boolean noSync) {
mTxnWriteNoSync = noSync;
}
/**
* Returns true if transactions are not flushed to disk.
*/
public boolean getTransactionWriteNoSync() {
return mTxnWriteNoSync;
}
/**
* Set the maximum number of concurrent transactions, or pass null to use
* the default. This setting has no effect for BDB-JE.
*/
public void setTransactionMaxActive(Integer max) {
mTxnMaxActive = max;
}
/**
* Returns the maximum number of concurrent transactions, or null if the
* default is used.
*/
public Integer getTransactionMaxActive() {
return mTxnMaxActive;
}
/**
* When true, allows databases to be transactional. This setting affects
* the databases, not the environment. If this is not explicitly set, the
* environment getTransactional is used.
*/
public void setDatabasesTransactional(Boolean transactional) {
mDatabasesTransactional = transactional;
}
/**
* Returns true if the databases are configured to be transactional,
* false if configured to not be transactional, null if this override was never set
*/
public Boolean getDatabasesTransactional() {
return mDatabasesTransactional;
}
/**
* Pass true to disable reverse split of B-tree nodes to reduce deadlocks.
* This setting has no effect for BDB-JE.
*/
public void setReverseSplitOff(boolean off) {
mReverseSplitOff = off;
}
public boolean isReverseSplitOff() {
return mReverseSplitOff;
}
/**
* Sets the desired page size for a given type. If not specified, the page
* size applies to all types.
*/
public void setDatabasePageSize(Integer bytes, Class extends Storable> type) {
if (mDatabasePageSizes == null) {
mDatabasePageSizes = new HashMap Checkpoint threshold is only used by Carbonado's built-in
* checkpointer, and is ignored when using BDB-JE.
*
* @param thresholdKB run checkpoint if at least this many kilobytes in log
*/
public void setCheckpointThresholdKB(int thresholdKB) {
mCheckpointThresholdKB = thresholdKB;
}
/**
* @return run checkpoint if at least this many kilobytes in log
*/
public int getCheckpointThresholdKB() {
return mCheckpointThresholdKB;
}
/**
* Set the time threshold to run checkpoints. This setting is ignored if
* the checkpointer is not configured to run. Default value is 1 minute.
*
* Checkpoint threshold is only used by Carbonado's built-in
* checkpointer, and is ignored when using BDB-JE.
*
* @param thresholdMinutes run checkpoint if at least this many minutes
* passed since last checkpoint
*/
public void setCheckpointThresholdMinutes(int thresholdMinutes) {
mCheckpointThresholdMinutes = thresholdMinutes;
}
/**
* @return run checkpoint if at least this many minutes passed since last
* checkpoint
*/
public int getCheckpointThresholdMinutes() {
return mCheckpointThresholdMinutes;
}
/**
* By default, transaction log files are deleted when no longer needed.
* Keeping log files can be used for incremental backups or for diagnosing
* problems. If using BDB-JE, old log files are renamed with a ".del"
* extension. If using BDB-core, the db_archive utility is required for
* identifying old log files.
*/
public void setKeepOldLogFiles(boolean keep) {
mKeepOldLogFiles = keep;
}
/**
* Returns false by default.
*/
public boolean getKeepOldLogFiles() {
return mKeepOldLogFiles;
}
/**
* Disable automatic deadlock detection of database if another thread is
* responsible for that.
*/
public void setRunDeadlockDetector(boolean runDeadlockDetector) {
mRunDeadlockDetector = runDeadlockDetector;
}
/**
* Returns true if deadlock detector is configured to run.
*/
public boolean getRunDeadlockDetector() {
return mRunDeadlockDetector;
}
/**
* Enable deadlock detection whenever a lock conflict occurs. Default is
* off, and it has no effect for BDB-JE.
*/
public void setLockConflictDeadlockDetectMode(boolean b) {
mLockConflictDeadlockDetect = b;
}
/**
* Returns true if deadlock detection is run whenever a lock conflict occurs.
*/
public boolean getLockConflictDeadlockDetectMode() {
return mLockConflictDeadlockDetect;
}
/**
* When true, enable checksum verification of pages read into the cache
* from the backing filestore. By default checksum is enabled for BDB-JE,
* and disabled for BDB-C.
*/
public void setChecksumEnabled(Boolean checksumEnabled) {
mChecksumEnabled = checksumEnabled;
}
/**
* Returns true if checksum verification is enabled. Returns null if the
* BDB default is used.
*/
public Boolean getChecksumEnabled() {
return mChecksumEnabled;
}
/**
* Optionally set the BDB specific environment configuration to
* use. The builder will verify that needed configuration values are set.
*/
public void setInitialEnvironmentConfig(Object envConfig) {
mInitialEnvConfig = envConfig;
}
/**
* Returns the optional BDB specific environment configuration to use.
*/
public Object getInitialEnvironmentConfig() {
return mInitialEnvConfig;
}
/**
* Optionally set the BDB specific database configuration to use
* for all databases created. The storage will verify that needed
* configuration values are set.
*/
public void setInitialDatabaseConfig(Object dbConfig) {
mInitialDBConfig = dbConfig;
}
/**
* Returns the optional BDB specific database configuration to use
* for all databases created.
*/
public Object getInitialDatabaseConfig() {
return mInitialDBConfig;
}
/**
* Override the default storable codec factory.
*/
public void setStorableCodecFactory(StorableCodecFactory factory) {
mStorableCodecFactory = factory;
}
/**
* Returns the storable codec factory used.
*/
public StorableCodecFactory getStorableCodecFactory() {
return mStorableCodecFactory;
}
/**
* Sets a callback to be invoked before the repository has finished running
* its own shutdown hooks. This method is also invoked when repository is
* manually closed.
*/
public void setPreShutdownHook(Runnable hook) {
mPreShutdownHook = hook;
}
/**
* Returns the custom shutdown hook that runs before the repository has
* finished running its own shutdown hooks, or null if none.
*/
public Runnable getPreShutdownHook() {
return mPreShutdownHook;
}
/**
* Sets a callback to be invoked after repository has finished running its
* own shutdown hooks. This method is also invoked when repository is
* manually closed.
*/
public void setShutdownHook(Runnable hook) {
mPostShutdownHook = hook;
}
/**
* Returns the custom shutdown hook that runs after the repository has
* finished running its own shutdown hooks, or null if none.
*/
public Runnable getShutdownHook() {
return mPostShutdownHook;
}
/**
* Sets a hook to be called whenever a database is opened.
*/
public void setDatabaseHook(DatabaseHook hook) {
mDatabaseHook = hook;
}
/**
* Returns the custom open database hook, or null if none.
*/
public DatabaseHook getDatabaseHook() {
return mDatabaseHook;
}
/**
* Set the compressor for the given class, overriding a custom StorableCodecFactory.
* @param type Storable to compress.
* @param compressionType String representation of type of
* compression. Available options are "NONE" for no compression or "GZIP"
* for gzip compression
*/
public void setCompressor(String type, String compressionType) {
mStorableCodecFactory = null;
compressionType = compressionType.toUpperCase();
if (mCompressionMap == null) {
mCompressionMap = new HashMap