Skip to content

Commit ada96e7

Browse files
committed
Ensure that the underlying collections are updated correctly
When the user sets a new ReadConcern, ReadPreference or WriteConcern those changes should be reflected in the underlying files and chunks collections. JAVA-2049
1 parent 224426e commit ada96e7

File tree

3 files changed

+158
-125
lines changed

3 files changed

+158
-125
lines changed

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

+24-53
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.bson.BsonObjectId;
3434
import org.bson.BsonValue;
3535
import org.bson.Document;
36-
import org.bson.codecs.configuration.CodecRegistry;
3736
import org.bson.conversions.Bson;
3837
import org.bson.types.ObjectId;
3938

@@ -49,46 +48,28 @@
4948

5049
@SuppressWarnings("deprecation")
5150
final class GridFSBucketImpl implements GridFSBucket {
52-
private final MongoDatabase database;
51+
private static final int DEFAULT_CHUNKSIZE_BYTES = 255 * 1024;
5352
private final String bucketName;
5453
private final int chunkSizeBytes;
55-
private final WriteConcern writeConcern;
56-
private final ReadConcern readConcern;
57-
private final ReadPreference readPreference;
5854
private final MongoCollection<Document> filesCollection;
5955
private final MongoCollection<Document> chunksCollection;
60-
private final CodecRegistry codecRegistry;
6156
private volatile boolean checkedIndexes;
6257

6358
GridFSBucketImpl(final MongoDatabase database) {
6459
this(database, "fs");
6560
}
6661

6762
GridFSBucketImpl(final MongoDatabase database, final String bucketName) {
68-
this.database = notNull("database", database);
69-
this.bucketName = notNull("bucketName", bucketName);
70-
this.chunkSizeBytes = 255 * 1024;
71-
this.writeConcern = database.getWriteConcern();
72-
this.readConcern = database.getReadConcern();
73-
this.readPreference = database.getReadPreference();
74-
this.codecRegistry = getCodecRegistry();
75-
this.filesCollection = getFilesCollection();
76-
this.chunksCollection = getChunksCollection();
77-
}
78-
79-
GridFSBucketImpl(final MongoDatabase database, final String bucketName, final int chunkSizeBytes, final CodecRegistry codecRegistry,
80-
final ReadPreference readPreference, final WriteConcern writeConcern, final ReadConcern readConcern,
81-
final MongoCollection<Document> filesCollection, final MongoCollection<Document> chunksCollection,
82-
final boolean checkedIndexes) {
83-
this.database = notNull("database", database);
63+
this(notNull("bucketName", bucketName), DEFAULT_CHUNKSIZE_BYTES,
64+
getFilesCollection(notNull("database", database), bucketName),
65+
getChunksCollection(database, bucketName));
66+
}
67+
68+
GridFSBucketImpl(final String bucketName, final int chunkSizeBytes, final MongoCollection<Document> filesCollection,
69+
final MongoCollection<Document> chunksCollection) {
8470
this.bucketName = notNull("bucketName", bucketName);
8571
this.chunkSizeBytes = chunkSizeBytes;
86-
this.codecRegistry = notNull("codecRegistry", codecRegistry);
87-
this.readPreference = notNull("readPreference", readPreference);
88-
this.writeConcern = notNull("writeConcern", writeConcern);
89-
this.readConcern = notNull("readConcern", readConcern);
90-
this.checkedIndexes = checkedIndexes;
91-
this.filesCollection = notNull("filesCollection", filesCollection);
72+
this.filesCollection = notNull("filesCollection", filesCollection);
9273
this.chunksCollection = notNull("chunksCollection", chunksCollection);
9374
}
9475

@@ -104,41 +85,40 @@ public int getChunkSizeBytes() {
10485

10586
@Override
10687
public ReadPreference getReadPreference() {
107-
return readPreference;
88+
return filesCollection.getReadPreference();
10889
}
10990

11091
@Override
11192
public WriteConcern getWriteConcern() {
112-
return writeConcern;
93+
return filesCollection.getWriteConcern();
11394
}
11495

11596
@Override
11697
public ReadConcern getReadConcern() {
117-
return readConcern;
98+
return filesCollection.getReadConcern();
11899
}
119100

120101
@Override
121102
public GridFSBucket withChunkSizeBytes(final int chunkSizeBytes) {
122-
return new GridFSBucketImpl(database, bucketName, chunkSizeBytes, codecRegistry, readPreference, writeConcern, readConcern,
123-
filesCollection, chunksCollection, checkedIndexes);
103+
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection, chunksCollection);
124104
}
125105

126106
@Override
127107
public GridFSBucket withReadPreference(final ReadPreference readPreference) {
128-
return new GridFSBucketImpl(database, bucketName, chunkSizeBytes, codecRegistry, readPreference, writeConcern, readConcern,
129-
filesCollection, chunksCollection, checkedIndexes);
108+
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withReadPreference(readPreference),
109+
chunksCollection.withReadPreference(readPreference));
130110
}
131111

132112
@Override
133113
public GridFSBucket withWriteConcern(final WriteConcern writeConcern) {
134-
return new GridFSBucketImpl(database, bucketName, chunkSizeBytes, codecRegistry, readPreference, writeConcern, readConcern,
135-
filesCollection, chunksCollection, checkedIndexes);
114+
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withWriteConcern(writeConcern),
115+
chunksCollection.withWriteConcern(writeConcern));
136116
}
137117

138118
@Override
139119
public GridFSBucket withReadConcern(final ReadConcern readConcern) {
140-
return new GridFSBucketImpl(database, bucketName, chunkSizeBytes, codecRegistry, readPreference, writeConcern, readConcern,
141-
filesCollection, chunksCollection, checkedIndexes);
120+
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withReadConcern(readConcern),
121+
chunksCollection.withReadConcern(readConcern));
142122
}
143123

144124
@Override
@@ -252,22 +232,13 @@ public void drop() {
252232
chunksCollection.drop();
253233
}
254234

255-
private CodecRegistry getCodecRegistry() {
256-
return fromRegistries(database.getCodecRegistry(), MongoClient.getDefaultCodecRegistry());
257-
}
258-
259-
private MongoCollection<Document> getFilesCollection() {
260-
return database.getCollection(bucketName + ".files")
261-
.withCodecRegistry(codecRegistry)
262-
.withReadPreference(readPreference)
263-
.withWriteConcern(writeConcern);
235+
private static MongoCollection<Document> getFilesCollection(final MongoDatabase database, final String bucketName) {
236+
return database.getCollection(bucketName + ".files").withCodecRegistry(fromRegistries(database.getCodecRegistry(),
237+
MongoClient.getDefaultCodecRegistry()));
264238
}
265239

266-
private MongoCollection<Document> getChunksCollection() {
267-
return database.getCollection(bucketName + ".chunks")
268-
.withCodecRegistry(MongoClient.getDefaultCodecRegistry())
269-
.withReadPreference(readPreference)
270-
.withWriteConcern(writeConcern);
240+
private static MongoCollection<Document> getChunksCollection(final MongoDatabase database, final String bucketName) {
241+
return database.getCollection(bucketName + ".chunks").withCodecRegistry(MongoClient.getDefaultCodecRegistry());
271242
}
272243

273244
private void checkCreateIndex() {

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

+26
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ import com.mongodb.client.MongoCollection
2222
import com.mongodb.client.MongoDatabase
2323
import com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions
2424
import com.mongodb.client.gridfs.model.GridFSUploadOptions
25+
import org.bson.BsonDocument
2526
import org.bson.Document
27+
import org.bson.UuidRepresentation
28+
import org.bson.codecs.UuidCodec
2629
import org.bson.types.ObjectId
2730
import spock.lang.Unroll
2831

2932
import java.security.MessageDigest
3033

3134
import static com.mongodb.Fixture.getDefaultDatabaseName
3235
import static com.mongodb.Fixture.getMongoClient
36+
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs
37+
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries
3338

3439
class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
3540
protected MongoDatabase mongoDatabase;
@@ -476,4 +481,25 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
476481
!collectionNames.contains(filesCollection.getNamespace().collectionName)
477482
!collectionNames.contains(chunksCollection.getNamespace().collectionName)
478483
}
484+
485+
def 'should use the user provided codec registries for encoding / decoding data'() {
486+
given:
487+
def codecRegistry = fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)), MongoClient.getDefaultCodecRegistry())
488+
def database = getMongoClient().getDatabase(getDefaultDatabaseName()).withCodecRegistry(codecRegistry)
489+
def uuid = UUID.randomUUID()
490+
def fileMeta = new Document('uuid', uuid)
491+
def gridFSBucket = GridFSBuckets.create(database)
492+
493+
when:
494+
def fileId = gridFSBucket.uploadFromStream('myFile', new ByteArrayInputStream(multiChunkString as byte[]),
495+
new GridFSUploadOptions().metadata(fileMeta));
496+
497+
def file = gridFSBucket.find(new Document('_id', fileId)).first()
498+
499+
then:
500+
file.getMetadata() == fileMeta
501+
502+
then:
503+
filesCollection.find(BsonDocument).first().getDocument('metadata').getBinary('uuid').getType() == 4 as byte
504+
}
479505
}

0 commit comments

Comments
 (0)