16
16
17
17
package com .mongodb .internal .operation ;
18
18
19
+ import com .mongodb .CreateIndexCommitQuorum ;
19
20
import com .mongodb .DuplicateKeyException ;
20
21
import com .mongodb .ErrorCategory ;
22
+ import com .mongodb .MongoClientException ;
21
23
import com .mongodb .MongoCommandException ;
22
24
import com .mongodb .MongoException ;
23
25
import com .mongodb .MongoNamespace ;
58
60
import static com .mongodb .internal .operation .OperationHelper .validateIndexRequestCollations ;
59
61
import static com .mongodb .internal .operation .OperationHelper .withAsyncConnection ;
60
62
import static com .mongodb .internal .operation .OperationHelper .withConnection ;
63
+ import static com .mongodb .internal .operation .ServerVersionHelper .serverIsAtLeastVersionFourDotFour ;
61
64
import static com .mongodb .internal .operation .WriteConcernHelper .appendWriteConcernToCommand ;
62
65
63
66
/**
@@ -71,6 +74,7 @@ public class CreateIndexesOperation implements AsyncWriteOperation<Void>, WriteO
71
74
private final List <IndexRequest > requests ;
72
75
private final WriteConcern writeConcern ;
73
76
private long maxTimeMS ;
77
+ private CreateIndexCommitQuorum commitQuorum ;
74
78
75
79
/**
76
80
* Construct a new instance.
@@ -161,6 +165,28 @@ public CreateIndexesOperation maxTime(final long maxTime, final TimeUnit timeUni
161
165
return this ;
162
166
}
163
167
168
+ /**
169
+ * Gets the create index commit quorum.
170
+ *
171
+ * @return the create index commit quorum
172
+ * @since 4.1
173
+ */
174
+ public CreateIndexCommitQuorum getCommitQuorum () {
175
+ return commitQuorum ;
176
+ }
177
+
178
+ /**
179
+ * Sets the create index commit quorum.
180
+ *
181
+ * @param commitQuorum the create index commit quorum
182
+ * @return this
183
+ * @since 4.1
184
+ */
185
+ public CreateIndexesOperation commitQuorum (final CreateIndexCommitQuorum commitQuorum ) {
186
+ this .commitQuorum = commitQuorum ;
187
+ return this ;
188
+ }
189
+
164
190
@ Override
165
191
public Void execute (final WriteBinding binding ) {
166
192
return withConnection (binding , new CallableWithConnection <Void >() {
@@ -194,14 +220,18 @@ public void call(final AsyncConnection connection, final Throwable t) {
194
220
if (t != null ) {
195
221
wrappedCallback .onResult (null , t );
196
222
} else {
197
- executeCommandAsync (binding , namespace .getDatabaseName (),
198
- getCommand (connection .getDescription ()), connection , writeConcernErrorWriteTransformer (),
199
- new SingleResultCallback <Void >() {
200
- @ Override
201
- public void onResult (final Void result , final Throwable t ) {
202
- wrappedCallback .onResult (null , translateException (t ));
203
- }
204
- });
223
+ try {
224
+ executeCommandAsync (binding , namespace .getDatabaseName (),
225
+ getCommand (connection .getDescription ()), connection , writeConcernErrorWriteTransformer (),
226
+ new SingleResultCallback <Void >() {
227
+ @ Override
228
+ public void onResult (final Void result , final Throwable t ) {
229
+ wrappedCallback .onResult (null , translateException (t ));
230
+ }
231
+ });
232
+ } catch (Throwable t1 ) {
233
+ wrappedCallback .onResult (null , t1 );
234
+ }
205
235
}
206
236
}
207
237
});
@@ -284,6 +314,14 @@ private BsonDocument getCommand(final ConnectionDescription description) {
284
314
command .put ("indexes" , new BsonArray (values ));
285
315
putIfNotZero (command , "maxTimeMS" , maxTimeMS );
286
316
appendWriteConcernToCommand (writeConcern , command , description );
317
+ if (commitQuorum != null ) {
318
+ if (serverIsAtLeastVersionFourDotFour (description )) {
319
+ command .put ("commitQuorum" , commitQuorum .toBsonValue ());
320
+ } else {
321
+ throw new MongoClientException ("Specifying a value for the create index commit quorum option "
322
+ + "requires a minimum MongoDB version of 4.4" );
323
+ }
324
+ }
287
325
return command ;
288
326
}
289
327
0 commit comments