Skip to content

Commit ed387e5

Browse files
committed
Changed MongoCollection.createIndex methods to return a String for the index name, and MongoCollection.createIndexes to return a List<String>for the index names
JAVA-1737
1 parent 54fd745 commit ed387e5

File tree

11 files changed

+113
-38
lines changed

11 files changed

+113
-38
lines changed

driver-async/src/examples/primer/IndexesPrimer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
package primer;
1818

1919

20+
import com.mongodb.async.SingleResultCallback;
21+
import org.bson.Document;
2022
import org.junit.Test;
2123

2224
// @imports: start
23-
import org.bson.Document;
24-
import com.mongodb.async.SingleResultCallback;
2525
// @imports: end
2626

2727
public class IndexesPrimer extends PrimerTestCase {
@@ -31,9 +31,9 @@ public void singleFieldIndex() {
3131

3232
// @begin: single-field-index
3333
// @code: start
34-
db.getCollection("restaurants").createIndex(new Document("cuisine", 1), new SingleResultCallback<Void>() {
34+
db.getCollection("restaurants").createIndex(new Document("cuisine", 1), new SingleResultCallback<String>() {
3535
@Override
36-
public void onResult(final Void result, final Throwable t) {
36+
public void onResult(final String result, final Throwable t) {
3737
System.out.println("Operation Finished");
3838
}
3939
});
@@ -48,9 +48,9 @@ public void createCompoundIndex() {
4848
// @begin: create-compound-index
4949
// @code: start
5050
db.getCollection("restaurants").createIndex(new Document("cuisine", 1).append("address.zipcode", 1),
51-
new SingleResultCallback<Void>() {
51+
new SingleResultCallback<String>() {
5252
@Override
53-
public void onResult(final Void result, final Throwable t) {
53+
public void onResult(final String result, final Throwable t) {
5454
System.out.println("Operation Finished");
5555
}
5656
});

driver-async/src/examples/tour/QuickTourAdmin.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ public void apply(final String databaseName) {
100100
collection.drop(callbackWhenFinished);
101101

102102
// create an ascending index on the "i" field
103-
collection.createIndex(new Document("i", 1), callbackWhenFinished);
103+
collection.createIndex(new Document("i", 1), new SingleResultCallback<String>() {
104+
@Override
105+
public void onResult(final String result, final Throwable t) {
106+
System.out.println("Operation finished");
107+
}
108+
});
104109

105110
// list the indexes on the collection
106111
Block<Document> printDocumentBlock = new Block<Document>() {
@@ -113,7 +118,12 @@ public void apply(final Document document) {
113118

114119

115120
// create a text index on the "content" field
116-
collection.createIndex(new Document("content", "text"), callbackWhenFinished);
121+
collection.createIndex(new Document("content", "text"), new SingleResultCallback<String>() {
122+
@Override
123+
public void onResult(final String result, final Throwable t) {
124+
System.out.println("Operation finished");
125+
}
126+
});
117127

118128
collection.insertOne(new Document("_id", 0).append("content", "textual content"), callbackWhenFinished);
119129
collection.insertOne(new Document("_id", 1).append("content", "additional content"), callbackWhenFinished);

driver-async/src/main/com/mongodb/async/client/MongoCollection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,33 +472,33 @@ void bulkWrite(List<? extends WriteModel<? extends TDocument>> requests, BulkWri
472472
void drop(SingleResultCallback<Void> callback);
473473

474474
/**
475-
* Creates an index.
475+
* Creates an index. If successful, the callback will be executed with the name of the created index as the result.
476476
*
477477
* @param key an object describing the index key(s), which may not be null.
478478
* @param callback the callback that is completed once the index has been created
479479
* @mongodb.driver.manual reference/command/createIndexes/ Create indexes
480480
*/
481-
void createIndex(Bson key, SingleResultCallback<Void> callback);
481+
void createIndex(Bson key, SingleResultCallback<String> callback);
482482

483483
/**
484-
* Creates an index.
484+
* Creates an index. If successful, the callback will be executed with the name of the created index as the result.
485485
*
486486
* @param key an object describing the index key(s), which may not be null.
487487
* @param options the options for the index
488488
* @param callback the callback that is completed once the index has been created
489489
* @mongodb.driver.manual reference/command/createIndexes/ Create indexes
490490
*/
491-
void createIndex(Bson key, IndexOptions options, SingleResultCallback<Void> callback);
491+
void createIndex(Bson key, IndexOptions options, SingleResultCallback<String> callback);
492492

493493
/**
494-
* Create multiple indexes.
494+
* Create multiple indexes. If successful, the callback will be executed with a list of the namess of the created index as the result.
495495
*
496496
* @param indexes the list of indexes
497497
* @param callback the callback that is completed once the indexes has been created
498498
* @mongodb.driver.manual reference/command/createIndexes Create indexes
499499
* @mongodb.server.release 2.6
500500
*/
501-
void createIndexes(List<IndexModel> indexes, SingleResultCallback<Void> callback);
501+
void createIndexes(List<IndexModel> indexes, SingleResultCallback<List<String>> callback);
502502

503503
/**
504504
* Get all the indexes in this collection.

driver-async/src/main/com/mongodb/async/client/MongoCollectionImpl.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
8080
import static java.lang.String.format;
8181
import static java.util.Arrays.asList;
82+
import static java.util.Collections.singletonList;
8283
import static java.util.concurrent.TimeUnit.MILLISECONDS;
8384

8485
class MongoCollectionImpl<TDocument> implements MongoCollection<TDocument> {
@@ -418,17 +419,26 @@ public void drop(final SingleResultCallback<Void> callback) {
418419
}
419420

420421
@Override
421-
public void createIndex(final Bson key, final SingleResultCallback<Void> callback) {
422+
public void createIndex(final Bson key, final SingleResultCallback<String> callback) {
422423
createIndex(key, new IndexOptions(), callback);
423424
}
424425

425426
@Override
426-
public void createIndex(final Bson key, final IndexOptions indexOptions, final SingleResultCallback<Void> callback) {
427-
createIndexes(asList(new IndexModel(key, indexOptions)), callback);
427+
public void createIndex(final Bson key, final IndexOptions indexOptions, final SingleResultCallback<String> callback) {
428+
createIndexes(singletonList(new IndexModel(key, indexOptions)), new SingleResultCallback<List<String>>() {
429+
@Override
430+
public void onResult(final List<String> result, final Throwable t) {
431+
if (t != null) {
432+
callback.onResult(null, t);
433+
} else {
434+
callback.onResult(result.get(0), null);
435+
}
436+
}
437+
});
428438
}
429439

430440
@Override
431-
public void createIndexes(final List<IndexModel> indexes, final SingleResultCallback<Void> callback) {
441+
public void createIndexes(final List<IndexModel> indexes, final SingleResultCallback<List<String>> callback) {
432442
notNull("indexes", indexes);
433443

434444
List<IndexRequest> indexRequests = new ArrayList<IndexRequest>(indexes.size());
@@ -451,7 +461,17 @@ public void createIndexes(final List<IndexModel> indexes, final SingleResultCall
451461
.bucketSize(model.getOptions().getBucketSize())
452462
.storageEngine(toBsonDocument(model.getOptions().getStorageEngine())));
453463
}
454-
executor.execute(new CreateIndexesOperation(getNamespace(), indexRequests), callback);
464+
final CreateIndexesOperation createIndexesOperation = new CreateIndexesOperation(getNamespace(), indexRequests);
465+
executor.execute(createIndexesOperation, new SingleResultCallback<Void>() {
466+
@Override
467+
public void onResult(final Void result, final Throwable t) {
468+
if (t != null) {
469+
callback.onResult(null, t);
470+
} else {
471+
callback.onResult(createIndexesOperation.getIndexNames(), null);
472+
}
473+
}
474+
});
455475
}
456476

457477
@Override

driver-async/src/test/functional/com/mongodb/async/client/SmokeTestSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class SmokeTestSpecification extends FunctionalSpecification {
138138
collectionNames.contains(collectionName)
139139

140140
then: 'create an index'
141-
run(collection.&createIndex, new Document('test', 1)) == null
141+
run(collection.&createIndex, new Document('test', 1)) == 'test_1'
142142

143143
then: 'has the newly created index'
144144
run(collection.listIndexes().&into, [])*.name.containsAll('_id_', 'test_1')

driver-async/src/test/unit/com/mongodb/async/client/MongoCollectionSpecification.groovy

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,26 +790,28 @@ class MongoCollectionSpecification extends Specification {
790790

791791
when:
792792
def expectedOperation = new CreateIndexesOperation(namespace, [new IndexRequest(new BsonDocument('key', new BsonInt32(1)))])
793-
def futureResultCallback = new FutureResultCallback<Void>()
793+
def futureResultCallback = new FutureResultCallback<String>()
794794
collection.createIndex(new Document('key', 1), futureResultCallback)
795-
futureResultCallback.get()
795+
def indexName = futureResultCallback.get()
796796
def operation = executor.getWriteOperation() as CreateIndexesOperation
797797

798798
then:
799799
expect operation, isTheSameAs(expectedOperation)
800+
indexName == 'key_1'
800801

801802
when:
802803
expectedOperation = new CreateIndexesOperation(namespace, [new IndexRequest(new BsonDocument('key', new BsonInt32(1)))])
803-
futureResultCallback = new FutureResultCallback<Void>()
804+
futureResultCallback = new FutureResultCallback<List<String>>()
804805
expectedOperation = new CreateIndexesOperation(namespace, [new IndexRequest(new BsonDocument('key', new BsonInt32(1))),
805806
new IndexRequest(new BsonDocument('key1', new BsonInt32(1)))])
806807
collection.createIndexes([new IndexModel(new Document('key', 1)), new IndexModel(new Document('key1', 1))],
807808
futureResultCallback)
808-
futureResultCallback.get()
809+
def indexNames = futureResultCallback.get()
809810
operation = executor.getWriteOperation() as CreateIndexesOperation
810811

811812
then:
812813
expect operation, isTheSameAs(expectedOperation)
814+
indexNames == ['key_1', 'key1_1']
813815

814816
when:
815817
expectedOperation =
@@ -834,7 +836,7 @@ class MongoCollectionSpecification extends Specification {
834836
new BsonDocument('configString',
835837
new BsonString(
836838
'block_compressor=zlib'))))])
837-
futureResultCallback = new FutureResultCallback<Void>()
839+
futureResultCallback = new FutureResultCallback<String>()
838840
collection.createIndex(new Document('key', 1), new IndexOptions()
839841
.background(true)
840842
.unique(true)
@@ -854,11 +856,12 @@ class MongoCollectionSpecification extends Specification {
854856
.storageEngine(new BsonDocument('wiredTiger',
855857
new BsonDocument('configString', new BsonString('block_compressor=zlib')))),
856858
futureResultCallback)
857-
futureResultCallback.get()
859+
indexName = futureResultCallback.get()
858860
operation = executor.getWriteOperation() as CreateIndexesOperation
859861

860862
then:
861863
expect operation, isTheSameAs(expectedOperation)
864+
indexName == 'aIndex'
862865
}
863866

864867
def 'should use ListIndexesOperations correctly'() {

driver-core/src/main/com/mongodb/operation/CreateIndexesOperation.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ public List<IndexRequest> getRequests() {
8989
return requests;
9090
}
9191

92+
/**
93+
* Gets the index names.
94+
*
95+
* @return a List<String> of index names
96+
*/
97+
public List<String> getIndexNames() {
98+
List<String> indexNames = new ArrayList<String>(requests.size());
99+
for (IndexRequest request : requests) {
100+
if (request.getName() != null) {
101+
indexNames.add(request.getName());
102+
} else {
103+
indexNames.add(IndexHelper.generateIndexName(request.getKeys()));
104+
}
105+
}
106+
return indexNames;
107+
}
108+
92109
@Override
93110
public Void execute(final WriteBinding binding) {
94111
return withConnection(binding, new CallableWithConnection<Void>() {

driver-core/src/test/functional/com/mongodb/operation/CreateIndexesOperationSpecification.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ class CreateIndexesOperationSpecification extends OperationFunctionalSpecificati
4444
def xyIndex = ['x.y': 1]
4545

4646

47+
def 'should get index names'() {
48+
when:
49+
50+
def createIndexOperation = new CreateIndexesOperation(getNamespace(),
51+
[new IndexRequest(new BsonDocument('field1', new BsonInt32(1))),
52+
new IndexRequest(new BsonDocument('field2', new BsonInt32(-1))),
53+
new IndexRequest(new BsonDocument('field3', new BsonInt32(1))
54+
.append('field4', new BsonInt32(-1))),
55+
new IndexRequest(new BsonDocument('field5', new BsonInt32(-1)))
56+
.name('customName')
57+
])
58+
then:
59+
createIndexOperation.indexNames == ['field1_1', 'field2_-1', 'field3_1_field4_-1', 'customName']
60+
61+
}
62+
4763
def 'should be able to create a single index'() {
4864
given:
4965
def keys = new BsonDocument('field', new BsonInt32(1))

driver/src/main/com/mongodb/MongoCollectionImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import static com.mongodb.assertions.Assertions.notNull;
7777
import static java.lang.String.format;
7878
import static java.util.Arrays.asList;
79+
import static java.util.Collections.singletonList;
7980
import static java.util.concurrent.TimeUnit.MILLISECONDS;
8081

8182
class MongoCollectionImpl<TDocument> implements MongoCollection<TDocument> {
@@ -386,17 +387,17 @@ public void drop() {
386387
}
387388

388389
@Override
389-
public void createIndex(final Bson keys) {
390-
createIndex(keys, new IndexOptions());
390+
public String createIndex(final Bson keys) {
391+
return createIndex(keys, new IndexOptions());
391392
}
392393

393394
@Override
394-
public void createIndex(final Bson keys, final IndexOptions indexOptions) {
395-
createIndexes(asList(new IndexModel(keys, indexOptions)));
395+
public String createIndex(final Bson keys, final IndexOptions indexOptions) {
396+
return createIndexes(singletonList(new IndexModel(keys, indexOptions))).get(0);
396397
}
397398

398399
@Override
399-
public void createIndexes(final List<IndexModel> indexes) {
400+
public List<String> createIndexes(final List<IndexModel> indexes) {
400401
notNull("indexes", indexes);
401402

402403
List<IndexRequest> indexRequests = new ArrayList<IndexRequest>(indexes.size());
@@ -419,7 +420,9 @@ public void createIndexes(final List<IndexModel> indexes) {
419420
.bucketSize(model.getOptions().getBucketSize())
420421
.storageEngine(toBsonDocument(model.getOptions().getStorageEngine())));
421422
}
422-
executor.execute(new CreateIndexesOperation(getNamespace(), indexRequests));
423+
CreateIndexesOperation createIndexesOperation = new CreateIndexesOperation(getNamespace(), indexRequests);
424+
executor.execute(createIndexesOperation);
425+
return createIndexesOperation.getIndexNames();
423426
}
424427

425428
@Override

driver/src/main/com/mongodb/client/MongoCollection.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,27 +469,30 @@ public interface MongoCollection<TDocument> {
469469
* Create an index with the given keys.
470470
*
471471
* @param keys an object describing the index key(s), which may not be null.
472+
* @return the index name
472473
* @mongodb.driver.manual reference/command/createIndexes Create indexes
473474
*/
474-
void createIndex(Bson keys);
475+
String createIndex(Bson keys);
475476

476477
/**
477478
* Create an index with the given keys and options.
478479
*
479480
* @param keys an object describing the index key(s), which may not be null.
480481
* @param indexOptions the options for the index
482+
* @return the index name
481483
* @mongodb.driver.manual reference/command/createIndexes Create indexes
482484
*/
483-
void createIndex(Bson keys, IndexOptions indexOptions);
485+
String createIndex(Bson keys, IndexOptions indexOptions);
484486

485487
/**
486488
* Create multiple indexes.
487489
*
488490
* @param indexes the list of indexes
491+
* @return the list of index names
489492
* @mongodb.driver.manual reference/command/createIndexes Create indexes
490493
* @mongodb.server.release 2.6
491494
*/
492-
void createIndexes(List<IndexModel> indexes);
495+
List<String> createIndexes(List<IndexModel> indexes);
493496

494497
/**
495498
* Get all the indexes in this collection.

0 commit comments

Comments
 (0)