Skip to content

Commit 2bc6f3c

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

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

driver-core/src/main/com/mongodb/internal/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;
@@ -85,8 +86,14 @@ public void onResult(final ArrayList<Document> indexes, final Throwable t) {
8586
callback.onResult(null, t);
8687
} else {
8788
boolean hasIndex = false;
88-
for (Document indexDoc : indexes) {
89-
if (indexDoc.get("key", Document.class).equals(index)) {
89+
for (Document result : indexes) {
90+
Document indexDoc = result.get("key", new Document());
91+
for (final Map.Entry<String, Object> entry : indexDoc.entrySet()) {
92+
if (entry.getValue() instanceof Number) {
93+
entry.setValue(((Number) entry.getValue()).intValue());
94+
}
95+
}
96+
if (indexDoc.equals(index)) {
9097
hasIndex = true;
9198
break;
9299
}

driver-core/src/test/functional/com/mongodb/internal/async/client/gridfs/AsyncGridFSBucketSmokeTestSpecification.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,29 @@ class AsyncGridFSBucketSmokeTestSpecification extends FunctionalSpecification {
450450
run(chunksCollection.listIndexes().&into, []).size() == 1
451451
}
452452

453+
def 'should not create if index is numerically the same'() {
454+
when:
455+
run(filesCollection.&createIndex, new Document('filename', indexValue1).append('uploadDate', indexValue2))
456+
run(chunksCollection.&createIndex, new Document('files_id', indexValue1).append('n', indexValue2))
457+
def contentBytes = 'Hello GridFS' as byte[]
458+
459+
then:
460+
run(filesCollection.listIndexes().&into, []).size() == 2
461+
run(chunksCollection.listIndexes().&into, []).size() == 2
462+
463+
when:
464+
def outputStream = gridFSBucket.openUploadStream('myFile')
465+
run(outputStream.&write, ByteBuffer.wrap(contentBytes))
466+
run(outputStream.&close)
467+
468+
then:
469+
run(filesCollection.listIndexes().&into, []).size() == 2
470+
run(chunksCollection.listIndexes().&into, []).size() == 2
471+
472+
where:
473+
[indexValue1, indexValue2] << [[1, 1.0, 1L], [1, 1.0, 1L]].combinations()
474+
}
475+
453476
def 'should use the user provided codec registries for encoding / decoding data'() {
454477
given:
455478
def client = AsyncMongoClients.create(getMongoClientBuilderFromConnectionString()

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;
@@ -496,8 +497,14 @@ private <T> boolean hasIndex(@Nullable final ClientSession clientSession, final
496497
}
497498

498499
ArrayList<Document> indexes = listIndexesIterable.into(new ArrayList<Document>());
499-
for (Document indexDoc : indexes) {
500-
if (indexDoc.get("key", Document.class).equals(index)) {
500+
for (Document result : indexes) {
501+
Document indexDoc = result.get("key", new Document());
502+
for (final Map.Entry<String, Object> entry : indexDoc.entrySet()) {
503+
if (entry.getValue() instanceof Number) {
504+
entry.setValue(((Number) entry.getValue()).intValue());
505+
}
506+
}
507+
if (indexDoc.equals(index)) {
501508
hasIndex = true;
502509
break;
503510
}

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
@@ -458,6 +458,33 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
458458
direct << [true, false]
459459
}
460460

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

0 commit comments

Comments
 (0)