From 55d57b7d9f075c275e4fe30d29577a5b914f05db Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Thu, 5 Nov 2009 23:58:22 +0000 Subject: Add support for compressed records. --- .../carbonado/repo/sleepycat/BDBRepository.java | 37 +++++++++++------- .../repo/sleepycat/BDBRepositoryBuilder.java | 45 +++++++++++++++++++++- .../carbonado/repo/sleepycat/BDBStorage.java | 10 ++--- 3 files changed, 71 insertions(+), 21 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo') diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java index 6fbc4f5..28fc347 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java @@ -49,6 +49,7 @@ import com.amazon.carbonado.capability.StorableInfoCapability; import com.amazon.carbonado.info.StorableIntrospector; +import com.amazon.carbonado.layout.Layout; import com.amazon.carbonado.layout.LayoutCapability; import com.amazon.carbonado.layout.LayoutFactory; @@ -88,7 +89,8 @@ abstract class BDBRepository extends AbstractRepository EnvironmentCapability, ShutdownCapability, StorableInfoCapability, - SequenceCapability + SequenceCapability, + LayoutCapability { private final Log mLog = LogFactory.getLog(getClass()); @@ -168,19 +170,6 @@ abstract class BDBRepository extends AbstractRepository getLog().info("Opening repository \"" + getName() + '"'); } - @Override - @SuppressWarnings("unchecked") - public C getCapability(Class capabilityType) { - C cap = super.getCapability(capabilityType); - if (cap != null) { - return cap; - } - if (capabilityType == LayoutCapability.class) { - return (C) mLayoutFactory; - } - return null; - } - public IndexInfo[] getIndexInfo(Class storableType) throws RepositoryException { @@ -340,6 +329,26 @@ abstract class BDBRepository extends AbstractRepository return (BDBStorage) storageFor(type); } + @Override + public Layout layoutFor(Class type) + throws FetchException, PersistException + { + try { + return ((BDBStorage) storageFor(type)).getLayout(mStorableCodecFactory); + } catch (PersistException e) { + throw e; + } catch (RepositoryException e) { + throw e.toFetchException(); + } + } + + @Override + public Layout layoutFor(Class type, int generation) + throws FetchException + { + return mLayoutFactory.layoutFor(type, generation); + } + @Override protected void finalize() { close(); diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java index c1e6196..0beea65 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepositoryBuilder.java @@ -34,7 +34,8 @@ import com.amazon.carbonado.RepositoryException; import com.amazon.carbonado.Storable; import com.amazon.carbonado.repo.indexed.IndexedRepositoryBuilder; -import com.amazon.carbonado.raw.GenericStorableCodecFactory; +import com.amazon.carbonado.raw.CompressionType; +import com.amazon.carbonado.raw.CompressedStorableCodecFactory; import com.amazon.carbonado.raw.StorableCodecFactory; import com.amazon.carbonado.spi.AbstractRepositoryBuilder; @@ -101,6 +102,7 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { private boolean mPrivate; private boolean mMultiversion; private boolean mLogInMemory; + private Integer mLogFileMaxSize; private boolean mRunFullRecovery; private boolean mRunCheckpointer = true; private int mCheckpointInterval = DEFAULT_CHECKPOINT_INTERVAL; @@ -110,10 +112,11 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { private Boolean mChecksumEnabled; private Object mInitialEnvConfig = null; private Object mInitialDBConfig = null; - private StorableCodecFactory mStorableCodecFactory = new GenericStorableCodecFactory(); + private StorableCodecFactory mStorableCodecFactory; private Runnable mPreShutdownHook; private Runnable mPostShutdownHook; private DatabaseHook mDatabaseHook; + private Map mCompressionMap; public BDBRepositoryBuilder() { } @@ -136,6 +139,10 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { } } + if (mStorableCodecFactory == null) { + mStorableCodecFactory = new CompressedStorableCodecFactory(mCompressionMap); + } + assertReady(); // Make environment directory if it doesn't exist. @@ -860,6 +867,40 @@ public class BDBRepositoryBuilder extends AbstractRepositoryBuilder { 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(); + } + CompressionType compressionEnum = CompressionType.valueOf(compressionType); + if (compressionEnum != null) { + mCompressionMap.put(type, compressionEnum); + } + } + + /** + * Return the compressor used for the given storable. + * @param type Storable to compress + * @return String representation of the type of compression used. Available options are "NONE" + * for no compression and "GZIP" for gzip compression. + */ + public String getCompressor(String type) { + if (mCompressionMap == null) { + return null; + } + + return mCompressionMap.get(type).toString(); + } + private long inMicros(double seconds) { if (seconds >= Long.MAX_VALUE) { return Long.MAX_VALUE; diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java index 4f1ff29..d00b7d5 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java @@ -60,6 +60,7 @@ import com.amazon.carbonado.info.StorableProperty; import com.amazon.carbonado.layout.Layout; import com.amazon.carbonado.layout.LayoutFactory; +import com.amazon.carbonado.layout.LayoutOptions; import com.amazon.carbonado.layout.Unevolvable; import com.amazon.carbonado.lob.Blob; @@ -416,11 +417,9 @@ abstract class BDBStorage implements Storage, Storag protected void open(boolean readOnly, Txn openTxn, boolean installTriggers) throws RepositoryException { - final Layout layout = getLayout(); - StorableInfo info = StorableIntrospector.examine(getStorableType()); - StorableCodecFactory codecFactory = mRepository.getStorableCodecFactory(); + final Layout layout = getLayout(codecFactory); // Open primary database. Object primaryDatabase; @@ -767,7 +766,7 @@ abstract class BDBStorage implements Storage, Storag } } - private Layout getLayout() throws RepositoryException { + Layout getLayout(StorableCodecFactory codecFactory) throws RepositoryException { if (Unevolvable.class.isAssignableFrom(getStorableType())) { // Don't record generation for storables marked as unevolvable. return null; @@ -782,7 +781,8 @@ abstract class BDBStorage implements Storage, Storag return null; } - return factory.layoutFor(getStorableType()); + Class type = getStorableType(); + return factory.layoutFor(type, codecFactory.getLayoutOptions(type)); } /** -- cgit v1.2.3