Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static void handleSequenceGenerator(
else if ( nameFromGeneratedValue != null ) {
properties.put( GENERATOR_NAME, nameFromGeneratedValue );
}
// we need to better handle default allocation-size here...
// we need to better handle the default allocation size here
properties.put( INCREMENT_PARAM, fallbackAllocationSize( generatorAnnotation, buildingContext ) );
},
generatorAnnotation == null
Expand Down Expand Up @@ -221,7 +221,7 @@ public static void handleTableGenerator(
else if ( nameFromGeneratedValue != null ) {
properties.put( GENERATOR_NAME, nameFromGeneratedValue );
}
// we need to better handle default allocation-size here...
// we need to better handle the default allocation size here
properties.put(
INCREMENT_PARAM,
fallbackAllocationSize( generatorAnnotation, buildingContext )
Expand All @@ -247,7 +247,7 @@ public static void handleIdGeneratorType(
final var markerAnnotation =
generatorAnnotation.annotationType().getAnnotation( IdGeneratorType.class );
idValue.setCustomIdGeneratorCreator( creationContext -> {
final Generator identifierGenerator =
final var identifierGenerator =
instantiateGenerator( beanContainer( buildingContext ), markerAnnotation.value() );
prepareForUse(
identifierGenerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public static void callConfigure(
Map<String, Object> configuration,
Value value) {
if ( generator instanceof Configurable configurable ) {
final Properties parameters = collectParameters(
final var parameters = collectParameters(
value,
creationContext.getDatabase().getDialect(),
creationContext.getRootClass(),
Expand Down Expand Up @@ -762,9 +762,9 @@ private static void instantiateNamedStrategyGenerator(
String generatorStrategy,
Map<String, Object> configuration,
MetadataBuildingContext context) {
final BeanContainer beanContainer = beanContainer( context );
final var beanContainer = beanContainer( context );
identifierValue.setCustomIdGeneratorCreator( creationContext -> {
final Generator identifierGenerator =
final var identifierGenerator =
instantiateGenerator( beanContainer, generatorClass( generatorStrategy, identifierValue ) );
// in this code path, there's no generator annotation,
// and therefore no need to call initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.hibernate.boot.model.relational;

import java.util.Arrays;

/**
* A general SQL command to be used while initializing a schema.
*
Expand All @@ -14,4 +16,15 @@ public record InitCommand(String... initCommands) {
public String[] getInitCommands() {
return initCommands;
}

@Override
public boolean equals(Object object) {
return object instanceof InitCommand that
&& Arrays.equals( this.initCommands, that.initCommands );
}

@Override
public int hashCode() {
return Arrays.hashCode( initCommands );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public interface MappingSettings {
String KEYWORD_AUTO_QUOTING_ENABLED = "hibernate.auto_quote_keyword";

/**
* When a generator specifies an increment-size and an optimizer was not explicitly
* When a generator specifies an increment size and an optimizer was not explicitly
* specified, which of the "pooled" optimizers should be preferred? Can specify an
* optimizer short name or the name of a class which implements
* {@link org.hibernate.id.enhanced.Optimizer}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public boolean sometimesNeedsStartingValue() {
public String getDropSequenceString(String sequenceName) throws MappingException {
return "drop sequence " + (supportsIfExists ? "if exists " : "") + sequenceName;
}

@Override
public String getRestartSequenceString(String sequenceName, long startWith) {
return "alter sequence " + sequenceName + " restart start with " + startWith;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ default String[] getCreateSequenceStrings(String sequenceName, int initialValue,
}

/**
* Typically dialects which support sequences can create a sequence with
* Typically, dialects which support sequences can create a sequence with
* a single command. This method is a convenience making it easier to
* implement {@link #getCreateSequenceStrings(String,int,int)} for these
* dialects.
Expand All @@ -169,7 +169,7 @@ default String getCreateSequenceString(String sequenceName) throws MappingExcept
}

/**
* Typically dialects which support sequences can create a sequence with
* Typically, dialects which support sequences can create a sequence with
* a single command. This method is a convenience making it easier to
* implement {@link #getCreateSequenceStrings(String,int,int)} for these
* dialects.
Expand Down Expand Up @@ -212,9 +212,9 @@ default String[] getDropSequenceStrings(String sequenceName) throws MappingExcep
}

/**
* Typically dialects which support sequences can drop a sequence
* with a single command. This is convenience form of
* {@link #getDropSequenceStrings} to help facilitate that.
* Typically, dialects which support sequences can drop a sequence
* with a single command. This is a convenience form of
* {@link #getDropSequenceStrings} which facilitates that.
* <p>
* Dialects which support sequences and can drop a sequence in a
* single command need *only* override this method. Dialects
Expand All @@ -229,6 +229,17 @@ default String getDropSequenceString(String sequenceName) throws MappingExceptio
return "drop sequence " + sequenceName;
}

/**
* A DDL statement to restart a sequence with a given value.
*
* @param sequenceName The name of the sequence
* @param startWith The value to restart at
* @return The {@code alter sequence ... restart} command
*/
default String getRestartSequenceString(String sequenceName, long startWith) {
return "alter sequence " + sequenceName + " restart with " + startWith;
}

/**
* Do we need to explicitly specify {@code minvalue} or
* {@code maxvalue} when the initial value doesn't have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.Value;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

Expand Down Expand Up @@ -64,6 +65,11 @@ public interface GeneratorCreationContext {
*/
Property getProperty();

/**
* The identifier.
*/
Value getValue();

/**
* Mapping details for the identifier type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.mapping.Table;

/**
* Encapsulates definition of the underlying data structure backing a
* sequence-style generator.
* Encapsulates definition of the underlying data structure backing
* a {@linkplain SequenceStyleGenerator sequence-style} generator.
*
* @author Steve Ebersole
*/
Expand Down Expand Up @@ -88,6 +89,21 @@ default void configure(Optimizer optimizer) {
@Override
void registerExportables(Database database);

/**
* Register additional database objects which need to be aware of the
* table for which this structure is used to generate values. Used to
* deal with automatic sequence resynchronization after data import.
*
* @param table The table for which this structure is used to generate values
* @param optimizer The {@link Optimizer} for this generator
*
* @see org.hibernate.relational.SchemaManager#resynchronizeGenerators()
*
* @since 7.2
*/
default void registerExtraExportables(Table table, Optimizer optimizer) {
}

/**
* Initializes this structure, in particular pre-generates SQL as necessary.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class ExportableColumnHelper {

static Column column(Database database, Table table, String segmentColumnName, BasicType<?> type, String typeName) {
final Column column = new Column( segmentColumnName );
final var column = new Column( segmentColumnName );
column.setSqlType( typeName );
column.setValue( new Value() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ else if ( ! generationState.upperLimit.gt( generationState.value ) ) {
private GenerationState noTenantState;
private Map<String,GenerationState> tenantSpecificState;

private GenerationState locateGenerationState(String tenantIdentifier) {
if ( tenantIdentifier == null ) {
private GenerationState locateGenerationState(String tenantId) {
if ( tenantId == null ) {
if ( noTenantState == null ) {
noTenantState = new GenerationState();
}
return noTenantState;
}
else {
GenerationState state;
if ( tenantSpecificState == null ) {
tenantSpecificState = new ConcurrentHashMap<>();
state = new GenerationState();
tenantSpecificState.put( tenantIdentifier, state );
final var state = new GenerationState();
tenantSpecificState.put( tenantId, state );
return state;
}
else {
state = tenantSpecificState.get( tenantIdentifier );
var state = tenantSpecificState.get( tenantId );
if ( state == null ) {
state = new GenerationState();
tenantSpecificState.put( tenantIdentifier, state );
tenantSpecificState.put( tenantId, state );
}
return state;
}
return state;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.id.IntegralDataTypeHolder;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.query.sqm.BinaryArithmeticOperator;
import org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression;
import org.hibernate.sql.ast.tree.expression.Expression;
Expand Down Expand Up @@ -59,8 +58,7 @@ public LegacyHiLoAlgorithmOptimizer(Class<?> returnClass, int incrementSize) {
public Serializable generate(AccessCallback callback) {
lock.lock();
try {
final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() );

final var generationState = locateGenerationState( callback.getTenantIdentifier() );
if ( generationState.lo > generationState.maxLo ) {
generationState.lastSourceValue = callback.getNextValue();
generationState.lo = generationState.lastSourceValue.eq( 0 ) ? 1 : 0;
Expand Down Expand Up @@ -90,25 +88,25 @@ private GenerationState locateGenerationState(String tenantIdentifier) {
return noTenantState;
}
else {
GenerationState state;
if ( tenantSpecificState == null ) {
tenantSpecificState = new ConcurrentHashMap<>();
state = createGenerationState();
final var state = createGenerationState();
tenantSpecificState.put( tenantIdentifier, state );
return state;
}
else {
state = tenantSpecificState.get( tenantIdentifier );
var state = tenantSpecificState.get( tenantIdentifier );
if ( state == null ) {
state = createGenerationState();
tenantSpecificState.put( tenantIdentifier, state );
}
return state;
}
return state;
}
}

private GenerationState createGenerationState() {
final GenerationState state = new GenerationState();
final var state = new GenerationState();
state.maxLo = initialMaxLo;
state.lo = initialMaxLo + 1;
return state;
Expand Down Expand Up @@ -156,7 +154,7 @@ public IntegralDataTypeHolder getLastValue() {

@Override
public Expression createLowValueExpression(Expression databaseValue, SessionFactoryImplementor sessionFactory) {
BasicValuedMapping integerType = sessionFactory.getTypeConfiguration().getBasicTypeForJavaType( Integer.class );
final var integerType = sessionFactory.getTypeConfiguration().getBasicTypeForJavaType( Integer.class );
return new BinaryArithmeticExpression(
databaseValue,
BinaryArithmeticOperator.MULTIPLY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.boot.model.relational.QualifiedName;
import org.hibernate.boot.model.relational.QualifiedNameParser;
import org.hibernate.boot.model.relational.QualifiedSequenceName;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.service.ServiceRegistry;

Expand Down Expand Up @@ -64,18 +65,16 @@ public QualifiedName determineSequenceName(
Map<?, ?> configValues,
ServiceRegistry serviceRegistry) {
final String sequenceName = implicitSequenceName( configValues );
return sequenceName.contains( "." )
? QualifiedNameParser.INSTANCE.parse( sequenceName )
: new QualifiedSequenceName(
catalogName,
schemaName,
serviceRegistry.requireService( JdbcEnvironment.class )
.getIdentifierHelper()
.toIdentifier( sequenceName )
);

if ( sequenceName.contains( "." ) ) {
return QualifiedNameParser.INSTANCE.parse( sequenceName );
}

return new QualifiedSequenceName(
catalogName,
schemaName,
serviceRegistry.requireService( JdbcEnvironment.class )
.getIdentifierHelper()
.toIdentifier( sequenceName )
);
}

private String implicitSequenceName(Map<?, ?> configValues) {
Expand Down Expand Up @@ -104,17 +103,15 @@ public QualifiedName determineTableName(
ServiceRegistry serviceRegistry) {
final String implicitName = implicitTableName( configValues );

if ( implicitName.contains( "." ) ) {
return QualifiedNameParser.INSTANCE.parse( implicitName );
}

return new QualifiedNameParser.NameParts(
catalogName,
schemaName,
serviceRegistry.requireService( JdbcEnvironment.class )
.getIdentifierHelper()
.toIdentifier( implicitName )
);
return implicitName.contains( "." )
? QualifiedNameParser.INSTANCE.parse( implicitName )
: new QualifiedTableName(
catalogName,
schemaName,
serviceRegistry.requireService( JdbcEnvironment.class )
.getIdentifierHelper()
.toIdentifier( implicitName )
);
}

private String implicitTableName(Map<?, ?> configValues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Serializable generate(AccessCallback callback) {
// reliable as it might be mutated by multiple threads.
// The lastSourceValue field is only accessed by tests,
// so this is not a concern.
IntegralDataTypeHolder value = callback.getNextValue();
final var value = callback.getNextValue();
lastSourceValue = value;
return value.makeValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface Optimizer {
*
* @return True if the values in the source are to be incremented
* according to the defined increment size; false otherwise, in which
* case the increment is totally an in memory construct.
* case the increment size is a completely in-memory construct.
*/
boolean applyIncrementSizeToSourceValues();

Expand All @@ -75,4 +75,11 @@ public interface Optimizer {
* @since 7.1
*/
Expression createLowValueExpression(Expression databaseValue, SessionFactoryImplementor sessionFactory);

/**
* @since 7.2
*/
default int getAdjustment() {
return 1;
}
}
Loading