Hibernate SVN: r20914 - core/patches/JBOSS_EAP_3_3_2_CP03_JBPAPP-5939/core/src/main/java/org/hibernate/id/enhanced.
by hibernate-commits@lists.jboss.org
Author: alessandrolt
Date: 2011-02-15 12:54:56 -0500 (Tue, 15 Feb 2011)
New Revision: 20914
Modified:
core/patches/JBOSS_EAP_3_3_2_CP03_JBPAPP-5939/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java
Log:
JBPAPP-5939: backporting the fix of JBPAPP-5763 to on-off patch
Modified: core/patches/JBOSS_EAP_3_3_2_CP03_JBPAPP-5939/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java
===================================================================
--- core/patches/JBOSS_EAP_3_3_2_CP03_JBPAPP-5939/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java 2011-02-15 16:28:02 UTC (rev 20913)
+++ core/patches/JBOSS_EAP_3_3_2_CP03_JBPAPP-5939/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java 2011-02-15 17:54:56 UTC (rev 20914)
@@ -27,13 +27,12 @@
import java.io.Serializable;
import java.lang.reflect.Constructor;
+import org.hibernate.HibernateException;
+import org.hibernate.id.IdentifierGeneratorFactory;
+import org.hibernate.util.ReflectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.hibernate.HibernateException;
-import org.hibernate.util.ReflectHelper;
-import org.hibernate.id.IdentifierGeneratorFactory;
-
/**
* Factory for {@link Optimizer} instances.
*
@@ -48,6 +47,23 @@
private static Class[] CTOR_SIG = new Class[] { Class.class, int.class };
+ /**
+ * Marker interface for optimizer which wish to know the user-specified initial value.
+ * <p/>
+ * Used instead of constructor injection since that is already a public understanding and
+ * because not all optimizers care.
+ */
+ public static interface InitialValueAwareOptimizer {
+ /**
+ * Reports the user specified initial value to the optimizer.
+ * <p/>
+ * <tt>-1</tt> is used to indicate that the user did not specify.
+ *
+ * @param initialValue The initial value specified by the user, or <tt>-1</tt> to indicate that the
+ * user did not specify.
+ */
+ public void injectInitialValue(long initialValue);
+ }
public static Optimizer buildOptimizer(String type, Class returnClass, int incrementSize) {
String optimizerClassName;
if ( NONE.equals( type ) ) {
@@ -75,8 +91,25 @@
// the default...
return new NoopOptimizer( returnClass, incrementSize );
}
-
/**
+ * Builds an optimizer
+ *
+ * @param type The optimizer type, either a short-hand name or the {@link Optimizer} class name.
+ * @param returnClass The generated value java type
+ * @param incrementSize The increment size.
+ * @param explicitInitialValue The user supplied initial-value (-1 indicates the user did not specify).
+ *
+ * @return The built optimizer
+ */
+ @SuppressWarnings({ "UnnecessaryBoxing", "deprecation" })
+ public static Optimizer buildOptimizer(String type, Class returnClass, int incrementSize, long explicitInitialValue) {
+ final Optimizer optimizer = buildOptimizer( type, returnClass, incrementSize );
+ if ( InitialValueAwareOptimizer.class.isInstance( optimizer ) ) {
+ ( (InitialValueAwareOptimizer) optimizer ).injectInitialValue( explicitInitialValue );
+ }
+ return optimizer;
+ }
+ /**
* Common support for optimizer implementations.
*/
public static abstract class OptimizerSupport implements Optimizer {
@@ -243,10 +276,10 @@
* Optimizer which uses a pool of values, storing the next low value of the
* range in the database.
*/
- public static class PooledOptimizer extends OptimizerSupport {
+ public static class PooledOptimizer extends OptimizerSupport implements InitialValueAwareOptimizer{
private long value;
private long hiValue = -1;
-
+ private long initialValue = -1;
public PooledOptimizer(Class returnClass, int incrementSize) {
super( returnClass, incrementSize );
if ( incrementSize < 1 ) {
@@ -270,7 +303,14 @@
// we are using a sequence...
log.info( "pooled optimizer source reported [" + value + "] as the initial value; use of 1 or greater highly recommended" );
}
- hiValue = callback.getNextValue();
+ if ( ( initialValue == -1 && value < incrementSize ) || ( value == initialValue ) ) {
+ // the call to obtain next-value just gave us the initialValue
+ hiValue = callback.getNextValue();
+ }
+ else {
+ hiValue = value;
+ value = hiValue - incrementSize;
+ }
}
else if ( value >= hiValue ) {
hiValue = callback.getNextValue();
@@ -301,5 +341,9 @@
public long getLastValue() {
return value - 1;
}
+
+ public void injectInitialValue( long initialValue ) {
+ this.initialValue = initialValue;
+ }
}
-}
+}
\ No newline at end of file