3333import org .bson .BsonObjectId ;
3434import org .bson .BsonValue ;
3535import org .bson .Document ;
36- import org .bson .codecs .configuration .CodecRegistry ;
3736import org .bson .conversions .Bson ;
3837import org .bson .types .ObjectId ;
3938
4948
5049@ SuppressWarnings ("deprecation" )
5150final 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 () {
0 commit comments