|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive.loader.ast.internal; |
| 7 | + |
| 8 | +import java.lang.reflect.Array; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.concurrent.CompletionStage; |
| 11 | + |
| 12 | +import org.hibernate.LockOptions; |
| 13 | +import org.hibernate.collection.spi.PersistentCollection; |
| 14 | +import org.hibernate.engine.spi.CollectionKey; |
| 15 | +import org.hibernate.engine.spi.LoadQueryInfluencers; |
| 16 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 17 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 18 | +import org.hibernate.engine.spi.SubselectFetch; |
| 19 | +import org.hibernate.loader.ast.internal.LoaderSelectBuilder; |
| 20 | +import org.hibernate.loader.ast.internal.MultiKeyLoadHelper; |
| 21 | +import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader; |
| 22 | +import org.hibernate.metamodel.mapping.JdbcMapping; |
| 23 | +import org.hibernate.metamodel.mapping.PluralAttributeMapping; |
| 24 | +import org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor; |
| 25 | +import org.hibernate.query.spi.QueryOptions; |
| 26 | +import org.hibernate.reactive.sql.exec.internal.StandardReactiveSelectExecutor; |
| 27 | +import org.hibernate.reactive.sql.results.spi.ReactiveListResultsConsumer; |
| 28 | +import org.hibernate.reactive.util.impl.CompletionStages; |
| 29 | +import org.hibernate.sql.ast.tree.expression.JdbcParameter; |
| 30 | +import org.hibernate.sql.ast.tree.select.SelectStatement; |
| 31 | +import org.hibernate.sql.exec.internal.JdbcParameterBindingImpl; |
| 32 | +import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl; |
| 33 | +import org.hibernate.sql.exec.internal.JdbcParameterImpl; |
| 34 | +import org.hibernate.sql.exec.spi.JdbcOperationQuerySelect; |
| 35 | +import org.hibernate.sql.exec.spi.JdbcParameterBindings; |
| 36 | +import org.hibernate.sql.results.internal.RowTransformerStandardImpl; |
| 37 | +import org.hibernate.type.BasicType; |
| 38 | + |
| 39 | +import static org.hibernate.loader.ast.internal.MultiKeyLoadLogging.MULTI_KEY_LOAD_DEBUG_ENABLED; |
| 40 | +import static org.hibernate.loader.ast.internal.MultiKeyLoadLogging.MULTI_KEY_LOAD_LOGGER; |
| 41 | + |
| 42 | +/** |
| 43 | + * @see org.hibernate.loader.ast.internal.CollectionBatchLoaderArrayParam |
| 44 | + */ |
| 45 | +public class ReactiveCollectionBatchLoaderArrayParam extends ReactiveAbstractCollectionBatchLoader |
| 46 | + implements SqlArrayMultiKeyLoader { |
| 47 | + |
| 48 | + private final Class<?> arrayElementType; |
| 49 | + private final JdbcMapping arrayJdbcMapping; |
| 50 | + private final JdbcParameter jdbcParameter; |
| 51 | + private final SelectStatement sqlSelect; |
| 52 | + private final JdbcOperationQuerySelect jdbcSelectOperation; |
| 53 | + |
| 54 | + public ReactiveCollectionBatchLoaderArrayParam( |
| 55 | + int domainBatchSize, |
| 56 | + LoadQueryInfluencers loadQueryInfluencers, |
| 57 | + PluralAttributeMapping attributeMapping, |
| 58 | + SessionFactoryImplementor sessionFactory) { |
| 59 | + super( domainBatchSize, loadQueryInfluencers, attributeMapping, sessionFactory ); |
| 60 | + |
| 61 | + if ( MULTI_KEY_LOAD_DEBUG_ENABLED ) { |
| 62 | + MULTI_KEY_LOAD_LOGGER.debugf( |
| 63 | + "Using ARRAY batch fetching strategy for collection `%s` : %s", |
| 64 | + attributeMapping.getNavigableRole().getFullPath(), |
| 65 | + domainBatchSize |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + final SimpleForeignKeyDescriptor keyDescriptor = (SimpleForeignKeyDescriptor) getLoadable().getKeyDescriptor(); |
| 70 | + |
| 71 | + arrayElementType = keyDescriptor.getJavaType().getJavaTypeClass(); |
| 72 | + Class<?> arrayClass = Array.newInstance( arrayElementType, 0 ).getClass(); |
| 73 | + |
| 74 | + final BasicType<?> arrayBasicType = getSessionFactory().getTypeConfiguration() |
| 75 | + .getBasicTypeRegistry() |
| 76 | + .getRegisteredType( arrayClass ); |
| 77 | + arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping( |
| 78 | + arrayBasicType, |
| 79 | + keyDescriptor.getJdbcMapping(), |
| 80 | + arrayClass, |
| 81 | + getSessionFactory() |
| 82 | + ); |
| 83 | + |
| 84 | + jdbcParameter = new JdbcParameterImpl( arrayJdbcMapping ); |
| 85 | + sqlSelect = LoaderSelectBuilder.createSelectBySingleArrayParameter( |
| 86 | + getLoadable(), |
| 87 | + keyDescriptor.getKeyPart(), |
| 88 | + getInfluencers(), |
| 89 | + LockOptions.NONE, |
| 90 | + jdbcParameter, |
| 91 | + getSessionFactory() |
| 92 | + ); |
| 93 | + |
| 94 | + jdbcSelectOperation = getSessionFactory().getJdbcServices() |
| 95 | + .getJdbcEnvironment() |
| 96 | + .getSqlAstTranslatorFactory() |
| 97 | + .buildSelectTranslator( getSessionFactory(), sqlSelect ) |
| 98 | + .translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE ); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public CompletionStage<PersistentCollection<?>> reactiveLoad(Object key, SharedSessionContractImplementor session) { |
| 103 | + if ( MULTI_KEY_LOAD_DEBUG_ENABLED ) { |
| 104 | + MULTI_KEY_LOAD_LOGGER.debugf( |
| 105 | + "Batch loading entity `%s#%s`", |
| 106 | + getLoadable().getNavigableRole().getFullPath(), |
| 107 | + key |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + final Object[] keysToInitialize = resolveKeysToInitialize( key, session ); |
| 112 | + return initializeKeys( keysToInitialize, session ) |
| 113 | + .thenApply( v -> { |
| 114 | + for ( int i = 0; i < keysToInitialize.length; i++ ) { |
| 115 | + finishInitializingKey( keysToInitialize[i], session ); |
| 116 | + } |
| 117 | + |
| 118 | + final CollectionKey collectionKey = new CollectionKey( |
| 119 | + getLoadable().getCollectionDescriptor(), |
| 120 | + key |
| 121 | + ); |
| 122 | + return session.getPersistenceContext().getCollection( collectionKey ); |
| 123 | + } ); |
| 124 | + } |
| 125 | + |
| 126 | + private Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) { |
| 127 | + final Object[] keysToInitialize = (Object[]) Array.newInstance( arrayElementType, getDomainBatchSize() ); |
| 128 | + session.getPersistenceContextInternal().getBatchFetchQueue().collectBatchLoadableCollectionKeys( |
| 129 | + getDomainBatchSize(), |
| 130 | + (index, value) -> keysToInitialize[index] = value, |
| 131 | + keyBeingLoaded, |
| 132 | + getLoadable() |
| 133 | + ); |
| 134 | + return keysToInitialize; |
| 135 | + } |
| 136 | + |
| 137 | + private CompletionStage<Void> initializeKeys(Object[] keysToInitialize, SharedSessionContractImplementor session) { |
| 138 | + assert jdbcSelectOperation != null; |
| 139 | + assert jdbcParameter != null; |
| 140 | + |
| 141 | + final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl( 1 ); |
| 142 | + jdbcParameterBindings |
| 143 | + .addBinding( jdbcParameter, new JdbcParameterBindingImpl( arrayJdbcMapping, keysToInitialize ) ); |
| 144 | + |
| 145 | + final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler( |
| 146 | + session.getPersistenceContext().getBatchFetchQueue(), |
| 147 | + sqlSelect, |
| 148 | + Collections.singletonList( jdbcParameter ), |
| 149 | + jdbcParameterBindings |
| 150 | + ); |
| 151 | + |
| 152 | + return StandardReactiveSelectExecutor.INSTANCE.list( |
| 153 | + jdbcSelectOperation, |
| 154 | + jdbcParameterBindings, |
| 155 | + new SingleIdExecutionContext( |
| 156 | + null, |
| 157 | + null, |
| 158 | + null, |
| 159 | + LockOptions.NONE, |
| 160 | + subSelectFetchableKeysHandler, |
| 161 | + session |
| 162 | + ), |
| 163 | + RowTransformerStandardImpl.instance(), |
| 164 | + ReactiveListResultsConsumer.UniqueSemantic.FILTER |
| 165 | + ).thenCompose( CompletionStages::voidFuture ); |
| 166 | + } |
| 167 | + |
| 168 | + public void prepare() { |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments