Skip to content

Commit a9cfe5f

Browse files
committed
JAVA-2757: Drop index by keys when keys are provided
When the application drops an index using a method that takes the index keys, rather than the name, the driver drops the index by those keys rather than constructing a name from the keys and dropping it by that name. This allows the dropping of index by keys even when the index was created with a name other than the default name.
1 parent 847d648 commit a9cfe5f

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@
3535
import static com.mongodb.assertions.Assertions.isTrueArgument;
3636
import static com.mongodb.assertions.Assertions.notNull;
3737
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
38+
import static com.mongodb.internal.operation.WriteConcernHelper.appendWriteConcernToCommand;
3839
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol;
3940
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocolAsync;
4041
import static com.mongodb.operation.CommandOperationHelper.isNamespaceError;
42+
import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorTransformer;
4143
import static com.mongodb.operation.DocumentHelper.putIfNotZero;
42-
import static com.mongodb.internal.operation.IndexHelper.generateIndexName;
4344
import static com.mongodb.operation.OperationHelper.LOGGER;
4445
import static com.mongodb.operation.OperationHelper.releasingCallback;
4546
import static com.mongodb.operation.OperationHelper.withConnection;
46-
import static com.mongodb.internal.operation.WriteConcernHelper.appendWriteConcernToCommand;
47-
import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorTransformer;
4847

4948
/**
5049
* An operation that drops an index.
@@ -55,6 +54,7 @@
5554
public class DropIndexOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
5655
private final MongoNamespace namespace;
5756
private final String indexName;
57+
private final BsonDocument indexKeys;
5858
private final WriteConcern writeConcern;
5959
private long maxTimeMS;
6060

@@ -93,20 +93,22 @@ public DropIndexOperation(final MongoNamespace namespace, final BsonDocument key
9393
public DropIndexOperation(final MongoNamespace namespace, final String indexName, final WriteConcern writeConcern) {
9494
this.namespace = notNull("namespace", namespace);
9595
this.indexName = notNull("indexName", indexName);
96+
this.indexKeys = null;
9697
this.writeConcern = writeConcern;
9798
}
9899

99100
/**
100101
* Construct a new instance.
101102
*
102103
* @param namespace the database and collection namespace for the operation.
103-
* @param keys the keys of the index to be dropped
104+
* @param indexKeys the keys of the index to be dropped
104105
* @param writeConcern the write concern
105106
* @since 3.4
106107
*/
107-
public DropIndexOperation(final MongoNamespace namespace, final BsonDocument keys, final WriteConcern writeConcern) {
108+
public DropIndexOperation(final MongoNamespace namespace, final BsonDocument indexKeys, final WriteConcern writeConcern) {
108109
this.namespace = notNull("namespace", namespace);
109-
this.indexName = generateIndexName(notNull("keys", keys));
110+
this.indexKeys = notNull("indexKeys", indexKeys);
111+
this.indexName = null;
110112
this.writeConcern = writeConcern;
111113
}
112114

@@ -190,8 +192,13 @@ public void onResult(final Void result, final Throwable t) {
190192
}
191193

192194
private BsonDocument getCommand(final ConnectionDescription description) {
193-
BsonDocument command = new BsonDocument("dropIndexes", new BsonString(namespace.getCollectionName()))
194-
.append("index", new BsonString(indexName));
195+
BsonDocument command = new BsonDocument("dropIndexes", new BsonString(namespace.getCollectionName()));
196+
if (indexName != null) {
197+
command.put("index", new BsonString(indexName));
198+
} else {
199+
command.put("index", indexKeys);
200+
}
201+
195202
putIfNotZero(command, "maxTimeMS", maxTimeMS);
196203
appendWriteConcernToCommand(writeConcern, command, description);
197204
return command;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import com.mongodb.WriteConcern
2424
import org.bson.BsonDocument
2525
import org.bson.BsonInt32
2626
import org.bson.BsonInt64
27+
import org.bson.BsonString
2728
import org.bson.Document
2829
import org.bson.codecs.DocumentCodec
2930
import spock.lang.IgnoreIf
31+
import spock.lang.Unroll
3032

3133
import static com.mongodb.ClusterFixture.disableMaxTimeFailPoint
3234
import static com.mongodb.ClusterFixture.enableMaxTimeFailPoint
@@ -79,9 +81,9 @@ class DropIndexOperationSpecification extends OperationFunctionalSpecification {
7981
async << [true, false]
8082
}
8183

84+
@Unroll
8285
def 'should drop existing index by keys'() {
8386
given:
84-
def keys = new BsonDocument('theField', new BsonInt32(1))
8587
collectionHelper.createIndex(keys)
8688

8789
when:
@@ -93,7 +95,14 @@ class DropIndexOperationSpecification extends OperationFunctionalSpecification {
9395
indexes[0].name == '_id_'
9496

9597
where:
96-
async << [true, false]
98+
[keys, async] << [
99+
[new BsonDocument('theField', new BsonInt32(1)),
100+
new BsonDocument('theField', new BsonInt32(1)).append('theSecondField', new BsonInt32(-1)),
101+
new BsonDocument('theField', new BsonString('2d')),
102+
new BsonDocument('theField', new BsonString('hashed')),
103+
],
104+
[true, false]
105+
].combinations()
97106
}
98107

99108
@IgnoreIf({ isSharded() })

driver-legacy/src/main/com/mongodb/DBCollection.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,11 @@ public ReadOperation<BatchCursor<DBObject>> asReadOperation() {
21992199
* @mongodb.driver.manual core/indexes/ Indexes
22002200
*/
22012201
public void dropIndex(final DBObject index) {
2202-
dropIndex(getIndexNameFromIndexFields(index));
2202+
try {
2203+
executor.execute(new DropIndexOperation(getNamespace(), wrap(index), getWriteConcern()), getReadConcern());
2204+
} catch (MongoWriteConcernException e) {
2205+
throw createWriteConcernException(e);
2206+
}
22032207
}
22042208

22052209
/**
@@ -2473,31 +2477,6 @@ private CreateIndexesOperation createIndexOperation(final DBObject key, final DB
24732477
return new CreateIndexesOperation(getNamespace(), singletonList(request), writeConcern);
24742478
}
24752479

2476-
private String getIndexNameFromIndexFields(final DBObject index) {
2477-
StringBuilder indexName = new StringBuilder();
2478-
for (final String keyNames : index.keySet()) {
2479-
if (indexName.length() != 0) {
2480-
indexName.append('_');
2481-
}
2482-
indexName.append(keyNames).append('_');
2483-
Object keyType = index.get(keyNames);
2484-
if (keyType instanceof Integer) {
2485-
List<Integer> validIndexTypes = asList(1, -1);
2486-
if (!validIndexTypes.contains(keyType)) {
2487-
throw new UnsupportedOperationException("Unsupported index type: " + keyType);
2488-
}
2489-
indexName.append(((Integer) keyType));
2490-
} else if (keyType instanceof String) {
2491-
List<String> validIndexTypes = asList("2d", "2dsphere", "text", "geoHaystack", "hashed");
2492-
if (!validIndexTypes.contains(keyType)) {
2493-
throw new UnsupportedOperationException("Unsupported index type: " + keyType);
2494-
}
2495-
indexName.append(((String) keyType).replace(' ', '_'));
2496-
}
2497-
}
2498-
return indexName.toString();
2499-
}
2500-
25012480
Codec<DBObject> getObjectCodec() {
25022481
return objectCodec;
25032482
}

driver-legacy/src/test/functional/com/mongodb/DBCollectionFunctionalSpecification.groovy

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class DBCollectionFunctionalSpecification extends FunctionalSpecification {
210210

211211
then:
212212
def exception = thrown(MongoCommandException)
213-
exception.getErrorMessage().contains('index not found')
213+
exception.getErrorMessage().contains('can\'t find index')
214214
}
215215

216216
def 'should drop nested index'() {
@@ -799,6 +799,17 @@ class DBCollectionFunctionalSpecification extends FunctionalSpecification {
799799
result == document
800800
}
801801

802+
def 'should drop compound index by key'() {
803+
given:
804+
def indexKeys = new BasicDBObject('x', 1).append('y', -1)
805+
collection.createIndex(indexKeys)
806+
807+
when:
808+
collection.dropIndex(indexKeys)
809+
810+
then:
811+
collection.getIndexInfo().size() == 1
812+
}
802813

803814
def caseInsensitive = Collation.builder().locale('en').collationStrength(CollationStrength.SECONDARY).build()
804815
}

0 commit comments

Comments
 (0)