From 8809341248c62b15b78d7e6d8e06ab2ec3793c8e Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 28 Mar 2007 22:00:24 +0000 Subject: Merged 1.2-dev to trunk. --- .../amazon/carbonado/raw/CustomStorableCodec.java | 39 +++++++ .../carbonado/raw/CustomStorableCodecFactory.java | 40 +++++++ .../carbonado/raw/GenericEncodingStrategy.java | 5 +- .../carbonado/raw/GenericInstanceFactory.java | 6 +- .../amazon/carbonado/raw/GenericStorableCodec.java | 120 ++++++++++++--------- .../carbonado/raw/GenericStorableCodecFactory.java | 23 +++- .../com/amazon/carbonado/raw/StorableCodec.java | 31 +++++- .../amazon/carbonado/raw/StorableCodecFactory.java | 16 +++ 8 files changed, 221 insertions(+), 59 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/raw') diff --git a/src/main/java/com/amazon/carbonado/raw/CustomStorableCodec.java b/src/main/java/com/amazon/carbonado/raw/CustomStorableCodec.java index 60bde78..cfb0883 100644 --- a/src/main/java/com/amazon/carbonado/raw/CustomStorableCodec.java +++ b/src/main/java/com/amazon/carbonado/raw/CustomStorableCodec.java @@ -232,6 +232,10 @@ public abstract class CustomStorableCodec implements Storabl private final int mPkPropertyCount; private final InstanceFactory mInstanceFactory; + // Modified by CustomStorableCodecFactory after construction. This provides + // backwards compatibility with implementations of CustomStorableCodecFactory. + RawSupport mSupport; + public interface InstanceFactory { Storable instantiate(RawSupport support, CustomStorableCodec codec); @@ -245,17 +249,40 @@ public abstract class CustomStorableCodec implements Storabl * @throws SupportException if Storable is not supported */ public CustomStorableCodec(Class type, boolean isMaster) throws SupportException { + this(type, isMaster, null); + } + + /** + * @param isMaster when true, version properties and sequences are managed + * @throws SupportException if Storable is not supported + */ + public CustomStorableCodec(Class type, boolean isMaster, RawSupport support) + throws SupportException + { mType = type; mPkPropertyCount = getPrimaryKeyIndex().getPropertyCount(); Class storableClass = getStorableClass(type, isMaster); mInstanceFactory = QuickConstructorGenerator .getInstance(storableClass, InstanceFactory.class); + mSupport = support; } public Class getStorableType() { return mType; } + @SuppressWarnings("unchecked") + public S instantiate() { + return (S) mInstanceFactory.instantiate(support(), this); + } + + @SuppressWarnings("unchecked") + public S instantiate(byte[] key, byte[] value) + throws FetchException + { + return (S) mInstanceFactory.instantiate(support(), key, value, this); + } + @SuppressWarnings("unchecked") public S instantiate(RawSupport support) { return (S) mInstanceFactory.instantiate(support, this); @@ -276,6 +303,18 @@ public abstract class CustomStorableCodec implements Storabl return encodePrimaryKey(values, 0, mPkPropertyCount); } + public RawSupport getSupport() { + return mSupport; + } + + private RawSupport support() { + RawSupport support = mSupport; + if (support == null) { + throw new IllegalStateException("No RawSupport"); + } + return support; + } + /** * Convenient access to all the storable properties. */ diff --git a/src/main/java/com/amazon/carbonado/raw/CustomStorableCodecFactory.java b/src/main/java/com/amazon/carbonado/raw/CustomStorableCodecFactory.java index bfba733..a3af33f 100644 --- a/src/main/java/com/amazon/carbonado/raw/CustomStorableCodecFactory.java +++ b/src/main/java/com/amazon/carbonado/raw/CustomStorableCodecFactory.java @@ -57,6 +57,30 @@ public abstract class CustomStorableCodecFactory implements StorableCodecFactory return createCodec(type, isMaster, layout); } + /** + * @param type type of storable to create codec for + * @param pkIndex ignored + * @param isMaster when true, version properties and sequences are managed + * @param layout when non-null, attempt to encode a storable layout + * generation value in each storable + * @param support binds generated storable with a storage layer + * @throws SupportException if type is not supported + */ + public CustomStorableCodec createCodec(Class type, + StorableIndex pkIndex, + boolean isMaster, + Layout layout, + RawSupport support) + throws SupportException + { + CustomStorableCodec codec = createCodec(type, isMaster, layout, support); + // Possibly set support after construction, for backwards compatibility. + if (codec.mSupport == null) { + codec.mSupport = support; + } + return codec; + } + /** * @param type type of storable to create codec for * @param isMaster when true, version properties and sequences are managed @@ -67,4 +91,20 @@ public abstract class CustomStorableCodecFactory implements StorableCodecFactory protected abstract CustomStorableCodec createCodec(Class type, boolean isMaster, Layout layout) throws SupportException; + + /** + * @param type type of storable to create codec for + * @param isMaster when true, version properties and sequences are managed + * @param layout when non-null, attempt to encode a storable layout + * generation value in each storable + * @param support binds generated storable with a storage layer + * @throws SupportException if type is not supported + */ + // Note: This factory method is not abstract for backwards compatibility. + protected CustomStorableCodec + createCodec(Class type, boolean isMaster, Layout layout, RawSupport support) + throws SupportException + { + return createCodec(type, isMaster, layout); + } } diff --git a/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java b/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java index 76622b7..c22881e 100644 --- a/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java +++ b/src/main/java/com/amazon/carbonado/raw/GenericEncodingStrategy.java @@ -1318,8 +1318,11 @@ public class GenericEncodingStrategy { * Generates code to push RawSupport instance to the stack. RawSupport is * available only in Storable instances. If instanceVar is an Object[], a * SupportException is thrown. + * + * @param instanceVar Storable instance or array of property values. Null + * is storable instance of "this". */ - private void pushRawSupport(CodeAssembler a, LocalVariable instanceVar) + protected void pushRawSupport(CodeAssembler a, LocalVariable instanceVar) throws SupportException { boolean isObjectArrayInstanceVar = instanceVar != null diff --git a/src/main/java/com/amazon/carbonado/raw/GenericInstanceFactory.java b/src/main/java/com/amazon/carbonado/raw/GenericInstanceFactory.java index 339bdbd..fb2ef45 100644 --- a/src/main/java/com/amazon/carbonado/raw/GenericInstanceFactory.java +++ b/src/main/java/com/amazon/carbonado/raw/GenericInstanceFactory.java @@ -30,9 +30,7 @@ import com.amazon.carbonado.Storable; public interface GenericInstanceFactory { Storable instantiate(RawSupport support); - Storable instantiate(RawSupport support, byte[] key) - throws FetchException; + Storable instantiate(RawSupport support, byte[] key) throws FetchException; - Storable instantiate(RawSupport support, byte[] key, byte[] value) - throws FetchException; + Storable instantiate(RawSupport support, byte[] key, byte[] value) throws FetchException; } diff --git a/src/main/java/com/amazon/carbonado/raw/GenericStorableCodec.java b/src/main/java/com/amazon/carbonado/raw/GenericStorableCodec.java index 741a50d..0ef4846 100644 --- a/src/main/java/com/amazon/carbonado/raw/GenericStorableCodec.java +++ b/src/main/java/com/amazon/carbonado/raw/GenericStorableCodec.java @@ -79,6 +79,7 @@ public class GenericStorableCodec implements StorableCodec implements StorableCodec GenericStorableCodec getInstance (GenericStorableCodecFactory factory, - GenericEncodingStrategy encodingStrategy, boolean isMaster, Layout layout) + GenericEncodingStrategy encodingStrategy, boolean isMaster, + Layout layout, RawSupport support) throws SupportException { - Object key; - if (layout == null) { - key = KeyFactory.createKey(new Object[] {encodingStrategy, isMaster}); - } else { - key = KeyFactory.createKey - (new Object[] {encodingStrategy, isMaster, factory, layout.getGeneration()}); - } + Object key = KeyFactory.createKey(new Object[] {encodingStrategy, isMaster, layout}); - GenericStorableCodec codec = (GenericStorableCodec) cCache.get(key); - if (codec == null) { - codec = new GenericStorableCodec - (factory, - encodingStrategy.getType(), - generateStorable(encodingStrategy, isMaster, layout), - encodingStrategy, - layout); - cCache.put(key, codec); + Class storableImpl = (Class) cCache.get(key); + if (storableImpl == null) { + storableImpl = generateStorable(encodingStrategy, isMaster, layout); + cCache.put(key, storableImpl); } - return codec; + return new GenericStorableCodec + (factory, + encodingStrategy.getType(), + storableImpl, + encodingStrategy, + layout, + support); } @SuppressWarnings("unchecked") @@ -325,10 +322,7 @@ public class GenericStorableCodec implements StorableCodec mStorableClass; - // Weakly reference the encoding strategy because of the way - // GenericStorableCodec instances are cached in a SoftValuedHashMap. - // GenericStorableCodec can still be reclaimed by the garbage collector. - private final WeakReference> mEncodingStrategy; + private final GenericEncodingStrategy mEncodingStrategy; private final GenericInstanceFactory mInstanceFactory; @@ -339,27 +333,30 @@ public class GenericStorableCodec implements StorableCodec mSupport; + // Maps layout generations to Decoders. private IntHashMap mDecoders; private GenericStorableCodec(GenericStorableCodecFactory factory, Class type, Class storableClass, GenericEncodingStrategy encodingStrategy, - Layout layout) { + Layout layout, RawSupport support) + { mFactory = factory; mType = type; mStorableClass = storableClass; - mEncodingStrategy = new WeakReference>(encodingStrategy); + mEncodingStrategy = encodingStrategy; mInstanceFactory = QuickConstructorGenerator .getInstance(storableClass, GenericInstanceFactory.class); mPrimaryKeyFactory = getSearchKeyFactory(encodingStrategy.gatherAllKeyProperties()); mLayout = layout; + mSupport = support; if (layout != null) { try { // Assign static reference back to this codec. - Method m = storableClass.getMethod - (ASSIGN_CODEC_METHOD_NAME, WeakReference.class); + Method m = storableClass.getMethod(ASSIGN_CODEC_METHOD_NAME, WeakReference.class); m.invoke(null, new WeakReference(this)); } catch (Exception e) { ThrowUnchecked.fireFirstDeclaredCause(e); @@ -375,7 +372,30 @@ public class GenericStorableCodec implements StorableCodec implements StorableCodec support, byte[] key, byte[] value) - throws FetchException - { + public S instantiate(RawSupport support, byte[] key, byte[] value) throws FetchException { try { return (S) mInstanceFactory.instantiate(support, key, value); } catch (CorruptEncodingException e) { @@ -407,11 +426,11 @@ public class GenericStorableCodec implements StorableCodec getPrimaryKeyIndex() { - return getEncodingStrategy().getPrimaryKeyIndex(); + return mEncodingStrategy.getPrimaryKeyIndex(); } public int getPrimaryKeyPrefixLength() { - return getEncodingStrategy().getConstantKeyPrefixLength(); + return mEncodingStrategy.getConstantKeyPrefixLength(); } public byte[] encodePrimaryKey(S storable) { @@ -434,6 +453,18 @@ public class GenericStorableCodec implements StorableCodec getSupport() { + return mSupport; + } + + private RawSupport support() { + RawSupport support = mSupport; + if (support == null) { + throw new IllegalStateException("No RawSupport"); + } + return support; + } + /** * Returns a concrete Storable implementation, which is fully * thread-safe. It has two constructors defined: @@ -499,17 +530,8 @@ public class GenericStorableCodec implements StorableCodec getEncodingStrategy() { - // Should never be null, even though it is weakly referenced. As long - // as this class can be reached by the cache, the encoding strategy - // object exists since it is the cache key. - return mEncodingStrategy.get(); - } - @SuppressWarnings("unchecked") private SearchKeyFactory generateSearchKeyFactory(OrderedProperty[] properties) { - GenericEncodingStrategy encodingStrategy = getEncodingStrategy(); - ClassInjector ci; { StringBuilder b = new StringBuilder(); @@ -562,7 +584,7 @@ public class GenericStorableCodec implements StorableCodec implements StorableCodec implements StorableCodec implements StorableCodec implements StorableCodec implements StorableCodec GenericStorableCodec createCodec(Class type, + StorableIndex pkIndex, + boolean isMaster, + Layout layout, + RawSupport support) + throws SupportException { return GenericStorableCodec.getInstance - (this, createStrategy(type, pkIndex), isMaster, layout); + (this, createStrategy(type, pkIndex), isMaster, layout, support); } /** diff --git a/src/main/java/com/amazon/carbonado/raw/StorableCodec.java b/src/main/java/com/amazon/carbonado/raw/StorableCodec.java index 7965690..f9e347c 100644 --- a/src/main/java/com/amazon/carbonado/raw/StorableCodec.java +++ b/src/main/java/com/amazon/carbonado/raw/StorableCodec.java @@ -35,19 +35,36 @@ public interface StorableCodec { Class getStorableType(); /** - * Instantiate a Storable with no key or value defined yet. + * Instantiate a Storable with no key or value defined yet. The default + * {@link RawSupport} is supplied to the instance. + * + * @throws IllegalStateException if no default support exists + */ + S instantiate(); + + /** + * Instantiate a Storable with a specific key and value. The default + * {@link RawSupport} is supplied to the instance. + * + * @throws IllegalStateException if no default support exists + */ + S instantiate(byte[] key, byte[] value) throws FetchException; + + /** + * Instantiate a Storable with no key or value defined yet. Any + * {@link RawSupport} can be supplied to the instance. * * @param support binds generated storable with a storage layer */ S instantiate(RawSupport support); /** - * Instantiate a Storable with a specific key and value. + * Instantiate a Storable with a specific key and value. Any + * {@link RawSupport} can be supplied to the instance. * * @param support binds generated storable with a storage layer */ - S instantiate(RawSupport support, byte[] key, byte[] value) - throws FetchException; + S instantiate(RawSupport support, byte[] key, byte[] value) throws FetchException; /** * Returns the sequence and directions of properties that make up the @@ -113,4 +130,10 @@ public interface StorableCodec { * prefix. Returned value may be null if no prefix is defined. */ byte[] encodePrimaryKeyPrefix(); + + /** + * Returns the default {@link RawSupport} object that is supplied to + * Storable instances produced by this codec. + */ + RawSupport getSupport(); } diff --git a/src/main/java/com/amazon/carbonado/raw/StorableCodecFactory.java b/src/main/java/com/amazon/carbonado/raw/StorableCodecFactory.java index 1b19490..db05a9e 100644 --- a/src/main/java/com/amazon/carbonado/raw/StorableCodecFactory.java +++ b/src/main/java/com/amazon/carbonado/raw/StorableCodecFactory.java @@ -51,4 +51,20 @@ public interface StorableCodecFactory { boolean isMaster, Layout layout) throws SupportException; + + /** + * @param type type of storable to create codec for + * @param pkIndex suggested index for primary key (optional) + * @param isMaster when true, version properties and sequences are managed + * @param layout when non-null, attempt to encode a storable layout + * generation value in each storable + * @param support binds generated storable with a storage layer + * @throws SupportException if type is not supported + */ + StorableCodec createCodec(Class type, + StorableIndex pkIndex, + boolean isMaster, + Layout layout, + RawSupport support) + throws SupportException; } -- cgit v1.2.3