Skip to content

Commit 2e33f7c

Browse files
committed
Ensure GridFS index checking supports indexes created in the shell
JAVA-3599
1 parent 4c5fc2e commit 2e33f7c

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

driver-async/src/main/com/mongodb/async/client/gridfs/GridFSIndexCheckImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.bson.Document;
2828

2929
import java.util.ArrayList;
30+
import java.util.Map;
3031

3132
import static com.mongodb.ReadPreference.primary;
3233
import static com.mongodb.assertions.Assertions.notNull;
@@ -84,8 +85,14 @@ public void onResult(final ArrayList<Document> indexes, final Throwable t) {
8485
callback.onResult(null, t);
8586
} else {
8687
boolean hasIndex = false;
87-
for (Document indexDoc : indexes) {
88-
if (indexDoc.get("key", Document.class).equals(index)) {
88+
for (Document result : indexes) {
89+
Document indexDoc = result.get("key", new Document());
90+
for (final Map.Entry<String, Object> entry : indexDoc.entrySet()) {
91+
if (entry.getValue() instanceof Number) {
92+
entry.setValue(((Number) entry.getValue()).intValue());
93+
}
94+
}
95+
if (indexDoc.equals(index)) {
8996
hasIndex = true;
9097
break;
9198
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,29 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
492492
direct << [true, false]
493493
}
494494

495+
def 'should not create if index is numerically the same'() {
496+
when:
497+
run(filesCollection.&createIndex, new Document('filename', indexValue1).append('uploadDate', indexValue2))
498+
run(chunksCollection.&createIndex, new Document('files_id', indexValue1).append('n', indexValue2))
499+
def contentBytes = 'Hello GridFS' as byte[]
500+
501+
then:
502+
run(filesCollection.listIndexes().&into, []).size() == 2
503+
run(chunksCollection.listIndexes().&into, []).size() == 2
504+
505+
when:
506+
def outputStream = gridFSBucket.openUploadStream('myFile')
507+
run(outputStream.&write, ByteBuffer.wrap(contentBytes))
508+
run(outputStream.&close)
509+
510+
then:
511+
run(filesCollection.listIndexes().&into, []).size() == 2
512+
run(chunksCollection.listIndexes().&into, []).size() == 2
513+
514+
where:
515+
[indexValue1, indexValue2] << [[1, 1.0, 1L], [1, 1.0, 1L]].combinations()
516+
}
517+
495518
def 'should use the user provided codec registries for encoding / decoding data'() {
496519
given:
497520
def codecRegistry = fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)), MongoClients.getDefaultCodecRegistry())

driver-sync/src/main/com/mongodb/client/gridfs/GridFSBucketImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.io.InputStream;
4646
import java.io.OutputStream;
4747
import java.util.ArrayList;
48+
import java.util.Map;
4849

4950
import static com.mongodb.ReadPreference.primary;
5051
import static com.mongodb.assertions.Assertions.notNull;
@@ -534,8 +535,14 @@ private <T> boolean hasIndex(@Nullable final ClientSession clientSession, final
534535
}
535536

536537
ArrayList<Document> indexes = listIndexesIterable.into(new ArrayList<Document>());
537-
for (Document indexDoc : indexes) {
538-
if (indexDoc.get("key", Document.class).equals(index)) {
538+
for (Document result : indexes) {
539+
Document indexDoc = result.get("key", new Document());
540+
for (final Map.Entry<String, Object> entry : indexDoc.entrySet()) {
541+
if (entry.getValue() instanceof Number) {
542+
entry.setValue(((Number) entry.getValue()).intValue());
543+
}
544+
}
545+
if (indexDoc.equals(index)) {
539546
hasIndex = true;
540547
break;
541548
}

driver-sync/src/test/functional/com/mongodb/client/gridfs/GridFSBucketSmokeTestSpecification.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,33 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
466466
direct << [true, false]
467467
}
468468

469+
def 'should not create if index is numerically the same'() {
470+
when:
471+
filesCollection.createIndex(new Document('filename', indexValue1).append('uploadDate', indexValue2))
472+
chunksCollection.createIndex(new Document('files_id', indexValue1).append('n', indexValue2))
473+
def contentBytes = 'Hello GridFS' as byte[]
474+
475+
then:
476+
filesCollection.listIndexes().into([]).size() == 2
477+
chunksCollection.listIndexes().into([]).size() == 2
478+
479+
when:
480+
if (direct) {
481+
gridFSBucket.uploadFromStream('myFile', new ByteArrayInputStream(contentBytes));
482+
} else {
483+
def outputStream = gridFSBucket.openUploadStream('myFile')
484+
outputStream.write(contentBytes)
485+
outputStream.close()
486+
}
487+
488+
then:
489+
filesCollection.listIndexes().into([]).size() == 2
490+
chunksCollection.listIndexes().into([]).size() == 2
491+
492+
where:
493+
[direct, indexValue1, indexValue2] << [[true, false], [1, 1.0, 1L], [1, 1.0, 1L]].combinations()
494+
}
495+
469496
def 'should mark and reset'() {
470497
given:
471498
def content = 1 .. 1000 as byte[]

0 commit comments

Comments
 (0)