|
| 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.persister.entity.mutation; |
| 7 | + |
| 8 | +import java.lang.invoke.MethodHandles; |
| 9 | +import java.util.concurrent.CompletableFuture; |
| 10 | +import java.util.concurrent.CompletionStage; |
| 11 | + |
| 12 | +import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; |
| 13 | +import org.hibernate.engine.jdbc.mutation.MutationExecutor; |
| 14 | +import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; |
| 15 | +import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService; |
| 16 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 17 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 18 | +import org.hibernate.persister.entity.AbstractEntityPersister; |
| 19 | +import org.hibernate.persister.entity.mutation.DeleteCoordinatorSoft; |
| 20 | +import org.hibernate.persister.entity.mutation.EntityTableMapping; |
| 21 | +import org.hibernate.reactive.adaptor.impl.PrepareStatementDetailsAdaptor; |
| 22 | +import org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor; |
| 23 | +import org.hibernate.reactive.engine.jdbc.env.internal.ReactiveMutationExecutor; |
| 24 | +import org.hibernate.reactive.logging.impl.Log; |
| 25 | +import org.hibernate.reactive.logging.impl.LoggerFactory; |
| 26 | +import org.hibernate.sql.model.MutationOperation; |
| 27 | +import org.hibernate.sql.model.MutationOperationGroup; |
| 28 | + |
| 29 | +import static org.hibernate.engine.jdbc.mutation.internal.ModelMutationHelper.identifiedResultsCheck; |
| 30 | +import static org.hibernate.reactive.util.impl.CompletionStages.failedFuture; |
| 31 | +import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture; |
| 32 | + |
| 33 | +public class ReactiveDeleteCoordinatorSoft extends DeleteCoordinatorSoft implements ReactiveAbstractDeleteCoordinator { |
| 34 | + |
| 35 | + private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); |
| 36 | + |
| 37 | + private CompletionStage<Void> stage; |
| 38 | + |
| 39 | + public ReactiveDeleteCoordinatorSoft( |
| 40 | + AbstractEntityPersister entityPersister, |
| 41 | + SessionFactoryImplementor factory) { |
| 42 | + super( entityPersister, factory ); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void delete(Object entity, Object id, Object version, SharedSessionContractImplementor session) { |
| 47 | + throw LOG.nonReactiveMethodCall( "coordinateReactiveDelete" ); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public CompletionStage<Void> reactiveDelete(Object entity, Object id, Object version, SharedSessionContractImplementor session) { |
| 52 | + try { |
| 53 | + super.delete( entity, id, version, session ); |
| 54 | + return stage != null ? stage : voidFuture(); |
| 55 | + } |
| 56 | + catch (Throwable t) { |
| 57 | + if ( stage == null ) { |
| 58 | + return failedFuture( t ); |
| 59 | + } |
| 60 | + stage.toCompletableFuture().completeExceptionally( t ); |
| 61 | + return stage; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void doDynamicDelete(Object entity, Object id, Object rowId, Object[] loadedState, SharedSessionContractImplementor session) { |
| 67 | + stage = new CompletableFuture<>(); |
| 68 | + final MutationOperationGroup operationGroup = generateOperationGroup( null, loadedState, true, session ); |
| 69 | + final ReactiveMutationExecutor mutationExecutor = mutationExecutor( session, operationGroup ); |
| 70 | + |
| 71 | + for ( int i = 0; i < operationGroup.getNumberOfOperations(); i++ ) { |
| 72 | + final MutationOperation mutation = operationGroup.getOperation( i ); |
| 73 | + if ( mutation != null ) { |
| 74 | + final String tableName = mutation.getTableDetails().getTableName(); |
| 75 | + mutationExecutor.getPreparedStatementDetails( tableName ); |
| 76 | + } |
| 77 | + } |
| 78 | + applyDynamicDeleteTableDetails( id, rowId, loadedState, mutationExecutor, operationGroup, session ); |
| 79 | + mutationExecutor.executeReactive( |
| 80 | + entity, |
| 81 | + null, |
| 82 | + null, |
| 83 | + (statementDetails, affectedRowCount, batchPosition) -> identifiedResultsCheck( |
| 84 | + statementDetails, |
| 85 | + affectedRowCount, |
| 86 | + batchPosition, |
| 87 | + entityPersister(), |
| 88 | + id, |
| 89 | + factory() |
| 90 | + ), |
| 91 | + session |
| 92 | + ) |
| 93 | + .whenComplete( (o, t) -> mutationExecutor.release() ) |
| 94 | + .whenComplete( this::complete ); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected void applyId( |
| 99 | + Object id, |
| 100 | + Object rowId, |
| 101 | + MutationExecutor mutationExecutor, |
| 102 | + MutationOperationGroup operationGroup, |
| 103 | + SharedSessionContractImplementor session) { |
| 104 | + final JdbcValueBindings jdbcValueBindings = mutationExecutor.getJdbcValueBindings(); |
| 105 | + |
| 106 | + for ( int position = 0; position < operationGroup.getNumberOfOperations(); position++ ) { |
| 107 | + final MutationOperation jdbcMutation = operationGroup.getOperation( position ); |
| 108 | + final EntityTableMapping tableDetails = (EntityTableMapping) jdbcMutation.getTableDetails(); |
| 109 | + breakDownKeyJdbcValues( id, rowId, session, jdbcValueBindings, tableDetails ); |
| 110 | + final PreparedStatementDetails statementDetails = mutationExecutor.getPreparedStatementDetails( tableDetails.getTableName() ); |
| 111 | + if ( statementDetails != null ) { |
| 112 | + PreparedStatementAdaptor.bind( statement -> { |
| 113 | + PrepareStatementDetailsAdaptor detailsAdaptor = new PrepareStatementDetailsAdaptor( |
| 114 | + statementDetails, |
| 115 | + statement, |
| 116 | + session.getJdbcServices() |
| 117 | + ); |
| 118 | + // force creation of the PreparedStatement |
| 119 | + //noinspection resource |
| 120 | + detailsAdaptor.resolveStatement(); |
| 121 | + } ); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected void doStaticDelete(Object entity, Object id, Object rowId, Object[] loadedState, Object version, SharedSessionContractImplementor session) { |
| 128 | + stage = new CompletableFuture<>(); |
| 129 | + final boolean applyVersion = entity != null; |
| 130 | + final MutationOperationGroup operationGroupToUse = entity == null |
| 131 | + ? resolveNoVersionDeleteGroup( session ) |
| 132 | + : getStaticMutationOperationGroup(); |
| 133 | + |
| 134 | + final ReactiveMutationExecutor mutationExecutor = mutationExecutor( session, operationGroupToUse ); |
| 135 | + for ( int position = 0; position < getStaticMutationOperationGroup().getNumberOfOperations(); position++ ) { |
| 136 | + final MutationOperation mutation = getStaticMutationOperationGroup().getOperation( position ); |
| 137 | + if ( mutation != null ) { |
| 138 | + mutationExecutor.getPreparedStatementDetails( mutation.getTableDetails().getTableName() ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if ( applyVersion ) { |
| 143 | + applyLocking( version, null, mutationExecutor, session ); |
| 144 | + } |
| 145 | + final JdbcValueBindings jdbcValueBindings = mutationExecutor.getJdbcValueBindings(); |
| 146 | + bindPartitionColumnValueBindings( loadedState, session, jdbcValueBindings ); |
| 147 | + applyId( id, rowId, mutationExecutor, getStaticMutationOperationGroup(), session ); |
| 148 | + mutationExecutor.executeReactive( |
| 149 | + entity, |
| 150 | + null, |
| 151 | + null, |
| 152 | + (statementDetails, affectedRowCount, batchPosition) -> identifiedResultsCheck( |
| 153 | + statementDetails, |
| 154 | + affectedRowCount, |
| 155 | + batchPosition, |
| 156 | + entityPersister(), |
| 157 | + id, |
| 158 | + factory() |
| 159 | + ), |
| 160 | + session |
| 161 | + ) |
| 162 | + .thenAccept( v -> mutationExecutor.release() ) |
| 163 | + .whenComplete( this::complete ); |
| 164 | + } |
| 165 | + |
| 166 | + private void complete(Object o, Throwable throwable) { |
| 167 | + if ( throwable != null ) { |
| 168 | + stage.toCompletableFuture().completeExceptionally( throwable ); |
| 169 | + } |
| 170 | + else { |
| 171 | + stage.toCompletableFuture().complete( null ); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private ReactiveMutationExecutor mutationExecutor( |
| 176 | + SharedSessionContractImplementor session, |
| 177 | + MutationOperationGroup operationGroup) { |
| 178 | + final MutationExecutorService mutationExecutorService = session |
| 179 | + .getFactory() |
| 180 | + .getServiceRegistry() |
| 181 | + .getService( MutationExecutorService.class ); |
| 182 | + |
| 183 | + return (ReactiveMutationExecutor) mutationExecutorService |
| 184 | + .createExecutor( this::getBatchKey, operationGroup, session ); |
| 185 | + } |
| 186 | +} |
0 commit comments