16
16
17
17
package com .mongodb .async .client ;
18
18
19
- import com .mongodb .Block ;
20
- import com .mongodb .Function ;
21
19
import com .mongodb .MongoNamespace ;
22
20
import com .mongodb .ReadConcern ;
23
21
import com .mongodb .ReadPreference ;
24
22
import com .mongodb .WriteConcern ;
25
23
import com .mongodb .async .AsyncBatchCursor ;
26
24
import com .mongodb .async .SingleResultCallback ;
27
25
import com .mongodb .client .model .Collation ;
28
- import com .mongodb .client .model .FindOptions ;
29
26
import com .mongodb .operation .AggregateOperation ;
30
27
import com .mongodb .operation .AggregateToCollectionOperation ;
31
28
import com .mongodb .operation .AsyncOperationExecutor ;
29
+ import com .mongodb .operation .AsyncReadOperation ;
30
+ import com .mongodb .operation .FindOperation ;
31
+ import com .mongodb .session .ClientSession ;
32
32
import org .bson .BsonDocument ;
33
33
import org .bson .BsonValue ;
34
34
import org .bson .codecs .configuration .CodecRegistry ;
35
35
import org .bson .conversions .Bson ;
36
36
37
37
import java .util .ArrayList ;
38
- import java .util .Collection ;
39
38
import java .util .List ;
40
39
import java .util .concurrent .TimeUnit ;
41
40
42
- import static com .mongodb .ReadPreference .primary ;
43
41
import static com .mongodb .assertions .Assertions .notNull ;
44
42
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
45
43
46
44
47
- class AggregateIterableImpl <TDocument , TResult > implements AggregateIterable <TResult > {
45
+ class AggregateIterableImpl <TDocument , TResult > extends MongoIterableImpl < TResult > implements AggregateIterable <TResult > {
48
46
private final MongoNamespace namespace ;
49
47
private final Class <TDocument > documentClass ;
50
48
private final Class <TResult > resultClass ;
51
- private final ReadPreference readPreference ;
52
- private final ReadConcern readConcern ;
53
49
private final WriteConcern writeConcern ;
54
50
private final CodecRegistry codecRegistry ;
55
- private final AsyncOperationExecutor executor ;
56
51
private final List <? extends Bson > pipeline ;
57
52
58
53
private Boolean allowDiskUse ;
59
- private Integer batchSize ;
60
- private long maxAwaitTimeMS ;
61
54
private long maxTimeMS ;
55
+ private long maxAwaitTimeMS ;
62
56
private Boolean useCursor ;
63
57
private Boolean bypassDocumentValidation ;
64
58
private Collation collation ;
65
59
private String comment ;
66
60
private Bson hint ;
67
61
68
- AggregateIterableImpl (final MongoNamespace namespace , final Class <TDocument > documentClass , final Class <TResult > resultClass ,
69
- final CodecRegistry codecRegistry , final ReadPreference readPreference , final ReadConcern readConcern ,
70
- final WriteConcern writeConcern , final AsyncOperationExecutor executor , final List <? extends Bson > pipeline ) {
62
+ AggregateIterableImpl (final ClientSession clientSession , final MongoNamespace namespace , final Class <TDocument > documentClass ,
63
+ final Class <TResult > resultClass , final CodecRegistry codecRegistry , final ReadPreference readPreference ,
64
+ final ReadConcern readConcern , final WriteConcern writeConcern , final AsyncOperationExecutor executor ,
65
+ final List <? extends Bson > pipeline ) {
66
+ super (clientSession , executor , readConcern , readPreference );
71
67
this .namespace = notNull ("namespace" , namespace );
72
68
this .documentClass = notNull ("documentClass" , documentClass );
73
69
this .resultClass = notNull ("resultClass" , resultClass );
74
70
this .codecRegistry = notNull ("codecRegistry" , codecRegistry );
75
- this .readPreference = notNull ("readPreference" , readPreference );
76
- this .readConcern = notNull ("readConcern" , readConcern );
77
71
this .writeConcern = notNull ("writeConcern" , writeConcern );
78
- this .executor = notNull ("executor" , executor );
79
72
this .pipeline = notNull ("pipeline" , pipeline );
80
73
}
81
74
82
75
@ Override
83
- public AggregateIterable <TResult > allowDiskUse (final Boolean allowDiskUse ) {
84
- this .allowDiskUse = allowDiskUse ;
85
- return this ;
76
+ public void toCollection (final SingleResultCallback <Void > callback ) {
77
+ List <BsonDocument > aggregateList = createBsonDocumentList (pipeline );
78
+
79
+ if (getOutCollection (aggregateList ) == null ) {
80
+ throw new IllegalStateException ("The last stage of the aggregation pipeline must be $out" );
81
+ }
82
+
83
+ getExecutor ().execute (createAggregateToCollectionOperation (aggregateList ), getClientSession (), callback );
86
84
}
87
85
88
86
@ Override
89
- public AggregateIterable <TResult > batchSize (final int batchSize ) {
90
- this .batchSize = batchSize ;
87
+ public AggregateIterable <TResult > allowDiskUse (final Boolean allowDiskUse ) {
88
+ this .allowDiskUse = allowDiskUse ;
91
89
return this ;
92
90
}
93
91
94
92
@ Override
95
- public AggregateIterable <TResult > maxAwaitTime (final long maxAwaitTime , final TimeUnit timeUnit ) {
96
- notNull ("timeUnit" , timeUnit );
97
- this .maxAwaitTimeMS = TimeUnit .MILLISECONDS .convert (maxAwaitTime , timeUnit );
93
+ public AggregateIterable <TResult > batchSize (final int batchSize ) {
94
+ super .batchSize (batchSize );
98
95
return this ;
99
96
}
100
97
@@ -105,7 +102,6 @@ public AggregateIterable<TResult> maxTime(final long maxTime, final TimeUnit tim
105
102
return this ;
106
103
}
107
104
108
-
109
105
@ Override
110
106
@ Deprecated
111
107
public AggregateIterable <TResult > useCursor (final Boolean useCursor ) {
@@ -114,51 +110,10 @@ public AggregateIterable<TResult> useCursor(final Boolean useCursor) {
114
110
}
115
111
116
112
@ Override
117
- public void toCollection (final SingleResultCallback <Void > callback ) {
118
- List <BsonDocument > aggregateList = createBsonDocumentList ();
119
- BsonValue outCollection = getAggregateOutCollection (aggregateList );
120
-
121
- if (outCollection == null ) {
122
- throw new IllegalStateException ("The last stage of the aggregation pipeline must be $out" );
123
- }
124
-
125
- executor .execute (new AggregateToCollectionOperation (namespace , aggregateList , writeConcern )
126
- .maxTime (maxTimeMS , MILLISECONDS )
127
- .allowDiskUse (allowDiskUse )
128
- .collation (collation )
129
- .hint (hint == null ? null : hint .toBsonDocument (documentClass , codecRegistry ))
130
- .comment (comment ), callback );
131
- }
132
-
133
- @ Override
134
- public void first (final SingleResultCallback <TResult > callback ) {
135
- notNull ("callback" , callback );
136
- execute ().first (callback );
137
- }
138
-
139
- @ Override
140
- public void forEach (final Block <? super TResult > block , final SingleResultCallback <Void > callback ) {
141
- notNull ("block" , block );
142
- notNull ("callback" , callback );
143
- execute ().forEach (block , callback );
144
- }
145
-
146
- @ Override
147
- public <A extends Collection <? super TResult >> void into (final A target , final SingleResultCallback <A > callback ) {
148
- notNull ("target" , target );
149
- notNull ("callback" , callback );
150
- execute ().into (target , callback );
151
- }
152
-
153
- @ Override
154
- public <U > MongoIterable <U > map (final Function <TResult , U > mapper ) {
155
- return new MappingIterable <TResult , U >(this , mapper );
156
- }
157
-
158
- @ Override
159
- public void batchCursor (final SingleResultCallback <AsyncBatchCursor <TResult >> callback ) {
160
- notNull ("callback" , callback );
161
- execute ().batchCursor (callback );
113
+ public AggregateIterable <TResult > maxAwaitTime (final long maxAwaitTime , final TimeUnit timeUnit ) {
114
+ notNull ("timeUnit" , timeUnit );
115
+ this .maxAwaitTimeMS = TimeUnit .MILLISECONDS .convert (maxAwaitTime , timeUnit );
116
+ return this ;
162
117
}
163
118
164
119
@ Override
@@ -185,53 +140,59 @@ public AggregateIterable<TResult> hint(final Bson hint) {
185
140
return this ;
186
141
}
187
142
143
+ @ Override
188
144
@ SuppressWarnings ("deprecation" )
189
- private MongoIterable < TResult > execute () {
190
- List <BsonDocument > aggregateList = createBsonDocumentList ();
191
- BsonValue outCollection = getAggregateOutCollection (aggregateList );
145
+ AsyncReadOperation < AsyncBatchCursor < TResult >> asAsyncReadOperation () {
146
+ List <BsonDocument > aggregateList = createBsonDocumentList (pipeline );
147
+ BsonValue outCollection = getOutCollection (aggregateList );
192
148
193
149
if (outCollection != null ) {
194
- AggregateToCollectionOperation operation = new AggregateToCollectionOperation (namespace , aggregateList , writeConcern )
195
- .maxTime (maxTimeMS , MILLISECONDS )
196
- .allowDiskUse (allowDiskUse )
197
- .bypassDocumentValidation (bypassDocumentValidation )
198
- .collation (collation )
199
- .hint (hint == null ? null : hint .toBsonDocument (documentClass , codecRegistry ))
200
- .comment (comment );
201
- MongoIterable <TResult > delegated = new FindIterableImpl <TDocument , TResult >(new MongoNamespace (namespace .getDatabaseName (),
202
- outCollection .asString ().getValue ()), documentClass , resultClass , codecRegistry , primary (), readConcern ,
203
- executor , new BsonDocument (), new FindOptions ().collation (collation ).maxAwaitTime (maxAwaitTimeMS , MILLISECONDS ));
204
- if (batchSize != null ) {
205
- delegated .batchSize (batchSize );
150
+ AggregateToCollectionOperation aggregateToCollectionOperation = createAggregateToCollectionOperation (aggregateList );
151
+ FindOperation <TResult > findOperation =
152
+ new FindOperation <TResult >(new MongoNamespace (namespace .getDatabaseName (), outCollection .asString ().getValue ()),
153
+ codecRegistry .get (resultClass ))
154
+ .readConcern (getReadConcern ())
155
+ .collation (collation )
156
+ .maxAwaitTime (maxAwaitTimeMS , MILLISECONDS );
157
+ if (getBatchSize () != null ) {
158
+ findOperation .batchSize (getBatchSize ());
206
159
}
207
- return new AwaitingWriteOperationIterable <TResult , Void >( operation , executor , delegated );
160
+ return new AggregateToCollectionThenFindOperation <TResult >( aggregateToCollectionOperation , findOperation );
208
161
} else {
209
- return new OperationIterable <TResult >(new AggregateOperation <TResult >(namespace , aggregateList , codecRegistry .get (resultClass ))
210
- .maxAwaitTime (maxAwaitTimeMS , MILLISECONDS )
162
+ return new AggregateOperation <TResult >(namespace , aggregateList , codecRegistry .get (resultClass ))
211
163
.maxTime (maxTimeMS , MILLISECONDS )
164
+ .maxAwaitTime (maxAwaitTimeMS , MILLISECONDS )
212
165
.allowDiskUse (allowDiskUse )
213
- .batchSize (batchSize )
166
+ .batchSize (getBatchSize () )
214
167
.useCursor (useCursor )
215
- .readConcern (readConcern )
168
+ .readConcern (getReadConcern () )
216
169
.collation (collation )
217
170
.hint (hint == null ? null : hint .toBsonDocument (documentClass , codecRegistry ))
218
- .comment (comment ),
219
- readPreference ,
220
- executor );
171
+ .comment (comment );
221
172
}
222
173
}
223
174
224
- private BsonValue getAggregateOutCollection (final List <BsonDocument > aggregateList ) {
175
+ private BsonValue getOutCollection (final List <BsonDocument > aggregateList ) {
225
176
return aggregateList .size () == 0 ? null : aggregateList .get (aggregateList .size () - 1 ).get ("$out" );
226
177
}
227
178
228
- private List <BsonDocument > createBsonDocumentList () {
179
+ private AggregateToCollectionOperation createAggregateToCollectionOperation (final List <BsonDocument > aggregateList ) {
180
+ return new AggregateToCollectionOperation (namespace , aggregateList , writeConcern )
181
+ .maxTime (maxTimeMS , MILLISECONDS )
182
+ .allowDiskUse (allowDiskUse )
183
+ .bypassDocumentValidation (bypassDocumentValidation )
184
+ .collation (collation )
185
+ .hint (hint == null ? null : hint .toBsonDocument (documentClass , codecRegistry ))
186
+ .comment (comment );
187
+ }
188
+
189
+ private List <BsonDocument > createBsonDocumentList (final List <? extends Bson > pipeline ) {
229
190
List <BsonDocument > aggregateList = new ArrayList <BsonDocument >(pipeline .size ());
230
- for (Bson document : pipeline ) {
231
- if (document == null ) {
191
+ for (Bson obj : pipeline ) {
192
+ if (obj == null ) {
232
193
throw new IllegalArgumentException ("pipeline can not contain a null value" );
233
194
}
234
- aggregateList .add (document .toBsonDocument (documentClass , codecRegistry ));
195
+ aggregateList .add (obj .toBsonDocument (documentClass , codecRegistry ));
235
196
}
236
197
return aggregateList ;
237
198
}
0 commit comments