Skip to content

Commit 169aa0c

Browse files
committed
Updated IndexHelper to take any BsonNumber
JAVA-1971
1 parent 1697770 commit 169aa0c

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.mongodb.operation;
1818

1919
import org.bson.BsonDocument;
20-
import org.bson.BsonInt32;
20+
import org.bson.BsonNumber;
2121
import org.bson.BsonString;
2222
import org.bson.BsonValue;
2323

@@ -36,8 +36,8 @@ static String generateIndexName(final BsonDocument index) {
3636
}
3737
indexName.append(keyNames).append('_');
3838
BsonValue ascOrDescValue = index.get(keyNames);
39-
if (ascOrDescValue instanceof BsonInt32) {
40-
indexName.append(((BsonInt32) ascOrDescValue).getValue());
39+
if (ascOrDescValue instanceof BsonNumber) {
40+
indexName.append(((BsonNumber) ascOrDescValue).intValue());
4141
} else if (ascOrDescValue instanceof BsonString) {
4242
indexName.append(((BsonString) ascOrDescValue).getValue().replace(' ', '_'));
4343
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.mongodb.bulk.IndexRequest
2525
import org.bson.BsonDocument
2626
import org.bson.BsonDocumentWrapper
2727
import org.bson.BsonInt32
28+
import org.bson.BsonInt64
2829
import org.bson.BsonString
2930
import org.bson.Document
3031
import org.bson.codecs.DocumentCodec
@@ -86,6 +87,33 @@ class CreateIndexesOperationSpecification extends OperationFunctionalSpecificati
8687
getUserCreatedIndexes('key') == [field1Index]
8788
}
8889

90+
91+
def 'should be able to create a single index with a BsonInt64'() {
92+
given:
93+
def keys = new BsonDocument('field', new BsonInt64(1))
94+
def createIndexOperation = new CreateIndexesOperation(getNamespace(), [new IndexRequest(keys)])
95+
96+
when:
97+
createIndexOperation.execute(getBinding())
98+
99+
then:
100+
getUserCreatedIndexes('key') == [field1Index]
101+
}
102+
103+
@Category(Async)
104+
def 'should be able to create a single index with a BsonInt64 asynchronously'() {
105+
106+
given:
107+
def keys = new BsonDocument('field', new BsonInt64(1))
108+
def createIndexOperation = new CreateIndexesOperation(getNamespace(), [new IndexRequest(keys)])
109+
110+
when:
111+
executeAsync(createIndexOperation)
112+
113+
then:
114+
getUserCreatedIndexes('key') == [field1Index]
115+
}
116+
89117
@IgnoreIf({ !serverVersionAtLeast(asList(2, 6, 0)) })
90118
def 'should be able to create multiple indexes'() {
91119
given:

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import com.mongodb.MongoException
2121
import com.mongodb.OperationFunctionalSpecification
2222
import org.bson.BsonDocument
2323
import org.bson.BsonInt32
24+
import org.bson.BsonInt64
2425
import org.bson.Document
2526
import org.bson.codecs.DocumentCodec
2627
import org.junit.experimental.categories.Category
2728

2829
import static com.mongodb.ClusterFixture.executeAsync
2930
import static com.mongodb.ClusterFixture.getBinding
3031

31-
class DropIndexesOperationSpecification extends OperationFunctionalSpecification {
32+
class DropIndexOperationSpecification extends OperationFunctionalSpecification {
3233

3334
def 'should not error when dropping non-existent index on non-existent collection'() {
3435
when:
@@ -84,8 +85,8 @@ class DropIndexesOperationSpecification extends OperationFunctionalSpecification
8485
}
8586

8687
def 'should drop existing index by keys'() {
87-
def keys = new BsonDocument('theField', new BsonInt32(1))
8888
given:
89+
def keys = new BsonDocument('theField', new BsonInt32(1))
8990
collectionHelper.createIndex(keys)
9091

9192
when:
@@ -100,8 +101,40 @@ class DropIndexesOperationSpecification extends OperationFunctionalSpecification
100101
@Category(Async)
101102
def 'should drop existing index asynchronously'() {
102103
given:
103-
collectionHelper.createIndex(new BsonDocument('theField', new BsonInt32(1)))
104-
def operation = new DropIndexOperation(getNamespace(), 'theField_1');
104+
def keys = new BsonDocument('theField', new BsonInt32(1))
105+
collectionHelper.createIndex(keys)
106+
def operation = new DropIndexOperation(getNamespace(), keys);
107+
108+
when:
109+
executeAsync(operation)
110+
List<Document> indexes = getIndexes()
111+
112+
then:
113+
indexes.size() == 1
114+
indexes[0].name == '_id_'
115+
}
116+
117+
118+
def 'should drop existing index by key when using BsonInt64'() {
119+
given:
120+
def keys = new BsonDocument('theField', new BsonInt32(1))
121+
collectionHelper.createIndex(keys)
122+
123+
when:
124+
new DropIndexOperation(getNamespace(), new BsonDocument('theField', new BsonInt64(1))).execute(getBinding())
125+
List<Document> indexes = getIndexes()
126+
127+
then:
128+
indexes.size() == 1
129+
indexes[0].name == '_id_'
130+
}
131+
132+
@Category(Async)
133+
def 'should drop existing index by key when using BsonInt64 asynchronously'() {
134+
given:
135+
def keys = new BsonDocument('theField', new BsonInt32(1))
136+
collectionHelper.createIndex(keys)
137+
def operation = new DropIndexOperation(getNamespace(), new BsonDocument('theField', new BsonInt64(1)));
105138

106139
when:
107140
executeAsync(operation)

0 commit comments

Comments
 (0)