44
44
45
45
import java .util .List ;
46
46
import java .util .concurrent .atomic .AtomicBoolean ;
47
- import java .util .concurrent .atomic .AtomicInteger ;
48
47
49
48
import static com .mongodb .assertions .Assertions .assertNotNull ;
50
49
import static com .mongodb .assertions .Assertions .assertTrue ;
63
62
class AsyncCommandBatchCursor <T > implements AsyncAggregateResponseBatchCursor <T > {
64
63
65
64
private final MongoNamespace namespace ;
66
- private final int limit ;
67
65
private final long maxTimeMS ;
68
66
private final Decoder <T > decoder ;
69
67
@ Nullable
@@ -72,21 +70,19 @@ class AsyncCommandBatchCursor<T> implements AsyncAggregateResponseBatchCursor<T>
72
70
private final boolean firstBatchEmpty ;
73
71
private final ResourceManager resourceManager ;
74
72
private final CommandCursorResult <T > commandCursorResult ;
75
- private final AtomicInteger count = new AtomicInteger ();
76
73
private final AtomicBoolean processedInitial = new AtomicBoolean ();
77
74
private int batchSize ;
78
75
79
76
AsyncCommandBatchCursor (
80
77
final BsonDocument commandCursorDocument ,
81
- final int limit , final int batchSize , final long maxTimeMS ,
78
+ final int batchSize , final long maxTimeMS ,
82
79
final Decoder <T > decoder ,
83
80
@ Nullable final BsonValue comment ,
84
81
final AsyncConnectionSource connectionSource ,
85
82
final AsyncConnection connection ) {
86
83
ConnectionDescription connectionDescription = connection .getDescription ();
87
84
this .commandCursorResult = toCommandCursorResult (connectionDescription .getServerAddress (), FIRST_BATCH , commandCursorDocument );
88
85
this .namespace = commandCursorResult .getNamespace ();
89
- this .limit = limit ;
90
86
this .batchSize = batchSize ;
91
87
this .maxTimeMS = maxTimeMS ;
92
88
this .decoder = notNull ("decoder" , decoder );
@@ -95,17 +91,11 @@ class AsyncCommandBatchCursor<T> implements AsyncAggregateResponseBatchCursor<T>
95
91
this .firstBatchEmpty = commandCursorResult .getResults ().isEmpty ();
96
92
97
93
AsyncConnection connectionToPin = null ;
98
- boolean releaseServerAndResources = false ;
99
- if (limitReached ()) {
100
- releaseServerAndResources = true ;
101
- } else if (connectionDescription .getServerType () == ServerType .LOAD_BALANCER ) {
94
+ if (connectionDescription .getServerType () == ServerType .LOAD_BALANCER ) {
102
95
connectionToPin = connection ;
103
96
}
104
97
105
98
resourceManager = new ResourceManager (namespace , connectionSource , connectionToPin , commandCursorResult .getServerCursor ());
106
- if (releaseServerAndResources ) {
107
- resourceManager .releaseServerAndClientResources (connection );
108
- }
109
99
}
110
100
111
101
@ Override
@@ -124,15 +114,12 @@ public void next(final SingleResultCallback<List<T>> callback) {
124
114
}
125
115
126
116
if (serverCursorIsNull || !batchResults .isEmpty ()) {
127
- if (serverCursorIsNull ) {
128
- close ();
129
- }
130
117
funcCallback .onResult (batchResults , null );
131
118
} else {
132
119
getMore (localServerCursor , funcCallback );
133
120
}
134
121
}).get ((r , t ) -> {
135
- if (limitReached () ) {
122
+ if (resourceManager . getServerCursor () == null ) {
136
123
close ();
137
124
}
138
125
callback .onResult (r , t );
@@ -196,8 +183,7 @@ private void getMore(final ServerCursor cursor, final SingleResultCallback<List<
196
183
private void getMoreLoop (final AsyncConnection connection , final ServerCursor serverCursor ,
197
184
final SingleResultCallback <List <T >> callback ) {
198
185
connection .commandAsync (namespace .getDatabaseName (),
199
- getMoreCommandDocument (serverCursor .getId (), connection .getDescription (), namespace ,
200
- limit , batchSize , count .get (), maxTimeMS , comment ),
186
+ getMoreCommandDocument (serverCursor .getId (), connection .getDescription (), namespace , batchSize , maxTimeMS , comment ),
201
187
NO_OP_FIELD_NAME_VALIDATOR , ReadPreference .primary (),
202
188
CommandResultDocumentCodec .create (decoder , NEXT_BATCH ),
203
189
assertNotNull (resourceManager .getConnectionSource ()).getOperationContext (),
@@ -214,35 +200,29 @@ private void getMoreLoop(final AsyncConnection connection, final ServerCursor se
214
200
connection .getDescription ().getServerAddress (), NEXT_BATCH , assertNotNull (commandResult ));
215
201
resourceManager .setServerCursor (commandCursorResult .getServerCursor ());
216
202
203
+ List <T > nextBatch = commandCursorResult .getResults ();
204
+ if (commandCursorResult .getServerCursor () == null || !nextBatch .isEmpty ()) {
205
+ callback .onResult (nextBatch , null );
206
+ return ;
207
+ }
208
+
217
209
if (!resourceManager .operable ()) {
218
- // The cursor was closed
219
- resourceManager .releaseServerAndClientResources (connection );
220
210
callback .onResult (emptyList (), null );
221
211
return ;
222
212
}
223
213
224
- List <T > nextBatch = commandCursorResult .getResults ();
225
- if (nextBatch .isEmpty () && commandCursorResult .getServerCursor () != null ) {
226
- getMoreLoop (connection , commandCursorResult .getServerCursor (), callback );
227
- } else {
228
- callback .onResult (nextBatch , null );
229
- }
230
- });
214
+ getMoreLoop (connection , commandCursorResult .getServerCursor (), callback );
215
+ });
231
216
}
232
217
233
218
private CommandCursorResult <T > toCommandCursorResult (final ServerAddress serverAddress , final String fieldNameContainingBatch ,
234
219
final BsonDocument commandCursorDocument ) {
235
220
CommandCursorResult <T > commandCursorResult = new CommandCursorResult <>(serverAddress , fieldNameContainingBatch ,
236
221
commandCursorDocument );
237
222
logCommandCursorResult (commandCursorResult );
238
- this .count .addAndGet (commandCursorResult .getResults ().size ());
239
223
return commandCursorResult ;
240
224
}
241
225
242
- private boolean limitReached () {
243
- return Math .abs (limit ) != 0 && count .get () >= Math .abs (limit );
244
- }
245
-
246
226
@ ThreadSafe
247
227
private static final class ResourceManager extends CursorResourceManager <AsyncConnectionSource , AsyncConnection > {
248
228
@@ -282,7 +262,7 @@ void doClose() {
282
262
if (getServerCursor () != null ) {
283
263
getConnection ((connection , t ) -> {
284
264
if (connection != null ) {
285
- releaseServerAndClientResources (connection , ( r , t1 ) -> connection . release () );
265
+ releaseServerAndClientResources (connection );
286
266
} else {
287
267
unsetServerCursor ();
288
268
releaseClientResources ();
@@ -320,10 +300,6 @@ private void getConnection(final SingleResultCallback<AsyncConnection> callback)
320
300
}
321
301
322
302
private void releaseServerAndClientResources (final AsyncConnection connection ) {
323
- releaseServerAndClientResources (connection , (r , t ) -> { /* Do nothing */ });
324
- }
325
-
326
- private void releaseServerAndClientResources (final AsyncConnection connection , final SingleResultCallback <Void > callback ) {
327
303
AsyncCallbackSupplier <Void > callbackSupplier = funcCallback -> {
328
304
ServerCursor localServerCursor = getServerCursor ();
329
305
if (localServerCursor != null ) {
@@ -333,7 +309,7 @@ private void releaseServerAndClientResources(final AsyncConnection connection, f
333
309
callbackSupplier .whenComplete (() -> {
334
310
unsetServerCursor ();
335
311
releaseClientResources ();
336
- }).get (callback );
312
+ }).whenComplete ( connection :: release ). get (( r , t ) -> { /* do nothing */ } );
337
313
}
338
314
339
315
private void killServerCursor (final MongoNamespace namespace , final ServerCursor localServerCursor ,
0 commit comments