Skip to content

Commit bba51d7

Browse files
committed
cleanups and sync with core for CollectionActions
1 parent 7f42111 commit bba51d7

File tree

3 files changed

+65
-158
lines changed

3 files changed

+65
-158
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/ReactiveCollectionRecreateAction.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ public ReactiveCollectionRecreateAction(final PersistentCollection collection, f
2929

3030
@Override
3131
public CompletionStage<Void> reactiveExecute() {
32-
final ReactiveCollectionPersister persister = (ReactiveCollectionPersister) getPersister();
33-
final SharedSessionContractImplementor session = getSession();
34-
final Object key = getKey();
35-
32+
// this method is called when a new non-null collection is persisted
33+
// or when an existing (non-null) collection is moved to a new owner
3634
final PersistentCollection<?> collection = getCollection();
37-
3835
preRecreate();
39-
36+
final SharedSessionContractImplementor session = getSession();
37+
final ReactiveCollectionPersister persister = (ReactiveCollectionPersister) getPersister();
4038
return persister
41-
.reactiveRecreate( collection, key, session )
39+
.reactiveRecreate( collection, getKey(), session )
4240
.thenAccept( v -> {
4341
// FIXME: I think we could move everything in a method reference call
4442
session.getPersistenceContextInternal().getCollectionEntry( collection ).afterAction( collection );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/ReactiveCollectionRemoveAction.java

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@
1212
import org.hibernate.action.internal.CollectionAction;
1313
import org.hibernate.collection.spi.PersistentCollection;
1414
import org.hibernate.engine.spi.SharedSessionContractImplementor;
15-
import org.hibernate.event.service.spi.EventListenerGroup;
1615
import org.hibernate.event.spi.EventSource;
17-
import org.hibernate.event.spi.PostCollectionRecreateEvent;
18-
import org.hibernate.event.spi.PostCollectionRecreateEventListener;
1916
import org.hibernate.event.spi.PostCollectionRemoveEvent;
2017
import org.hibernate.event.spi.PostCollectionRemoveEventListener;
21-
import org.hibernate.event.spi.PreCollectionRecreateEvent;
22-
import org.hibernate.event.spi.PreCollectionRecreateEventListener;
2318
import org.hibernate.event.spi.PreCollectionRemoveEvent;
2419
import org.hibernate.event.spi.PreCollectionRemoveEventListener;
2520
import org.hibernate.persister.collection.CollectionPersister;
2621
import org.hibernate.reactive.engine.ReactiveExecutable;
2722
import org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister;
28-
import org.hibernate.reactive.util.impl.CompletionStages;
2923
import org.hibernate.stat.spi.StatisticsImplementor;
3024

25+
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
26+
3127
public class ReactiveCollectionRemoveAction extends CollectionAction implements ReactiveExecutable {
3228
private final Object affectedOwner;
3329
private final boolean emptySnapshot;
@@ -68,47 +64,33 @@ public ReactiveCollectionRemoveAction(
6864

6965
@Override
7066
public CompletionStage<Void> reactiveExecute() {
71-
final Object key = getKey();
72-
final SharedSessionContractImplementor session = getSession();
73-
final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister) getPersister();
74-
final CollectionPersister corePersister = getPersister();
75-
final PersistentCollection collection = getCollection();
76-
final StatisticsImplementor statistics = session.getFactory().getStatistics();
67+
preRemove();
7768

78-
CompletionStage<Void> removeStage = CompletionStages.voidFuture();
79-
80-
if ( !emptySnapshot ) {
69+
final SharedSessionContractImplementor session = getSession();
70+
CompletionStage<Void> removeStage;
71+
if ( emptySnapshot ) {
72+
removeStage = voidFuture();
73+
}
74+
else {
75+
final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister) getPersister();
8176
// an existing collection that was either non-empty or uninitialized
8277
// is replaced by null or a different collection
8378
// (if the collection is uninitialized, hibernate has no way of
8479
// knowing if the collection is actually empty without querying the db)
85-
removeStage = removeStage.thenAccept( v -> preRemove() )
86-
.thenCompose( v -> reactivePersister
87-
.reactiveRemove( key, session )
88-
.thenAccept( ignore -> {
89-
evict();
90-
postRemove();
91-
})
92-
);
80+
removeStage = reactivePersister.reactiveRemove( getKey(), session );
9381
}
94-
if( collection != null ) {
95-
return removeStage.thenAccept(v -> {
82+
return removeStage.thenAccept( v -> {
83+
final PersistentCollection<?> collection = getCollection();
84+
if ( collection != null ) {
9685
session.getPersistenceContextInternal().getCollectionEntry( collection ).afterAction( collection );
97-
evict();
98-
postRemove();
99-
if ( statistics.isStatisticsEnabled() ) {
100-
statistics.updateCollection( corePersister.getRole() );
101-
}
102-
} );
103-
}
104-
return removeStage.thenAccept(v -> {
86+
}
10587
evict();
10688
postRemove();
89+
final StatisticsImplementor statistics = session.getFactory().getStatistics();
10790
if ( statistics.isStatisticsEnabled() ) {
108-
statistics.updateCollection( corePersister.getRole() );
91+
statistics.updateCollection( getPersister().getRole() );
10992
}
11093
} );
111-
11294
}
11395

11496
@Override
@@ -118,57 +100,32 @@ public void execute() throws HibernateException {
118100
}
119101

120102
private void preRemove() {
121-
final EventListenerGroup<PreCollectionRemoveEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_PRE_COLLECTION_REMOVE;
122-
if ( listenerGroup.isEmpty() ) {
123-
return;
124-
}
125-
final PreCollectionRemoveEvent event = new PreCollectionRemoveEvent(
103+
getFastSessionServices().eventListenerGroup_PRE_COLLECTION_REMOVE
104+
.fireLazyEventOnEachListener( this::newPreCollectionRemoveEvent,
105+
PreCollectionRemoveEventListener::onPreRemoveCollection );
106+
}
107+
108+
private PreCollectionRemoveEvent newPreCollectionRemoveEvent() {
109+
return new PreCollectionRemoveEvent(
126110
getPersister(),
127111
getCollection(),
128112
eventSource(),
129113
affectedOwner
130114
);
131-
for ( PreCollectionRemoveEventListener listener : listenerGroup.listeners() ) {
132-
listener.onPreRemoveCollection( event );
133-
}
134115
}
135116

136117
private void postRemove() {
137-
final EventListenerGroup<PostCollectionRemoveEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_POST_COLLECTION_REMOVE;
138-
if ( listenerGroup.isEmpty() ) {
139-
return;
140-
}
141-
final PostCollectionRemoveEvent event = new PostCollectionRemoveEvent(
118+
getFastSessionServices().eventListenerGroup_POST_COLLECTION_REMOVE
119+
.fireLazyEventOnEachListener( this::newPostCollectionRemoveEvent,
120+
PostCollectionRemoveEventListener::onPostRemoveCollection );
121+
}
122+
123+
private PostCollectionRemoveEvent newPostCollectionRemoveEvent() {
124+
return new PostCollectionRemoveEvent(
142125
getPersister(),
143126
getCollection(),
144127
eventSource(),
145128
affectedOwner
146129
);
147-
for ( PostCollectionRemoveEventListener listener : listenerGroup.listeners() ) {
148-
listener.onPostRemoveCollection( event );
149-
}
150-
}
151-
152-
private void preRecreate() {
153-
final EventListenerGroup<PreCollectionRecreateEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_PRE_COLLECTION_RECREATE;
154-
if ( listenerGroup.isEmpty() ) {
155-
return;
156-
}
157-
final PreCollectionRecreateEvent event = new PreCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
158-
for ( PreCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
159-
listener.onPreRecreateCollection( event );
160-
}
161-
}
162-
163-
private void postRecreate() {
164-
final EventListenerGroup<PostCollectionRecreateEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_POST_COLLECTION_RECREATE;
165-
if ( listenerGroup.isEmpty() ) {
166-
return;
167-
}
168-
final PostCollectionRecreateEvent event = new PostCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
169-
for ( PostCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
170-
listener.onPostRecreateCollection( event );
171-
}
172130
}
173-
174131
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/ReactiveCollectionUpdateAction.java

Lines changed: 29 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,20 @@
1313
import org.hibernate.action.internal.CollectionAction;
1414
import org.hibernate.collection.spi.PersistentCollection;
1515
import org.hibernate.engine.spi.SharedSessionContractImplementor;
16-
import org.hibernate.event.service.spi.EventListenerGroup;
1716
import org.hibernate.event.spi.EventSource;
18-
import org.hibernate.event.spi.PostCollectionRecreateEvent;
19-
import org.hibernate.event.spi.PostCollectionRecreateEventListener;
2017
import org.hibernate.event.spi.PostCollectionUpdateEvent;
2118
import org.hibernate.event.spi.PostCollectionUpdateEventListener;
22-
import org.hibernate.event.spi.PreCollectionRecreateEvent;
23-
import org.hibernate.event.spi.PreCollectionRecreateEventListener;
2419
import org.hibernate.event.spi.PreCollectionUpdateEvent;
2520
import org.hibernate.event.spi.PreCollectionUpdateEventListener;
2621
import org.hibernate.persister.collection.CollectionPersister;
2722
import org.hibernate.reactive.engine.ReactiveExecutable;
2823
import org.hibernate.reactive.logging.impl.Log;
2924
import org.hibernate.reactive.logging.impl.LoggerFactory;
3025
import org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister;
31-
import org.hibernate.reactive.util.impl.CompletionStages;
3226
import org.hibernate.stat.spi.StatisticsImplementor;
3327

3428
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
29+
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
3530

3631

3732
/**
@@ -59,15 +54,13 @@ public CompletionStage<Void> reactiveExecute() {
5954
final Object key = getKey();
6055
final SharedSessionContractImplementor session = getSession();
6156
final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister) getPersister();
62-
final CollectionPersister corePersister = getPersister();
57+
final CollectionPersister persister = getPersister();
6358
final PersistentCollection collection = getCollection();
64-
final boolean affectedByFilters = corePersister.isAffectedByEnabledFilters( session );
59+
final boolean affectedByFilters = persister.isAffectedByEnabledFilters( session );
6560

6661
preUpdate();
6762

68-
CompletionStage<Void> updateStage = CompletionStages.voidFuture();
69-
70-
// And then make sure that each operations is executed in its own stage maintaining the same order as in ORM
63+
final CompletionStage<Void> updateStage;
7164
if ( !collection.wasInitialized() ) {
7265
// If there were queued operations, they would have been processed
7366
// and cleared by now.
@@ -76,53 +69,35 @@ public CompletionStage<Void> reactiveExecute() {
7669
throw new AssertionFailure( "collection is not dirty" );
7770
}
7871
//do nothing - we only need to notify the cache...
72+
updateStage = voidFuture();
7973
}
8074
else if ( !affectedByFilters && collection.empty() ) {
81-
if ( !emptySnapshot ) {
82-
updateStage = updateStage
83-
.thenCompose( v -> reactivePersister.reactiveRemove( key, session ) )
84-
.thenAccept( count -> { /* We don't care, maybe we can log it as debug */} );
85-
}
75+
updateStage = emptySnapshot ? voidFuture() : reactivePersister.reactiveRemove( key, session );
8676
}
87-
else if ( collection.needsRecreate( corePersister ) ) {
77+
else if ( collection.needsRecreate( persister ) ) {
8878
if ( affectedByFilters ) {
89-
throw LOG.cannotRecreateCollectionWhileFilterIsEnabled( collectionInfoString( corePersister, collection, key, session ) );
90-
}
91-
if ( !emptySnapshot ) {
92-
updateStage = updateStage
93-
.thenCompose( v -> reactivePersister.reactiveRemove( key, session ) )
94-
.thenAccept( count -> { /* We don't care, maybe we can log it as debug */} );
79+
throw LOG.cannotRecreateCollectionWhileFilterIsEnabled( collectionInfoString( persister, collection, key, session ) );
9580
}
96-
97-
return updateStage
98-
.thenCompose( v -> reactivePersister
99-
.reactiveRecreate( collection, key, session )
100-
.thenAccept( ignore -> {
101-
session.getPersistenceContextInternal().getCollectionEntry( collection ).afterAction( collection );
102-
evict();
103-
postUpdate();
104-
final StatisticsImplementor statistics = session.getFactory().getStatistics();
105-
if ( statistics.isStatisticsEnabled() ) {
106-
statistics.updateCollection( corePersister.getRole() );
107-
}
108-
})
109-
);
81+
updateStage = emptySnapshot
82+
? reactivePersister.reactiveRecreate( collection, key, session )
83+
: reactivePersister.reactiveRemove( key, session )
84+
.thenCompose( v -> reactivePersister.reactiveRecreate( collection, key, session ) );
11085
}
11186
else {
112-
updateStage = updateStage
87+
updateStage = voidFuture()
11388
.thenCompose( v -> reactivePersister.reactiveDeleteRows( collection, key, session ) )
11489
.thenCompose( v -> reactivePersister.reactiveUpdateRows( collection, key, session ) )
11590
.thenCompose( v -> reactivePersister.reactiveInsertRows( collection, key, session ) );
11691
}
11792

118-
return updateStage.thenAccept(v -> {
93+
return updateStage.thenAccept( v -> {
11994
session.getPersistenceContextInternal().getCollectionEntry( collection ).afterAction( collection );
12095
evict();
12196
postUpdate();
12297

12398
final StatisticsImplementor statistics = session.getFactory().getStatistics();
12499
if ( statistics.isStatisticsEnabled() ) {
125-
statistics.updateCollection( corePersister.getRole() );
100+
statistics.updateCollection( persister.getRole() );
126101
}
127102
} );
128103
}
@@ -134,54 +109,31 @@ public void execute() throws HibernateException {
134109
}
135110

136111
private void preUpdate() {
137-
final EventListenerGroup<PreCollectionUpdateEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_PRE_COLLECTION_UPDATE;
138-
if ( listenerGroup.isEmpty() ) {
139-
return;
140-
}
141-
final PreCollectionUpdateEvent event = new PreCollectionUpdateEvent(
112+
getFastSessionServices().eventListenerGroup_PRE_COLLECTION_UPDATE
113+
.fireLazyEventOnEachListener( this::newPreCollectionUpdateEvent,
114+
PreCollectionUpdateEventListener::onPreUpdateCollection );
115+
}
116+
117+
private PreCollectionUpdateEvent newPreCollectionUpdateEvent() {
118+
return new PreCollectionUpdateEvent(
142119
getPersister(),
143120
getCollection(),
144121
eventSource()
145122
);
146-
for ( PreCollectionUpdateEventListener listener : listenerGroup.listeners() ) {
147-
listener.onPreUpdateCollection( event );
148-
}
149123
}
150124

151125
private void postUpdate() {
152-
final EventListenerGroup<PostCollectionUpdateEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_POST_COLLECTION_UPDATE;
153-
if ( listenerGroup.isEmpty() ) {
154-
return;
155-
}
156-
final PostCollectionUpdateEvent event = new PostCollectionUpdateEvent(
126+
getFastSessionServices().eventListenerGroup_POST_COLLECTION_UPDATE
127+
.fireLazyEventOnEachListener( this::newPostCollectionUpdateEvent,
128+
PostCollectionUpdateEventListener::onPostUpdateCollection );
129+
}
130+
131+
private PostCollectionUpdateEvent newPostCollectionUpdateEvent() {
132+
return new PostCollectionUpdateEvent(
157133
getPersister(),
158134
getCollection(),
159135
eventSource()
160136
);
161-
for ( PostCollectionUpdateEventListener listener : listenerGroup.listeners() ) {
162-
listener.onPostUpdateCollection( event );
163-
}
164137
}
165138

166-
private void preRecreate() {
167-
final EventListenerGroup<PreCollectionRecreateEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_PRE_COLLECTION_RECREATE;
168-
if ( listenerGroup.isEmpty() ) {
169-
return;
170-
}
171-
final PreCollectionRecreateEvent event = new PreCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
172-
for ( PreCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
173-
listener.onPreRecreateCollection( event );
174-
}
175-
}
176-
177-
private void postRecreate() {
178-
final EventListenerGroup<PostCollectionRecreateEventListener> listenerGroup = getFastSessionServices().eventListenerGroup_POST_COLLECTION_RECREATE;
179-
if ( listenerGroup.isEmpty() ) {
180-
return;
181-
}
182-
final PostCollectionRecreateEvent event = new PostCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
183-
for ( PostCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
184-
listener.onPostRecreateCollection( event );
185-
}
186-
}
187139
}

0 commit comments

Comments
 (0)