Skip to content

Commit

Permalink
HHH-14444 Encapsulate ID generation in GenerationState for PooledLoTh…
Browse files Browse the repository at this point in the history
…readLocalOptimizer

This is just a cosmetic change, it doesn't change the behavior at all.

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
  • Loading branch information
yrodiere authored and Sanne committed Feb 9, 2021
1 parent 77467d0 commit 77a847c
Showing 1 changed file with 24 additions and 25 deletions.
Expand Up @@ -31,15 +31,6 @@ public class PooledLoThreadLocalOptimizer extends AbstractOptimizer {
PooledLoOptimizer.class.getName()
);

private static class GenerationState {
// last value read from db source
private IntegralDataTypeHolder lastSourceValue;
// the current generator value
private IntegralDataTypeHolder value;
// the value at which we'll hit the db again
private IntegralDataTypeHolder upperLimitValue;
}

private final ThreadLocal<GenerationState> singleTenantState = ThreadLocal.withInitial( GenerationState::new );
private final ThreadLocal<Map<String, GenerationState>> multiTenantStates = ThreadLocal.withInitial( HashMap::new );

Expand All @@ -59,22 +50,8 @@ public PooledLoThreadLocalOptimizer(Class returnClass, int incrementSize) {

@Override
public Serializable generate(AccessCallback callback) {
final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() );
return generate( generationState, callback );
}

private Serializable generate(GenerationState generationState, AccessCallback callback) {
if ( generationState.value == null
|| !generationState.value.lt( generationState.upperLimitValue ) ) {
generationState.lastSourceValue = callback.getNextValue();
generationState.upperLimitValue = generationState.lastSourceValue.copy().add( incrementSize );
generationState.value = generationState.lastSourceValue.copy();
// handle cases where initial-value is less that one (hsqldb for instance).
while ( generationState.value.lt( 1 ) ) {
generationState.value.increment();
}
}
return generationState.value.makeValueThenIncrement();
return locateGenerationState( callback.getTenantIdentifier() )
.generate( callback, incrementSize );
}

private GenerationState locateGenerationState(String tenantIdentifier) {
Expand Down Expand Up @@ -112,4 +89,26 @@ public IntegralDataTypeHolder getLastSourceValue() {
public boolean applyIncrementSizeToSourceValues() {
return true;
}

private static class GenerationState {
// last value read from db source
private IntegralDataTypeHolder lastSourceValue;
// the current generator value
private IntegralDataTypeHolder value;
// the value at which we'll hit the db again
private IntegralDataTypeHolder upperLimitValue;

private Serializable generate(AccessCallback callback, int incrementSize) {
if ( value == null || !value.lt( upperLimitValue ) ) {
lastSourceValue = callback.getNextValue();
upperLimitValue = lastSourceValue.copy().add( incrementSize );
value = lastSourceValue.copy();
// handle cases where initial-value is less that one (hsqldb for instance).
while ( value.lt( 1 ) ) {
value.increment();
}
}
return value.makeValueThenIncrement();
}
}
}

0 comments on commit 77a847c

Please sign in to comment.