Author: stliu
Date: 2011-02-08 10:38:08 -0500 (Tue, 08 Feb 2011)
New Revision: 20888
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java
Log:
JBPAPP-5763 HHH-5217 Minimize double sequence value reads in PooledOptimizer
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java
===================================================================
---
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java 2011-02-03
18:27:30 UTC (rev 20887)
+++
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java 2011-02-08
15:38:08 UTC (rev 20888)
@@ -47,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 ) ) {
@@ -74,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 {
@@ -242,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 ) {
@@ -269,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();
@@ -300,5 +341,9 @@
public long getLastValue() {
return value - 1;
}
+
+ public void injectInitialValue( long initialValue ) {
+ this.initialValue = initialValue;
+ }
}
}
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java
===================================================================
---
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java 2011-02-03
18:27:30 UTC (rev 20887)
+++
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java 2011-02-08
15:38:08 UTC (rev 20888)
@@ -178,7 +178,7 @@
this.databaseStructure = buildDatabaseStructure( params, dialect, forceTableUse,
sequenceName, initialValue, incrementSize );
- this.optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy,
identifierType.getReturnedClass(), incrementSize );
+ this.optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy,
identifierType.getReturnedClass(), incrementSize, -1 );
this.databaseStructure.prepare( optimizer );
}
@@ -281,7 +281,7 @@
* @param sequenceName The name to use for the sequence or table.
* @param initialValue The initial value.
* @param incrementSize the increment size to use (after any adjustments).
- * @return The db structure representation
+ * @return An abstraction for the actual database structure in use (table vs.
sequence).
*/
protected DatabaseStructure buildDatabaseStructure(
Properties params,
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java
===================================================================
---
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java 2011-02-03
18:27:30 UTC (rev 20887)
+++
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java 2011-02-08
15:38:08 UTC (rev 20888)
@@ -294,7 +294,7 @@
String defOptStrategy = incrementSize <= 1 ? OptimizerFactory.NONE :
OptimizerFactory.POOL;
String optimizationStrategy = PropertiesHelper.getString( OPT_PARAM, params,
defOptStrategy );
- optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy,
identifierType.getReturnedClass(), incrementSize );
+ optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy,
identifierType.getReturnedClass(), incrementSize, -1 );
}
/**