13
13
import org .hibernate .action .internal .CollectionAction ;
14
14
import org .hibernate .collection .spi .PersistentCollection ;
15
15
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
16
- import org .hibernate .event .service .spi .EventListenerGroup ;
17
16
import org .hibernate .event .spi .EventSource ;
18
- import org .hibernate .event .spi .PostCollectionRecreateEvent ;
19
- import org .hibernate .event .spi .PostCollectionRecreateEventListener ;
20
17
import org .hibernate .event .spi .PostCollectionUpdateEvent ;
21
18
import org .hibernate .event .spi .PostCollectionUpdateEventListener ;
22
- import org .hibernate .event .spi .PreCollectionRecreateEvent ;
23
- import org .hibernate .event .spi .PreCollectionRecreateEventListener ;
24
19
import org .hibernate .event .spi .PreCollectionUpdateEvent ;
25
20
import org .hibernate .event .spi .PreCollectionUpdateEventListener ;
26
21
import org .hibernate .persister .collection .CollectionPersister ;
27
22
import org .hibernate .reactive .engine .ReactiveExecutable ;
28
23
import org .hibernate .reactive .logging .impl .Log ;
29
24
import org .hibernate .reactive .logging .impl .LoggerFactory ;
30
25
import org .hibernate .reactive .persister .collection .impl .ReactiveCollectionPersister ;
31
- import org .hibernate .reactive .util .impl .CompletionStages ;
32
26
import org .hibernate .stat .spi .StatisticsImplementor ;
33
27
34
28
import static org .hibernate .pretty .MessageHelper .collectionInfoString ;
29
+ import static org .hibernate .reactive .util .impl .CompletionStages .voidFuture ;
35
30
36
31
37
32
/**
@@ -59,15 +54,13 @@ public CompletionStage<Void> reactiveExecute() {
59
54
final Object key = getKey ();
60
55
final SharedSessionContractImplementor session = getSession ();
61
56
final ReactiveCollectionPersister reactivePersister = (ReactiveCollectionPersister ) getPersister ();
62
- final CollectionPersister corePersister = getPersister ();
57
+ final CollectionPersister persister = getPersister ();
63
58
final PersistentCollection collection = getCollection ();
64
- final boolean affectedByFilters = corePersister .isAffectedByEnabledFilters ( session );
59
+ final boolean affectedByFilters = persister .isAffectedByEnabledFilters ( session );
65
60
66
61
preUpdate ();
67
62
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 ;
71
64
if ( !collection .wasInitialized () ) {
72
65
// If there were queued operations, they would have been processed
73
66
// and cleared by now.
@@ -76,53 +69,35 @@ public CompletionStage<Void> reactiveExecute() {
76
69
throw new AssertionFailure ( "collection is not dirty" );
77
70
}
78
71
//do nothing - we only need to notify the cache...
72
+ updateStage = voidFuture ();
79
73
}
80
74
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 );
86
76
}
87
- else if ( collection .needsRecreate ( corePersister ) ) {
77
+ else if ( collection .needsRecreate ( persister ) ) {
88
78
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 ) );
95
80
}
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 ) );
110
85
}
111
86
else {
112
- updateStage = updateStage
87
+ updateStage = voidFuture ()
113
88
.thenCompose ( v -> reactivePersister .reactiveDeleteRows ( collection , key , session ) )
114
89
.thenCompose ( v -> reactivePersister .reactiveUpdateRows ( collection , key , session ) )
115
90
.thenCompose ( v -> reactivePersister .reactiveInsertRows ( collection , key , session ) );
116
91
}
117
92
118
- return updateStage .thenAccept (v -> {
93
+ return updateStage .thenAccept ( v -> {
119
94
session .getPersistenceContextInternal ().getCollectionEntry ( collection ).afterAction ( collection );
120
95
evict ();
121
96
postUpdate ();
122
97
123
98
final StatisticsImplementor statistics = session .getFactory ().getStatistics ();
124
99
if ( statistics .isStatisticsEnabled () ) {
125
- statistics .updateCollection ( corePersister .getRole () );
100
+ statistics .updateCollection ( persister .getRole () );
126
101
}
127
102
} );
128
103
}
@@ -134,54 +109,31 @@ public void execute() throws HibernateException {
134
109
}
135
110
136
111
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 (
142
119
getPersister (),
143
120
getCollection (),
144
121
eventSource ()
145
122
);
146
- for ( PreCollectionUpdateEventListener listener : listenerGroup .listeners () ) {
147
- listener .onPreUpdateCollection ( event );
148
- }
149
123
}
150
124
151
125
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 (
157
133
getPersister (),
158
134
getCollection (),
159
135
eventSource ()
160
136
);
161
- for ( PostCollectionUpdateEventListener listener : listenerGroup .listeners () ) {
162
- listener .onPostUpdateCollection ( event );
163
- }
164
137
}
165
138
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
- }
187
139
}
0 commit comments