@@ -158,17 +158,16 @@ Data data() {
158
158
* @throws QueryException query exception
159
159
*/
160
160
void apply (final QueryContext qc ) throws QueryException {
161
- // execute database updates
161
+ // apply initial database operations
162
+ Collections .sort (dbUpdates );
163
+ applyDbUpdates (true );
164
+
165
+ // execute updates within databases
162
166
auc .execute (true );
163
167
auc = null ;
164
168
165
- // execute database operations
166
- Collections .sort (dbUpdates );
167
- final int sz = dbUpdates .size ();
168
- for (int i = 0 ; i < sz ; i ++) {
169
- dbUpdates .get (i ).apply ();
170
- dbUpdates .set (i , null );
171
- }
169
+ // apply remaining database operations
170
+ applyDbUpdates (false );
172
171
173
172
// execute fn:put operations
174
173
for (final Put put : puts .values ()) put .apply ();
@@ -198,6 +197,23 @@ void apply(final QueryContext qc) throws QueryException {
198
197
}
199
198
}
200
199
200
+ /**
201
+ * Applies all database operations.
202
+ * @param before run updates specified before or after node updates
203
+ * @throws QueryException query exception
204
+ */
205
+ private void applyDbUpdates (final boolean before ) throws QueryException {
206
+ final int pos = UpdateType ._NODE_UPDATES_ .ordinal ();
207
+ for (final ListIterator <DBUpdate > iter = dbUpdates .listIterator (); iter .hasNext ();) {
208
+ final DBUpdate up = iter .next ();
209
+ final int ord = up .type .ordinal ();
210
+ if (before ? ord < pos : ord > pos ) {
211
+ up .apply ();
212
+ iter .remove ();
213
+ }
214
+ }
215
+ }
216
+
201
217
/**
202
218
* Prepares the {@link NodeUpdate} for execution incl. ordering,
203
219
* and removes the update primitive references to save memory.
@@ -207,17 +223,13 @@ private List<NodeUpdate> preparePrimitives() {
207
223
final List <NodeUpdate > upd = new ArrayList <>();
208
224
for (int i = nodes .size () - 1 ; i >= 0 ; i --) {
209
225
final int pre = nodes .get (i );
210
- final NodeUpdates n = nodeUpdates .get (pre );
211
- for (final NodeUpdate p : n .finish ()) {
212
- upd .add (p );
213
- size += p .size ();
226
+ for (final NodeUpdate up : nodeUpdates .get (pre ).finish ()) {
227
+ upd .add (up );
228
+ size += up .size ();
214
229
}
215
230
}
216
- for (int i = dbUpdates .size () - 1 ; i >= 0 ; i --) {
217
- size += dbUpdates .get (i ).size ();
218
- }
219
- nodeUpdates = null ;
220
231
nodes = null ;
232
+ for (final DBUpdate up : dbUpdates ) size += up .size ();
221
233
upd .sort (new NodeUpdateComparator ());
222
234
return upd ;
223
235
}
0 commit comments